Skip to content

Commit

Permalink
implemented soft delete, liquibase support, delete and update book me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
TarasYashchuk committed Jun 18, 2024
1 parent d75993d commit 08259ac
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 2 deletions.
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<maven.checkstyle.plugin.configLocation>checkstyle.xml</maven.checkstyle.plugin.configLocation>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
<liquibase.version>4.28.0</liquibase.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -71,6 +72,12 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -114,6 +121,15 @@
</configuration>
</plugin>

<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/mate/academy/controller/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
import lombok.RequiredArgsConstructor;
import mate.academy.dto.BookDto;
import mate.academy.dto.CreateBookRequestDto;
import mate.academy.dto.UpdateBookRequestDto;
import mate.academy.service.BookService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
Expand All @@ -32,4 +37,16 @@ public BookDto save(@RequestBody CreateBookRequestDto requestDto) {
public BookDto getBookById(@PathVariable Long id) {
return bookService.getBookById(id);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
bookService.deleteById(id);
}

@ResponseStatus(HttpStatus.OK)
@PutMapping("/{id}")
public BookDto updateBook(@PathVariable Long id, @RequestBody UpdateBookRequestDto requestDto) {
return bookService.updateBookDetails(id, requestDto);
}
}
14 changes: 14 additions & 0 deletions src/main/java/mate/academy/dto/UpdateBookRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mate.academy.dto;

import java.math.BigDecimal;
import lombok.Data;

@Data
public class UpdateBookRequestDto {
private String title;
private String author;
private String isbn;
private BigDecimal price;
private String description;
private String coverImage;
}
5 changes: 5 additions & 0 deletions src/main/java/mate/academy/mapper/BookMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import mate.academy.config.MapperConfig;
import mate.academy.dto.BookDto;
import mate.academy.dto.CreateBookRequestDto;
import mate.academy.dto.UpdateBookRequestDto;
import mate.academy.model.Book;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(config = MapperConfig.class)
public interface BookMapper {
BookDto toDto(Book book);

@Mapping(target = "id", ignore = true)
void updateBookFromDto(UpdateBookRequestDto dto, @MappingTarget Book entity);

@Mapping(target = "id", ignore = true)
Book toModel(CreateBookRequestDto requestDto);
}
6 changes: 6 additions & 0 deletions src/main/java/mate/academy/model/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;

@Entity
@Getter
@Setter
@SQLDelete(sql = "UPDATE books set is_deleted = true WHERE id=?")
@SQLRestriction("is_deleted=false")
@Table(name = "books")
public class Book {
@Id
Expand All @@ -28,4 +32,6 @@ public class Book {
private BigDecimal price;
private String description;
private String coverImage;
@Column(nullable = false, name = "is_deleted", columnDefinition = "BIT")
private boolean isDeleted = false;
}
12 changes: 12 additions & 0 deletions src/main/java/mate/academy/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.RequiredArgsConstructor;
import mate.academy.dto.BookDto;
import mate.academy.dto.CreateBookRequestDto;
import mate.academy.dto.UpdateBookRequestDto;
import mate.academy.exception.EntityNotFoundException;
import mate.academy.mapper.BookMapper;
import mate.academy.model.Book;
Expand Down Expand Up @@ -34,4 +35,15 @@ public BookDto getBookById(Long id) {
() -> new EntityNotFoundException("Book with id " + id + " not found"));
return bookMapper.toDto(book);
}

public void deleteById(Long id) {
bookRepository.deleteById(id);
}

public BookDto updateBookDetails(Long id, UpdateBookRequestDto requestDto) {
Book book = bookRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Book not found with id " + id));
bookMapper.updateBookFromDto(requestDto,book);
return bookMapper.toDto(bookRepository.save(book));
}
}
6 changes: 4 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ spring.datasource.url=jdbc:mysql://localhost:3306/bookStore
spring.datasource.username=root
spring.datasource.password=12345678
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.type=bit

server.servlet.context-path=/api
48 changes: 48 additions & 0 deletions src/main/resources/db/changelog/changes/01-create-books-table.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
databaseChangeLog:
- changeSet:
id: create-book-table
author: mate_academy
changes:
- createTable:
tableName: books
columns:
- column:
name: id
type: BIGINT
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: title
type: VARCHAR(255)
constraints:
nullable: false
- column:
name: author
type: VARCHAR(255)
constraints:
nullable: false
- column:
name: isbn
type: VARCHAR(255)
constraints:
nullable: false
unique: true
- column:
name: price
type: DECIMAL(19, 2)
constraints:
nullable: false
- column:
name: description
type: TEXT
- column:
name: cover_image
type: VARCHAR(255)
- column:
name: is_deleted
type: BIT
defaultValueBoolean: false
constraints:
nullable: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
databaseChangeLog:
- changeSet:
id: modify-is_deleted-column-type
author: mate_academy
changes:
- modifyDataType:
columnName: is_deleted
newDataType: BIT
tableName: books
5 changes: 5 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
databaseChangeLog:
- include:
file: db/changelog/changes/01-create-books-table.yaml
- include:
file: db/changelog/changes/02-modify-is_deleted-column-type.yaml
4 changes: 4 additions & 0 deletions src/main/resources/liquibase.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
url=jdbc:mysql://localhost:3306/BookStore
username=root
password=12345678
changeLogFile=/db/changelog/db.changelog-master.yaml

0 comments on commit 08259ac

Please sign in to comment.