From ab3643265e3e184947676dc91d383e68f2c32d30 Mon Sep 17 00:00:00 2001 From: JackKaif Date: Tue, 20 Feb 2024 12:16:10 +0300 Subject: [PATCH] Complete service layers task --- .../service-layer/settings.gradle.kts | 2 +- .../controller/AuthorsController.java | 37 +++++++++++++++- .../exercise/controller/BooksController.java | 37 +++++++++++++++- .../java/exercise/mapper/AuthorMapper.java | 4 +- .../main/java/exercise/mapper/BookMapper.java | 8 +++- .../java/exercise/service/AuthorService.java | 41 ++++++++++++++++- .../java/exercise/service/BookService.java | 44 +++++++++++++++++-- .../controller/AuthorsControllerTest.java | 3 +- .../controller/BooksControllerTest.java | 1 + 9 files changed, 166 insertions(+), 11 deletions(-) diff --git a/java-spring-ru/service-layer/settings.gradle.kts b/java-spring-ru/service-layer/settings.gradle.kts index 3196f69..85c41b7 100644 --- a/java-spring-ru/service-layer/settings.gradle.kts +++ b/java-spring-ru/service-layer/settings.gradle.kts @@ -1,3 +1,3 @@ -rootProject.name = "one-to-many" +rootProject.name = "service-layer" // spring.jpa.generate-ddl = true // spring.jpa.hibernate.ddl-auto diff --git a/java-spring-ru/service-layer/src/main/java/exercise/controller/AuthorsController.java b/java-spring-ru/service-layer/src/main/java/exercise/controller/AuthorsController.java index 7f2eb52..0ab0c5b 100644 --- a/java-spring-ru/service-layer/src/main/java/exercise/controller/AuthorsController.java +++ b/java-spring-ru/service-layer/src/main/java/exercise/controller/AuthorsController.java @@ -6,6 +6,7 @@ import exercise.service.AuthorService; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.http.HttpStatus; +import java.net.URI; import java.util.List; @RestController @@ -27,6 +29,39 @@ public class AuthorsController { private AuthorService authorService; // BEGIN - + @GetMapping("") + public ResponseEntity> index() { + var authors = authorService.getAll(); + return ResponseEntity.ok() + .body(authors); + } + + @GetMapping("/{id}") + public ResponseEntity show(@PathVariable Long id) { + var author = authorService.findById(id); + return ResponseEntity.ok() + .body(author); + } + + @PostMapping("") + public ResponseEntity create(@RequestBody AuthorCreateDTO newAuthor) { + var author = authorService.create(newAuthor); + return ResponseEntity.created(URI.create("/authors")) + .body(author); + } + + @PutMapping("/{id}") + public ResponseEntity update(@PathVariable Long id, + @RequestBody AuthorUpdateDTO editedAuthor) { + var author = authorService.update(id, editedAuthor); + return ResponseEntity.ok() + .body(author); + } + + @DeleteMapping("/{id}") + public ResponseEntity delete(@PathVariable Long id) { + authorService.delete(id); + return ResponseEntity.ok().build(); + } // END } diff --git a/java-spring-ru/service-layer/src/main/java/exercise/controller/BooksController.java b/java-spring-ru/service-layer/src/main/java/exercise/controller/BooksController.java index 4bcd065..6a82405 100644 --- a/java-spring-ru/service-layer/src/main/java/exercise/controller/BooksController.java +++ b/java-spring-ru/service-layer/src/main/java/exercise/controller/BooksController.java @@ -1,5 +1,6 @@ package exercise.controller; +import java.net.URI; import java.util.List; import exercise.dto.BookCreateDTO; @@ -8,6 +9,7 @@ import exercise.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -26,6 +28,39 @@ public class BooksController { private BookService bookService; // BEGIN - + @GetMapping("") + public ResponseEntity> index() { + var books = bookService.getAll(); + return ResponseEntity.ok() + .body(books); + } + + @GetMapping("/{id}") + public ResponseEntity show(@PathVariable Long id) { + var book = bookService.findById(id); + return ResponseEntity.ok() + .body(book); + } + + @PostMapping("") + public ResponseEntity create(@RequestBody BookCreateDTO newBook) { + var book = bookService.create(newBook); + return ResponseEntity.created(URI.create("/books")) + .body(book); + } + + @PutMapping("/{id}") + public ResponseEntity update(@PathVariable Long id, + @RequestBody BookUpdateDTO editedBook) { + var book = bookService.update(id, editedBook); + return ResponseEntity.ok() + .body(book); + } + + @DeleteMapping("/{id}") + public ResponseEntity delete(@PathVariable Long id) { + bookService.delete(id); + return ResponseEntity.ok().build(); + } // END } diff --git a/java-spring-ru/service-layer/src/main/java/exercise/mapper/AuthorMapper.java b/java-spring-ru/service-layer/src/main/java/exercise/mapper/AuthorMapper.java index 0928f9c..89ba0f5 100644 --- a/java-spring-ru/service-layer/src/main/java/exercise/mapper/AuthorMapper.java +++ b/java-spring-ru/service-layer/src/main/java/exercise/mapper/AuthorMapper.java @@ -19,7 +19,9 @@ public abstract class AuthorMapper { // BEGIN - + public abstract Author map(AuthorCreateDTO dto); + + public abstract AuthorDTO map(Author model); // END public abstract void update(AuthorUpdateDTO dto, @MappingTarget Author model); diff --git a/java-spring-ru/service-layer/src/main/java/exercise/mapper/BookMapper.java b/java-spring-ru/service-layer/src/main/java/exercise/mapper/BookMapper.java index d01c4d8..8f9126f 100644 --- a/java-spring-ru/service-layer/src/main/java/exercise/mapper/BookMapper.java +++ b/java-spring-ru/service-layer/src/main/java/exercise/mapper/BookMapper.java @@ -20,7 +20,13 @@ public abstract class BookMapper { // BEGIN - + @Mapping(target = "author", source = "authorId") + public abstract Book map(BookCreateDTO dto); + + @Mapping(target = "authorId", source = "author.id") + @Mapping(target = "authorFirstName", source = "author.firstName") + @Mapping(target = "authorLastName", source = "author.lastName") + public abstract BookDTO map(Book model); // END @Mapping(target = "author", source = "authorId") diff --git a/java-spring-ru/service-layer/src/main/java/exercise/service/AuthorService.java b/java-spring-ru/service-layer/src/main/java/exercise/service/AuthorService.java index 0e6b459..39c7f87 100644 --- a/java-spring-ru/service-layer/src/main/java/exercise/service/AuthorService.java +++ b/java-spring-ru/service-layer/src/main/java/exercise/service/AuthorService.java @@ -8,12 +8,51 @@ import exercise.repository.AuthorRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestBody; import java.util.List; @Service public class AuthorService { // BEGIN - + @Autowired + private AuthorRepository authorRepository; + + @Autowired + private AuthorMapper authorMapper; + + public List getAll() { + return authorRepository.findAll().stream() + .map(authorMapper::map) + .toList(); + } + + public AuthorDTO create(AuthorCreateDTO newAuthor) { + var author = authorMapper.map(newAuthor); + authorRepository.save(author); + return authorMapper.map(author); + } + + public AuthorDTO findById(Long id) { + var author = authorRepository.findById(id) + .orElseThrow(() -> { + throw new ResourceNotFoundException("Author with id " + id + " not found"); + }); + return authorMapper.map(author); + } + + public AuthorDTO update(Long id, AuthorUpdateDTO editedAuthor) { + var author = authorRepository.findById(id) + .orElseThrow(() -> { + throw new ResourceNotFoundException("Author with id " + id + " not found"); + }); + authorMapper.update(editedAuthor, author); + authorRepository.save(author); + return authorMapper.map(author); + } + + public void delete(Long id) { + authorRepository.deleteById(id); + } // END } diff --git a/java-spring-ru/service-layer/src/main/java/exercise/service/BookService.java b/java-spring-ru/service-layer/src/main/java/exercise/service/BookService.java index d84530b..86e3499 100644 --- a/java-spring-ru/service-layer/src/main/java/exercise/service/BookService.java +++ b/java-spring-ru/service-layer/src/main/java/exercise/service/BookService.java @@ -1,8 +1,6 @@ package exercise.service; -import exercise.dto.BookCreateDTO; -import exercise.dto.BookDTO; -import exercise.dto.BookUpdateDTO; +import exercise.dto.*; import exercise.exception.ResourceNotFoundException; import exercise.mapper.BookMapper; import exercise.repository.BookRepository; @@ -14,6 +12,44 @@ @Service public class BookService { // BEGIN - + @Autowired + private BookRepository bookRepository; + + @Autowired + private BookMapper bookMapper; + + public List getAll() { + return bookRepository.findAll().stream() + .map(bookMapper::map) + .toList(); + } + + public BookDTO create(BookCreateDTO newBook) { + var book = bookMapper.map(newBook); + bookRepository.save(book); + return bookMapper.map(book); + } + + public BookDTO findById(Long id) { + var book = bookRepository.findById(id) + .orElseThrow(() -> { + throw new ResourceNotFoundException("Book with id " + id + " not found"); + }); + return bookMapper.map(book); + } + + public BookDTO update(Long id, BookUpdateDTO editedBook) { + var book = bookRepository.findById(id) + .orElseThrow(() -> { + throw new ResourceNotFoundException("Book with id " + id + " not found"); + }); + bookMapper.update(editedBook, book); + bookRepository.save(book); + return bookMapper.map(book); + } + + public void delete(Long id) { + bookRepository.deleteById(id); + } // END } diff --git a/java-spring-ru/service-layer/src/test/java/exercise/controller/AuthorsControllerTest.java b/java-spring-ru/service-layer/src/test/java/exercise/controller/AuthorsControllerTest.java index 5346c51..8bcd7e9 100644 --- a/java-spring-ru/service-layer/src/test/java/exercise/controller/AuthorsControllerTest.java +++ b/java-spring-ru/service-layer/src/test/java/exercise/controller/AuthorsControllerTest.java @@ -156,9 +156,10 @@ public void testPartialUpdate() throws Exception { assertThat(author.getFirstName()).isEqualTo(dto.get("firstName")); } + @Test public void testDestroy() throws Exception { authorRepository.save(testAuthor); - var request = delete("/books/{id}", testAuthor.getId()); + var request = delete("/authors/{id}", testAuthor.getId()); mockMvc.perform(request) .andExpect(status().isOk()); diff --git a/java-spring-ru/service-layer/src/test/java/exercise/controller/BooksControllerTest.java b/java-spring-ru/service-layer/src/test/java/exercise/controller/BooksControllerTest.java index 7f5476c..518494b 100644 --- a/java-spring-ru/service-layer/src/test/java/exercise/controller/BooksControllerTest.java +++ b/java-spring-ru/service-layer/src/test/java/exercise/controller/BooksControllerTest.java @@ -170,6 +170,7 @@ public void testPartialUpdate() throws Exception { assertThat(task.getAuthor().getId()).isEqualTo(dto.get("authorId")); } + @Test public void testDestroy() throws Exception { bookRepository.save(testBook); var request = delete("/books/{id}", testBook.getId());