diff --git a/pom.xml b/pom.xml
index a18fe0e..310652a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -16,6 +16,8 @@
17
checkstyle.xml
+ 1.5.5.Final
+ 0.2.0
@@ -52,14 +54,98 @@
test
+
+ org.mapstruct
+ mapstruct
+ ${org.mapstruct.version}
+
+
+
+ org.mapstruct
+ mapstruct-processor
+ ${org.mapstruct.version}
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.0
+
+
+ 1.8
+
+
+ org.mapstruct
+ mapstruct-processor
+ ${org.mapstruct.version}
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-checkstyle-plugin
+ 3.3.0
+
+
+ compile
+
+ check
+
+
+
+
+ ${maven.checkstyle.plugin.configLocation}
+ true
+ true
+ false
+ src
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.13.0
+
+
+ 11
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+
+
+ org.mapstruct
+ mapstruct-processor
+ ${org.mapstruct.version}
+
+
+ org.projectlombok
+ lombok-mapstruct-binding
+ ${lombok.mapstruct.binding.version}
+
+
+
+
+
org.springframework.boot
spring-boot-maven-plugin
+
org.apache.maven.plugins
maven-checkstyle-plugin
diff --git a/src/main/java/mate/academy/BookStoreApplication.java b/src/main/java/mate/academy/BookStoreApplication.java
index a9d16a1..a399bcd 100644
--- a/src/main/java/mate/academy/BookStoreApplication.java
+++ b/src/main/java/mate/academy/BookStoreApplication.java
@@ -1,40 +1,13 @@
package mate.academy;
-import java.math.BigDecimal;
-import mate.academy.model.Book;
-import mate.academy.repository.BookRepository;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class BookStoreApplication {
- private BookRepository bookRepository;
-
- @Autowired
- public BookStoreApplication(BookRepository bookRepository) {
- this.bookRepository = bookRepository;
- }
public static void main(String[] args) {
SpringApplication.run(BookStoreApplication.class, args);
}
- @Bean
- public CommandLineRunner commandLineRunner() {
- return args -> {
- Book book = new Book();
- book.setAuthor("Jack");
- book.setId(1L);
- book.setPrice(BigDecimal.valueOf(13));
- book.setDescription("123");
- book.setCoverImage("/images");
- book.setIsbn("123");
- book.setTitle("title");
-
- bookRepository.save(book);
- };
- }
}
diff --git a/src/main/java/mate/academy/config/MapperConfig.java b/src/main/java/mate/academy/config/MapperConfig.java
new file mode 100644
index 0000000..cd4bdbf
--- /dev/null
+++ b/src/main/java/mate/academy/config/MapperConfig.java
@@ -0,0 +1,13 @@
+package mate.academy.config;
+
+import org.mapstruct.InjectionStrategy;
+import org.mapstruct.NullValueCheckStrategy;
+
+@org.mapstruct. MapperConfig(
+ componentModel = "spring",
+ injectionStrategy = InjectionStrategy.CONSTRUCTOR,
+ nullValueCheckStrategy = NullValueCheckStrategy. ALWAYS,
+ implementationPackage = ".impl"
+)
+public class MapperConfig {
+}
diff --git a/src/main/java/mate/academy/controller/BookController.java b/src/main/java/mate/academy/controller/BookController.java
new file mode 100644
index 0000000..be94dfd
--- /dev/null
+++ b/src/main/java/mate/academy/controller/BookController.java
@@ -0,0 +1,35 @@
+package mate.academy.controller;
+
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import mate.academy.dto.BookDto;
+import mate.academy.dto.CreateBookRequestDto;
+import mate.academy.service.BookService;
+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.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RequiredArgsConstructor
+@RestController
+@RequestMapping("/books")
+public class BookController {
+ private final BookService bookService;
+
+ @GetMapping
+ public List getAll() {
+ return bookService.getAll();
+ }
+
+ @PostMapping
+ public BookDto save(@RequestBody CreateBookRequestDto requestDto) {
+ return bookService.save(requestDto);
+ }
+
+ @GetMapping("/{id}")
+ public BookDto getBookById(@PathVariable Long id) {
+ return bookService.getBookById(id);
+ }
+}
diff --git a/src/main/java/mate/academy/dto/BookDto.java b/src/main/java/mate/academy/dto/BookDto.java
new file mode 100644
index 0000000..34cc1e1
--- /dev/null
+++ b/src/main/java/mate/academy/dto/BookDto.java
@@ -0,0 +1,15 @@
+package mate.academy.dto;
+
+import java.math.BigDecimal;
+import lombok.Data;
+
+@Data
+public class BookDto {
+ private Long id;
+ private String title;
+ private String author;
+ private String isbn;
+ private BigDecimal price;
+ private String description;
+ private String coverImage;
+}
diff --git a/src/main/java/mate/academy/dto/CreateBookRequestDto.java b/src/main/java/mate/academy/dto/CreateBookRequestDto.java
new file mode 100644
index 0000000..fd15546
--- /dev/null
+++ b/src/main/java/mate/academy/dto/CreateBookRequestDto.java
@@ -0,0 +1,14 @@
+package mate.academy.dto;
+
+import java.math.BigDecimal;
+import lombok.Data;
+
+@Data
+public class CreateBookRequestDto {
+ private String title;
+ private String author;
+ private String isbn;
+ private BigDecimal price;
+ private String description;
+ private String coverImage;
+}
diff --git a/src/main/java/mate/academy/exception/EntityNotFoundException.java b/src/main/java/mate/academy/exception/EntityNotFoundException.java
new file mode 100644
index 0000000..33a903a
--- /dev/null
+++ b/src/main/java/mate/academy/exception/EntityNotFoundException.java
@@ -0,0 +1,7 @@
+package mate.academy.exception;
+
+public class EntityNotFoundException extends RuntimeException {
+ public EntityNotFoundException(String message) {
+ super(message);
+ }
+}
diff --git a/src/main/java/mate/academy/mapper/BookMapper.java b/src/main/java/mate/academy/mapper/BookMapper.java
new file mode 100644
index 0000000..5d93334
--- /dev/null
+++ b/src/main/java/mate/academy/mapper/BookMapper.java
@@ -0,0 +1,16 @@
+package mate.academy.mapper;
+
+import mate.academy.config.MapperConfig;
+import mate.academy.dto.BookDto;
+import mate.academy.dto.CreateBookRequestDto;
+import mate.academy.model.Book;
+import org.mapstruct.Mapper;
+import org.mapstruct.Mapping;
+
+@Mapper(config = MapperConfig.class)
+public interface BookMapper {
+ BookDto toDto(Book book);
+
+ @Mapping(target = "id", ignore = true)
+ Book toModel(CreateBookRequestDto requestDto);
+}
diff --git a/src/main/java/mate/academy/service/BookService.java b/src/main/java/mate/academy/service/BookService.java
index 7cc0c10..3e7d5b1 100644
--- a/src/main/java/mate/academy/service/BookService.java
+++ b/src/main/java/mate/academy/service/BookService.java
@@ -1,25 +1,37 @@
package mate.academy.service;
import java.util.List;
+import lombok.RequiredArgsConstructor;
+import mate.academy.dto.BookDto;
+import mate.academy.dto.CreateBookRequestDto;
+import mate.academy.exception.EntityNotFoundException;
+import mate.academy.mapper.BookMapper;
import mate.academy.model.Book;
import mate.academy.repository.BookRepository;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
+@RequiredArgsConstructor
@Service
public class BookService {
private final BookRepository bookRepository;
- @Autowired
- public BookService(BookRepository bookRepository) {
- this.bookRepository = bookRepository;
+ private final BookMapper bookMapper;
+
+ public BookDto save(CreateBookRequestDto requestDto) {
+ Book book = bookMapper.toModel(requestDto);
+ return bookMapper.toDto(bookRepository.save(book));
}
- public Book save(Book book) {
- return bookRepository.save(book);
+ public List getAll() {
+ return bookRepository.findAll().stream()
+ .map(bookMapper::toDto)
+ .toList();
}
- public List findAll() {
- return bookRepository.findAll();
+ public BookDto getBookById(Long id) {
+ Book book = bookRepository.findById(id)
+ .orElseThrow(
+ () -> new EntityNotFoundException("Book with id " + id + " not found"));
+ return bookMapper.toDto(book);
}
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index a1ef8bf..2bb012b 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -7,3 +7,5 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
+
+server.servlet.context-path=/api