Skip to content

Commit

Permalink
Merged branch main. Resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
nklimovych committed Apr 28, 2024
2 parents 47a0ef6 + a77f38f commit 69c736b
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 17 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
<version>6.5.0.CR2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down Expand Up @@ -69,6 +69,16 @@
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.27.0</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>4.27.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import mate.academy.bookstore.dto.BookDto;
import mate.academy.bookstore.dto.BookRequestDto;
import mate.academy.bookstore.dto.BookSearchParametersDto;
import mate.academy.bookstore.dto.CreateBookRequestDto;
import mate.academy.bookstore.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.RestController;
Expand All @@ -30,10 +33,21 @@ public BookDto getBookById(@PathVariable Long id) {
}

@PostMapping
public BookDto createBook(@RequestBody CreateBookRequestDto requestDto) {
public BookDto createBook(@RequestBody BookRequestDto requestDto) {
return bookService.save(requestDto);
}

@PutMapping("/{id}")
public BookDto updateBook(@PathVariable Long id, @RequestBody BookRequestDto requestDto) {
return bookService.updateById(id, requestDto);
}

@DeleteMapping("/{id}")
public HttpStatus deleteBook(@PathVariable Long id) {
bookService.delete(id);
return HttpStatus.NO_CONTENT;
}

@GetMapping("/search")
public List<BookDto> searchBook(BookSearchParametersDto parametersDto) {
return bookService.search(parametersDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Data;

@Data
public class CreateBookRequestDto {
public class BookRequestDto {
private String title;
private String author;
private String isbn;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/mate/academy/bookstore/mapper/BookMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import mate.academy.bookstore.config.MapperConfig;
import mate.academy.bookstore.dto.BookDto;
import mate.academy.bookstore.dto.CreateBookRequestDto;
import mate.academy.bookstore.dto.BookRequestDto;
import mate.academy.bookstore.model.Book;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

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

Book toModel(CreateBookRequestDto requestDto);
@Mapping(target = "id", ignore = true)
@Mapping(target = "isDeleted", ignore = true)
Book toModel(BookRequestDto requestDto);
}
21 changes: 19 additions & 2 deletions src/main/java/mate/academy/bookstore/model/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,40 @@
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.math.BigDecimal;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;

@Getter
@Setter
@EqualsAndHashCode(of = {"id", "title", "author", "isbn"})
@Entity
@Data
@SQLDelete(sql = "UPDATE books SET is_deleted = true WHERE id=?")
@SQLRestriction(value = "is_deleted=false")
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String title;

@Column(nullable = false)
private String author;

@Column(nullable = false, unique = true)
private String isbn;

@Column(nullable = false)
private BigDecimal price;

private String description;

private String coverImage;

@Column(nullable = false)
private Boolean isDeleted;
}
8 changes: 6 additions & 2 deletions src/main/java/mate/academy/bookstore/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import java.util.List;
import mate.academy.bookstore.dto.BookDto;
import mate.academy.bookstore.dto.BookRequestDto;
import mate.academy.bookstore.dto.BookSearchParametersDto;
import mate.academy.bookstore.dto.CreateBookRequestDto;

public interface BookService {
BookDto save(CreateBookRequestDto requestDto);
BookDto save(BookRequestDto requestDto);

List<BookDto> findAll();

BookDto findById(Long id);

List<BookDto> search(BookSearchParametersDto parametersDto);

BookDto updateById(Long id, BookRequestDto requestDto);

void delete(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.List;
import mate.academy.bookstore.dto.BookDto;
import mate.academy.bookstore.dto.BookRequestDto;
import mate.academy.bookstore.dto.BookSearchParametersDto;
import mate.academy.bookstore.dto.CreateBookRequestDto;
import mate.academy.bookstore.exception.EntityNotFoundException;
import mate.academy.bookstore.mapper.BookMapper;
import mate.academy.bookstore.model.Book;
Expand All @@ -29,7 +29,7 @@ public BookServiceImpl(BookRepository bookRepository, BookMapper bookMapper,
}

@Override
public BookDto save(CreateBookRequestDto requestDto) {
public BookDto save(BookRequestDto requestDto) {
Book book = bookMapper.toModel(requestDto);
Book savedBook = bookRepository.save(book);
return bookMapper.toDto(savedBook);
Expand All @@ -44,10 +44,25 @@ public List<BookDto> findAll() {

public BookDto findById(Long id) {
Book book = bookRepository.findById(id).orElseThrow(
() -> new EntityNotFoundException("Entity not found by id " + id));
() -> new EntityNotFoundException("Book not found by id " + id));
return bookMapper.toDto(book);
}

public BookDto updateById(Long id, BookRequestDto requestDto) {
bookRepository.findById(id).orElseThrow(
() -> new EntityNotFoundException("Book not found by id " + id));

Book book = bookMapper.toModel(requestDto);
book.setId(id);
Book savedBook = bookRepository.save(book);
return bookMapper.toDto(savedBook);
}

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

@Override
public List<BookDto> search(BookSearchParametersDto params) {
Specification<Book> specification = specificationBuilder.build(params);
Expand Down
9 changes: 5 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
spring.application.name=book-store
spring.datasource.url=jdbc:mysql://localhost:3306/bookstore?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
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.open-in-view=false
52 changes: 52 additions & 0 deletions src/main/resources/db/changelog/changes/01-create-book-table.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
databaseChangeLog:
- changeSet:
id: create-books-table
author: nklimovych
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(10, 2)
constraints:
nullable: false
- column:
name: description
type: varchar(1000)
constraints:
nullable: true
- column:
name: cover_image
type: varchar(1000)
constraints:
nullable: true
- column:
name: is_deleted
type: bit
defaultValueBoolean: false
constraints:
nullable: false
3 changes: 3 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,3 @@
databaseChangeLog:
- include:
file: db/changelog/changes/01-create-book-table.yaml

0 comments on commit 69c736b

Please sign in to comment.