-
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 yaaariiik/wip-book-entity
Was added: BookDto classes, Mapper with config class, BookController, new exception. Was updated: BookRepository, BookService
- Loading branch information
Showing
14 changed files
with
181 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,37 +1,13 @@ | ||
package book.store; | ||
|
||
import book.store.model.Book; | ||
import book.store.service.BookService; | ||
import java.math.BigDecimal; | ||
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 OnlineBookStoreApplication { | ||
@Autowired | ||
private BookService bookService; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(OnlineBookStoreApplication.class, args); | ||
} | ||
|
||
@Bean | ||
public CommandLineRunner commandLineRunner() { | ||
return new CommandLineRunner() { | ||
@Override | ||
public void run(String... args) throws Exception { | ||
Book theFountainhead = new Book(); | ||
theFountainhead.setTitle("Джерело"); | ||
theFountainhead.setAuthor("Айн Ренд"); | ||
theFountainhead.setIsbn("9786177279555"); | ||
theFountainhead.setPrice(BigDecimal.valueOf(396)); | ||
|
||
bookService.save(theFountainhead); | ||
System.out.println(bookService.findAll()); | ||
} | ||
}; | ||
} | ||
} |
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 book.store.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 book.store.controller; | ||
|
||
import book.store.dto.book.BookDto; | ||
import book.store.dto.book.CreateBookRequestDto; | ||
import book.store.service.BookService; | ||
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; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping(value = "/books") | ||
public class BookController { | ||
private final BookService bookService; | ||
|
||
@GetMapping | ||
public List<BookDto> getAll() { | ||
return bookService.findAll(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public BookDto getBookById(@PathVariable Long id) { | ||
return bookService.findById(id); | ||
} | ||
|
||
@PostMapping | ||
public BookDto createBook(@RequestBody CreateBookRequestDto bookDto) { | ||
return bookService.save(bookDto); | ||
} | ||
} |
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 book.store.dto.book; | ||
|
||
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; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/book/store/dto/book/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,14 @@ | ||
package book.store.dto.book; | ||
|
||
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/book/store/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 book.store.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,14 @@ | ||
package book.store.mapper; | ||
|
||
import book.store.config.MapperConfig; | ||
import book.store.dto.book.BookDto; | ||
import book.store.dto.book.CreateBookRequestDto; | ||
import book.store.model.Book; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface BookMapper { | ||
BookDto toDto(Book book); | ||
|
||
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
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,13 @@ | ||
package book.store.service; | ||
|
||
import book.store.model.Book; | ||
import book.store.dto.book.BookDto; | ||
import book.store.dto.book.CreateBookRequestDto; | ||
import java.util.List; | ||
|
||
public interface BookService { | ||
Book save(Book book); | ||
BookDto save(CreateBookRequestDto requestDto); | ||
|
||
List<Book> findAll(); | ||
List<BookDto> findAll(); | ||
|
||
BookDto findById(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
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