-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ChabVlad/hw1_dto
Hw1 dto
- Loading branch information
Showing
12 changed files
with
195 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package project.bookstore.config; | ||
|
||
import org.mapstruct.InjectionStrategy; | ||
import org.mapstruct.NullValueCheckStrategy; | ||
|
||
@org.mapstruct.MapperConfig( | ||
componentModel = "spring", | ||
injectionStrategy = InjectionStrategy.CONSTRUCTOR, | ||
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, | ||
implementationPackage = "<PACKAGE_NAME>.impl" | ||
) | ||
public class MapperConfig { | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/project/bookstore/controller/BookController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package project.bookstore.controller; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
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; | ||
import project.bookstore.dto.BookDto; | ||
import project.bookstore.dto.CreateBookRequestDto; | ||
import project.bookstore.mapper.BookMapper; | ||
import project.bookstore.service.BookService; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/books") | ||
public class BookController { | ||
private final BookService bookService; | ||
private final BookMapper bookMapper; | ||
|
||
@GetMapping | ||
public List<BookDto> getAll() { | ||
return bookService.findAll(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public BookDto getBookById(@PathVariable Long id) { | ||
return bookService.getBookById(id); | ||
} | ||
|
||
@PostMapping | ||
public BookDto createBook(@RequestBody CreateBookRequestDto requestBookDto) { | ||
return bookMapper.toDto(bookService.save(requestBookDto)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package project.bookstore.dto; | ||
|
||
import java.math.BigDecimal; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class BookDto { | ||
private String title; | ||
private String author; | ||
private String isbn; | ||
private BigDecimal price; | ||
private String description; | ||
private String coverImage; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/project/bookstore/dto/CreateBookRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package project.bookstore.dto; | ||
|
||
import java.math.BigDecimal; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class CreateBookRequestDto { | ||
private String title; | ||
private String author; | ||
private String isbn; | ||
private BigDecimal price; | ||
private String description; | ||
private String coverImage; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/project/bookstore/exception/DataProcessingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package project.bookstore.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/project/bookstore/exception/EntityNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package project.bookstore.exception; | ||
|
||
public class EntityNotFoundException extends RuntimeException { | ||
public EntityNotFoundException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public EntityNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package project.bookstore.mapper; | ||
|
||
import org.mapstruct.Mapper; | ||
import project.bookstore.config.MapperConfig; | ||
import project.bookstore.dto.BookDto; | ||
import project.bookstore.dto.CreateBookRequestDto; | ||
import project.bookstore.model.Book; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface BookMapper { | ||
public BookDto toDto(Book book); | ||
|
||
public Book toModel(CreateBookRequestDto requestDto); | ||
} |
5 changes: 4 additions & 1 deletion
5
src/main/java/project/bookstore/repository/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package project.bookstore.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import project.bookstore.model.Book; | ||
|
||
public interface BookRepository { | ||
Book save(Book book); | ||
|
||
List findAll(); | ||
List<Book> findAll(); | ||
|
||
Optional<Book> findBookById(Long id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package project.bookstore.service; | ||
|
||
import java.util.List; | ||
import project.bookstore.dto.BookDto; | ||
import project.bookstore.dto.CreateBookRequestDto; | ||
import project.bookstore.model.Book; | ||
|
||
public interface BookService { | ||
Book save(Book book); | ||
Book save(CreateBookRequestDto requestBookDto); | ||
|
||
List findAll(); | ||
List<BookDto> findAll(); | ||
|
||
BookDto getBookById(Long id); | ||
} |
22 changes: 17 additions & 5 deletions
22
src/main/java/project/bookstore/service/impl/BookServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
package project.bookstore.service.impl; | ||
|
||
import java.util.List; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import project.bookstore.dto.BookDto; | ||
import project.bookstore.dto.CreateBookRequestDto; | ||
import project.bookstore.exception.EntityNotFoundException; | ||
import project.bookstore.mapper.BookMapper; | ||
import project.bookstore.model.Book; | ||
import project.bookstore.repository.BookRepository; | ||
import project.bookstore.service.BookService; | ||
|
||
@Service | ||
public class BookServiceImpl implements BookService { | ||
@Autowired | ||
private BookRepository bookRepository; | ||
private BookMapper bookMapper; | ||
|
||
@Override | ||
public Book save(Book book) { | ||
public Book save(CreateBookRequestDto requestBookDto) { | ||
Book book = bookMapper.toModel(requestBookDto); | ||
return bookRepository.save(book); | ||
} | ||
|
||
@Override | ||
public List findAll() { | ||
return bookRepository.findAll(); | ||
public List<BookDto> findAll() { | ||
return bookRepository.findAll().stream() | ||
.map(bookMapper::toDto) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public BookDto getBookById(Long id) { | ||
return bookMapper.toDto(bookRepository.findBookById(id).orElseThrow( | ||
() -> new EntityNotFoundException("Book not found by id: " + id))); | ||
} | ||
} |