-
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.
SD/feat/#32 Added Controller, Service, Model, DTO classes. Added flyw…
…ay Book migration. Changed package name typo from chohort2d to cohort2d.
- Loading branch information
Showing
22 changed files
with
585 additions
and
31 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
13 changes: 0 additions & 13 deletions
13
...re/src/main/java/com/amigoscode/chohort2d/onlinebookstore/OnlineBookStoreApplication.java
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
...ook-store/src/main/java/com/amigoscode/chohort2d/onlinebookstore/book/BookController.java
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
...ore/src/main/java/com/amigoscode/cohort2d/onlinebookstore/OnlineBookStoreApplication.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,29 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore; | ||
|
||
import com.amigoscode.cohort2d.onlinebookstore.book.BookDemoData; | ||
import com.amigoscode.cohort2d.onlinebookstore.book.BookRepository; | ||
|
||
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 { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(OnlineBookStoreApplication.class, args); | ||
} | ||
|
||
@Bean | ||
public CommandLineRunner createBookDemoData(BookRepository bookRepository, BookDemoData bookDemoData) { | ||
return args -> { | ||
if (bookRepository.count() == 0) { | ||
|
||
bookRepository.saveAll(bookDemoData.generateData()); | ||
|
||
} | ||
}; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
online-book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/Book.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,60 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
|
||
@Data | ||
@Entity | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Book { | ||
|
||
@Id | ||
@SequenceGenerator( | ||
name = "book_id_seq", | ||
sequenceName = "book_id_seq", | ||
allocationSize = 1 | ||
) | ||
@GeneratedValue( | ||
strategy = GenerationType.SEQUENCE, | ||
generator = "book_id_seq" | ||
) | ||
@NotNull | ||
private Long id; | ||
|
||
|
||
@NotNull | ||
@Column(unique=true) | ||
private String isbn; | ||
|
||
@NotNull | ||
private String title; | ||
|
||
@NotNull | ||
private String description; | ||
|
||
@NotNull | ||
private BigDecimal price; | ||
|
||
@NotNull | ||
private Integer numberOfPages; | ||
|
||
@NotNull | ||
private Integer quantity; | ||
|
||
@NotNull | ||
@Column(columnDefinition = "LOCALDATE") | ||
private LocalDate publishDate; | ||
|
||
@NotNull | ||
@Enumerated(EnumType.STRING) | ||
private BookFormat bookFormat; | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/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,27 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("api/v1/books") | ||
public class BookController { | ||
|
||
private final BookService bookService; | ||
|
||
@GetMapping() | ||
public List<BookDTO> getBooks() { | ||
return bookService.getAllBooks(); | ||
} | ||
|
||
@GetMapping("{id}") | ||
private BookDTO getBookById(@PathVariable("id") int id){ | ||
return bookService.getBookById(id); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
online-book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/BookDAO.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 com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface BookDAO { | ||
|
||
List<Book> findAllBooks(); | ||
|
||
Optional<Book> findById(int id); | ||
} |
18 changes: 18 additions & 0 deletions
18
online-book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/BookDTO.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,18 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
|
||
public record BookDTO ( | ||
Long id, | ||
String isbn, | ||
String title, | ||
String description, | ||
BigDecimal price, | ||
Integer numberOfPages, | ||
Integer quantity, | ||
LocalDate publishDate, | ||
String bookFormat | ||
){ | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...-book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/BookDTOMapper.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,26 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
import java.util.List; | ||
|
||
@Mapper( | ||
componentModel = "spring" | ||
) | ||
public interface BookDTOMapper { | ||
|
||
BookDTOMapper INSTANCE = Mappers.getMapper( BookDTOMapper.class ); | ||
|
||
BookDTO modelToDTO(Book book); | ||
|
||
List<BookDTO> modelToDTO(Iterable<Book> books); | ||
|
||
Book dtoToModel(BookDTO bookDTO); | ||
|
||
List<Book> dtoToModel(Iterable<BookDTO> bookDTOS); | ||
|
||
} | ||
|
||
|
51 changes: 51 additions & 0 deletions
51
...e-book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/BookDemoData.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,51 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
import com.github.javafaker.Faker; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.ZoneId; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
@Component | ||
public class BookDemoData { | ||
|
||
private final Faker faker = new Faker(); | ||
|
||
public List<Book> generateData(){ | ||
int count = 20; | ||
|
||
List<Book> books = new ArrayList<>(); | ||
|
||
for (int i = 0; i < count; i++) { | ||
|
||
Book book = new Book(); | ||
|
||
book.setTitle(faker.book().title()); | ||
book.setIsbn(faker.code().isbn13()); | ||
book.setDescription(faker.book().publisher()); | ||
book.setPrice(new BigDecimal("19.99")); | ||
book.setNumberOfPages(faker.number().numberBetween(10, 1000)); | ||
book.setQuantity(faker.number().numberBetween(1, 250)); | ||
|
||
Date date = faker.date().birthday(); | ||
book.setPublishDate(date.toInstant() | ||
.atZone(ZoneId.systemDefault()) | ||
.toLocalDate()); | ||
|
||
|
||
// Alternate between Digital and physical formats when creating demo book entries | ||
if (i % 2 == 0) { | ||
book.setBookFormat(BookFormat.DIGITAL); | ||
} else { | ||
book.setBookFormat(BookFormat.PHYSICAL); | ||
} | ||
|
||
books.add(book); | ||
} | ||
return books; | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
online-book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/BookFormat.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,6 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
public enum BookFormat { | ||
DIGITAL, | ||
PHYSICAL | ||
} |
25 changes: 25 additions & 0 deletions
25
...book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/BookJPAService.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,25 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
import com.amigoscode.cohort2d.onlinebookstore.exceptions.ResourceNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RequiredArgsConstructor | ||
@Repository("jpa") | ||
public class BookJPAService implements BookDAO { | ||
|
||
private final BookRepository bookRepository; | ||
|
||
@Override | ||
public List<Book> findAllBooks() { | ||
return bookRepository.findAll(); | ||
} | ||
|
||
@Override | ||
public Optional<Book> findById(int id) throws ResourceNotFoundException { | ||
return bookRepository.findById(id); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BookRepository extends JpaRepository<Book, Integer> { | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...ne-book-store/src/main/java/com/amigoscode/cohort2d/onlinebookstore/book/BookService.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,29 @@ | ||
package com.amigoscode.cohort2d.onlinebookstore.book; | ||
|
||
import com.amigoscode.cohort2d.onlinebookstore.exceptions.ResourceNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Qualifier(value = "jpa") | ||
public class BookService { | ||
|
||
private final BookDAO bookDAO; | ||
private final BookDTOMapper bookDTOMapper; | ||
|
||
public List<BookDTO> getAllBooks() { | ||
return bookDTOMapper.modelToDTO(bookDAO.findAllBooks()); | ||
} | ||
|
||
public BookDTO getBookById(int id){ | ||
return bookDTOMapper.modelToDTO( | ||
bookDAO.findById(id) | ||
.orElseThrow( | ||
() -> new ResourceNotFoundException("Book with id [%s] not found".formatted(id)) | ||
)); | ||
} | ||
} |
Oops, something went wrong.