-
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 #2 from TarasYashchuk/dev
implemented dto and service
- Loading branch information
Showing
10 changed files
with
208 additions
and
35 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 |
---|---|---|
@@ -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); | ||
}; | ||
} | ||
} |
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 mate.academy.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 { | ||
} |
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,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<BookDto> 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); | ||
} | ||
} |
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,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; | ||
} |
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 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; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/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,7 @@ | ||
package mate.academy.exception; | ||
|
||
public class EntityNotFoundException extends RuntimeException { | ||
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,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); | ||
} |
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,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<BookDto> getAll() { | ||
return bookRepository.findAll().stream() | ||
.map(bookMapper::toDto) | ||
.toList(); | ||
} | ||
|
||
public List<Book> 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); | ||
} | ||
} |
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