Compare commits
73 Commits
main
...
d0c7514624
| Author | SHA1 | Date | |
|---|---|---|---|
| d0c7514624 | |||
| fd9863097c | |||
| c582b54a42 | |||
| a828b0db95 | |||
| 4511e4e961 | |||
| 889f17d806 | |||
| 8fc037e4b1 | |||
| 0d2a88f4a0 | |||
| a5c26c62e3 | |||
| 1ac8896253 | |||
| 4d7079c585 | |||
| 5b7e413a44 | |||
| f0704e4039 | |||
| 7ba85a0ae8 | |||
| 9bdccbb954 | |||
| 01e92154e7 | |||
| 42b0073297 | |||
| aa40ad0e3b | |||
| 76b60297c1 | |||
| 3ace5810f8 | |||
| 28a923c126 | |||
| a93ea4fc31 | |||
| 45d4016960 | |||
| 7c2eb19e13 | |||
| 49ddb77fb2 | |||
| 3deb17a4c7 | |||
| 655fad8fb2 | |||
| 5adb10101f | |||
| 2c33798c83 | |||
| 9e69e02066 | |||
| b2969260c0 | |||
| 2a6db59950 | |||
| 5d5945b48b | |||
| da19dfcbac | |||
| 13f8cab3ed | |||
| 1d8b4c0ac9 | |||
| 9ee039a32e | |||
| 051a8e03ed | |||
| 1d066a802f | |||
| a4808a28c9 | |||
| 90939326dc | |||
| 5cc3986307 | |||
| 5705081bbe | |||
| e11c5b744f | |||
| 90929b7bdd | |||
| 62b4a30add | |||
| 6e2bf9aab9 | |||
| 07b9357829 | |||
| 20fafa1a1c | |||
| 77d79de12a | |||
| 0c87631c85 | |||
| 50dd02423d | |||
| 6e3750e275 | |||
| 58e4a53196 | |||
| ee299e1e19 | |||
| 5196f30fd8 | |||
| 4d47cdd9a0 | |||
| 099cb403ca | |||
| 4c79ddcf6a | |||
| d8ae07a032 | |||
| 7d11743c02 | |||
| 182fcc0259 | |||
| adb6a885ae | |||
| aca81b68db | |||
| 495343ddb8 | |||
| 2b29d34615 | |||
| 961b3b69ba | |||
| 958f391b05 | |||
| e8563d44b1 | |||
| 55e43fca89 | |||
| c767f47719 | |||
| 370d35078f | |||
| 80c2b82c98 |
@@ -0,0 +1 @@
|
||||
Notre groupe est constitué de Marvin Aubert, Maxime Lebreton et de Patrick Felix Vimalaratnam
|
||||
@@ -14,10 +14,17 @@
|
||||
"langue": "string"
|
||||
},
|
||||
"output": {
|
||||
"_comment": "c'est ce qui affiche quand tu crée avec la usecase (regarde register de customer)",
|
||||
"isbn": "string(13)"
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
"PasserCommande": {
|
||||
"input": {
|
||||
"clientId": "uuid",
|
||||
@@ -39,6 +46,12 @@
|
||||
"pointsFideliteGagnes": "integer"
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
"GererAvis": {
|
||||
"input": {
|
||||
"clientId": "uuid",
|
||||
|
||||
@@ -117,6 +117,11 @@
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class BookDTO {
|
||||
private String isbn;
|
||||
private String title;
|
||||
private String author;
|
||||
private String editor;
|
||||
private LocalDate date;
|
||||
private double price;
|
||||
private Integer stock;
|
||||
private ArrayList<String> categories = new ArrayList<>();
|
||||
private String description ;
|
||||
private String language;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class BookDetails {
|
||||
private ArrayList<String> categories = new ArrayList<>();
|
||||
private String description ;
|
||||
private String language;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public record BookInfo(String isbn, String title, String author, String editor, LocalDate date) {
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class BookSalesInfo {
|
||||
private double price;
|
||||
private Integer stock;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.converter;
|
||||
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDetails;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||
|
||||
public final class BookConverter {
|
||||
private BookConverter(){}
|
||||
|
||||
public static Book ToDomain(BookInfo bookinfo, BookSalesInfo booksalesinfo, BookDetails bookdetails){
|
||||
return Book.builder()
|
||||
.isbn(bookinfo.isbn())
|
||||
.title(bookinfo.title())
|
||||
.author(bookinfo.author())
|
||||
.editor(bookinfo.editor())
|
||||
.date(bookinfo.date())
|
||||
.price(booksalesinfo.getPrice())
|
||||
.stock(booksalesinfo.getStock())
|
||||
.categories(bookdetails.getCategories())
|
||||
.description(bookdetails.getDescription())
|
||||
.language(bookdetails.getLanguage())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static BookDTO ToDTO(Book book){
|
||||
return BookDTO.builder()
|
||||
.isbn(book.getIsbn())
|
||||
.title(book.getTitle())
|
||||
.author(book.getAuthor())
|
||||
.editor(book.getEditor())
|
||||
.date(book.getDate())
|
||||
.price(book.getPrice())
|
||||
.stock(book.getStock())
|
||||
.categories(book.getCategories())
|
||||
.description(book.getDescription())
|
||||
.language(book.getLanguage())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
|
||||
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class Book {
|
||||
private String isbn;
|
||||
private String title;
|
||||
private String author;
|
||||
private String editor;
|
||||
private LocalDate date;
|
||||
private double price;
|
||||
private Integer stock;
|
||||
private ArrayList<String> categories = new ArrayList<>();
|
||||
private String description ;
|
||||
private String language;
|
||||
|
||||
public void addStock(Integer copyToAdd){
|
||||
this.stock += copyToAdd;
|
||||
}
|
||||
|
||||
public void removeStock(Integer copyToRemomve) throws IllegalBookStockException {
|
||||
if (copyToRemomve > this.stock){
|
||||
throw new IllegalBookStockException(copyToRemomve, this.stock);
|
||||
}
|
||||
this.stock -= copyToRemomve;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
public class BookNotFoundException extends RuntimeException {
|
||||
public static final String THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The book with isbn {0} does not exist";
|
||||
|
||||
public BookNotFoundException(String isbn) {
|
||||
super(MessageFormat.format(THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE, isbn));
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
public class IllegalBookStockException extends Exception {
|
||||
public static final String CANNOT_REMOVE_COPY = "Cannot remove {0} copy from {1} copy";
|
||||
|
||||
public IllegalBookStockException(Integer toremove, Integer actual ) {
|
||||
|
||||
super(MessageFormat.format(CANNOT_REMOVE_COPY, toremove,
|
||||
actual));
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||
|
||||
public class NotValidBookException extends RuntimeException {
|
||||
public NotValidBookException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BookRepository {
|
||||
|
||||
private final List<Book> books = new ArrayList<>();
|
||||
|
||||
public List<Book> findAll() {
|
||||
return books;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
books.clear();
|
||||
}
|
||||
|
||||
public Book save(Book newBook) {
|
||||
Optional<Book> optionalBookWithSameIsbn = this.findByIsbn(newBook.getIsbn());
|
||||
optionalBookWithSameIsbn.ifPresent(books::remove);
|
||||
this.books.add(newBook);
|
||||
return newBook;
|
||||
}
|
||||
|
||||
public Optional<Book> findByIsbn(String isbn) {
|
||||
return this.books.stream()
|
||||
.filter(book -> book.getIsbn().equals(isbn))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsByIsbn(String isbn) {
|
||||
return this.books.stream()
|
||||
.anyMatch(book -> book.getIsbn().equals(isbn));
|
||||
}
|
||||
|
||||
public void delete(Book book) {
|
||||
|
||||
this.books.remove(book);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDetails;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.converter.BookConverter;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.BookNotFoundException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.validator.BookValidator;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class BookUseCase {
|
||||
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
public BookUseCase(BookRepository bookRepository) {
|
||||
|
||||
this.bookRepository = bookRepository;
|
||||
}
|
||||
|
||||
public String registerBook(BookInfo newbook, BookSalesInfo newbooksalesinfo, BookDetails newbookdetails) throws NotValidBookException {
|
||||
BookValidator.validate(newbook);
|
||||
BookValidator.validate(newbooksalesinfo);
|
||||
BookValidator.validate(newbookdetails);
|
||||
Book bookToRegister = BookConverter.ToDomain(newbook, newbooksalesinfo, newbookdetails);
|
||||
Book bookToRegistered = bookRepository.save(bookToRegister);
|
||||
return bookToRegistered.getIsbn();
|
||||
}
|
||||
|
||||
public Optional<BookDTO> findBookByIsbn(String isbn) {
|
||||
Optional<Book> optionalBook = bookRepository.findByIsbn(isbn);
|
||||
return optionalBook.map(BookConverter::ToDTO);
|
||||
}
|
||||
|
||||
public BookDTO updateBook(String isbn, BookInfo bookinfo, BookSalesInfo booksalesinfo, BookDetails bookdetails)
|
||||
throws BookNotFoundException, NotValidBookException {
|
||||
BookValidator.validate(bookinfo);
|
||||
BookValidator.validate(booksalesinfo);
|
||||
BookValidator.validate(bookdetails);
|
||||
Book bookByIsbn = getBookIfDoesNotExistThrowBookNotFoundException(isbn);
|
||||
Book book = Book.builder()
|
||||
.isbn(isbn)
|
||||
.title(bookinfo.title())
|
||||
.author(bookinfo.author())
|
||||
.editor(bookinfo.editor())
|
||||
.date(bookByIsbn.getDate())
|
||||
.price(booksalesinfo.getPrice())
|
||||
.stock(booksalesinfo.getStock())
|
||||
.categories(bookdetails.getCategories())
|
||||
.description(bookdetails.getDescription())
|
||||
.language(bookByIsbn.getLanguage())
|
||||
.build();
|
||||
Book updatedBook = bookRepository.save(book);
|
||||
return BookConverter.ToDTO(updatedBook);
|
||||
}
|
||||
|
||||
public void deleteBook(String isbn) throws BookNotFoundException {
|
||||
Book bookToDelete = getBookIfDoesNotExistThrowBookNotFoundException(isbn);
|
||||
this.bookRepository.delete(bookToDelete);
|
||||
}
|
||||
|
||||
public int addStockCopies(String isbn, int stockCopiesToAdd) throws BookNotFoundException {
|
||||
Book bookToAddStockCopies = getBookIfDoesNotExistThrowBookNotFoundException(isbn);
|
||||
bookToAddStockCopies.addStock(stockCopiesToAdd);
|
||||
bookRepository.save(bookToAddStockCopies);
|
||||
return bookToAddStockCopies.getStock();
|
||||
}
|
||||
|
||||
public int subtractStockCopies(String isbn, int stockCopiesToRemove)
|
||||
throws BookNotFoundException, IllegalBookStockException {
|
||||
Book bookToSubtractStockCopies = getBookIfDoesNotExistThrowBookNotFoundException(isbn);
|
||||
bookToSubtractStockCopies.removeStock(stockCopiesToRemove);
|
||||
bookRepository.save(bookToSubtractStockCopies);
|
||||
return bookToSubtractStockCopies.getStock();
|
||||
}
|
||||
|
||||
private Book getBookIfDoesNotExistThrowBookNotFoundException(String isbn)
|
||||
throws BookNotFoundException {
|
||||
Optional<Book> optionalBookByIsbn = bookRepository.findByIsbn(isbn);
|
||||
if (optionalBookByIsbn.isEmpty()) {
|
||||
throw new BookNotFoundException(isbn);
|
||||
}
|
||||
return optionalBookByIsbn.get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDetails;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BookValidator {
|
||||
|
||||
public static final String ISBN_IS_NOT_VALID = "Isbn is not valid";
|
||||
public static final String TITLE_CANNOT_BE_BLANK = "Title cannot be blank";
|
||||
public static final String AUTHOR_CANNOT_BE_BLANK = "Author cannot be blank";
|
||||
public static final String EDITOR_CANNOT_BE_BLANK = "Editor cannot be blank";
|
||||
public static final String DATE_IS_NOT_VALID = "Date is not valid";
|
||||
public static final String PRICE_IS_NOT_VALID = "Price is not valid";
|
||||
public static final String STOCK_IS_NOT_VALID = "Stock is not valid";
|
||||
public static final String CATEGORIES_IS_NOT_VALID = "Categories is not valid";
|
||||
public static final String LANGUAGE_CANNOT_BE_BLANK = "Language cannot be blank";
|
||||
|
||||
private BookValidator() {
|
||||
}
|
||||
|
||||
public static void validate(BookInfo newBook) throws NotValidBookException {
|
||||
validateISBN(newBook);
|
||||
validateTitle(newBook);
|
||||
validateAuthor(newBook);
|
||||
validateEditor(newBook);
|
||||
validateDate(newBook);
|
||||
}
|
||||
|
||||
public static void validate(BookSalesInfo newBook) throws NotValidBookException {
|
||||
validatePrice(newBook);
|
||||
validateStock(newBook);
|
||||
}
|
||||
|
||||
public static void validate(BookDetails newBook) throws NotValidBookException {
|
||||
validateCategories(newBook);
|
||||
validateLanguage(newBook);
|
||||
}
|
||||
|
||||
private static void validateISBN(BookInfo newBook) throws NotValidBookException {
|
||||
if (newBook.isbn().isBlank()) {
|
||||
throw new NotValidBookException(ISBN_IS_NOT_VALID);
|
||||
} else if (newBook.isbn().length() != 13) {
|
||||
throw new NotValidBookException(ISBN_IS_NOT_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateTitle(BookInfo newBook) throws NotValidBookException {
|
||||
if (newBook.title().isBlank()) {
|
||||
throw new NotValidBookException(TITLE_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateAuthor(BookInfo newBook) throws NotValidBookException {
|
||||
if (newBook.author().isBlank()) {
|
||||
throw new NotValidBookException(AUTHOR_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateEditor(BookInfo newBook) throws NotValidBookException {
|
||||
if (newBook.editor().isBlank()) {
|
||||
throw new NotValidBookException(EDITOR_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateDate(BookInfo newBook) throws NotValidBookException {
|
||||
if (newBook.date().isAfter(LocalDate.now())) {
|
||||
throw new NotValidBookException(DATE_IS_NOT_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePrice(BookSalesInfo newBook) throws NotValidBookException {
|
||||
if (newBook.getPrice() <= 0) {
|
||||
throw new NotValidBookException(PRICE_IS_NOT_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateStock(BookSalesInfo newBook) throws NotValidBookException {
|
||||
if (newBook.getStock() < 0) {
|
||||
throw new NotValidBookException(STOCK_IS_NOT_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateCategories(BookDetails newBook) throws NotValidBookException {
|
||||
if (newBook.getCategories().isEmpty()) {
|
||||
throw new NotValidBookException(CATEGORIES_IS_NOT_VALID);
|
||||
}else if (CategoriesNotBlank(newBook.getCategories())) {
|
||||
throw new NotValidBookException(CATEGORIES_IS_NOT_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean CategoriesNotBlank(ArrayList<String> categories){
|
||||
for (String categorie : categories) {
|
||||
if (categorie.isBlank()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void validateLanguage(BookDetails newBook) throws NotValidBookException {
|
||||
if (newBook.getLanguage().isBlank()) {
|
||||
throw new NotValidBookException(LANGUAGE_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||
|
||||
public record AdresseInfo(String rue, String ville, String codePostal, String pays) {}
|
||||
@@ -0,0 +1,23 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class CommandeDTO {
|
||||
private UUID clientId;
|
||||
private List<LigneCommandeInfo> lignesCommande;
|
||||
private String rue;
|
||||
private String ville;
|
||||
private String codePostal;
|
||||
private String pays;
|
||||
private String modePaiement;
|
||||
private UUID commandeId ;
|
||||
private double montantTotal ;
|
||||
private Integer pointsFideliteGagnes ;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record CommandeInfo(List<LigneCommandeInfo> listeLigne , String modePayement) {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class LigneCommandeDTO {
|
||||
|
||||
int quantite;
|
||||
UUID id;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record LigneCommandeInfo(int quantite) {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||
|
||||
public enum ModePaiement {
|
||||
CB,
|
||||
PAYPAL,
|
||||
POINTS_FIDELITE,
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||
|
||||
public final class CommandeConverter {
|
||||
|
||||
|
||||
public static Commande toDomain(CommandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
|
||||
return Commande.builder()
|
||||
.lignesCommande(commandeInfo.listeLigne())
|
||||
.rue(adressInfo.rue())
|
||||
.ville(adressInfo.ville())
|
||||
.codePostal(adressInfo.codePostal())
|
||||
.pays(adressInfo.pays())
|
||||
.modePaiement(commandeInfo.modePayement())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static Commande toDomain(CommandeInfo commandeInfo, AdresseInfo adressInfo) {
|
||||
return Commande.builder()
|
||||
.lignesCommande(commandeInfo.listeLigne())
|
||||
.rue(adressInfo.rue())
|
||||
.ville(adressInfo.ville())
|
||||
.codePostal(adressInfo.codePostal())
|
||||
.pays(adressInfo.pays())
|
||||
.modePaiement(commandeInfo.modePayement())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static CommandeDTO toDTO(CommandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
|
||||
return CommandeDTO.builder()
|
||||
.lignesCommande(commandeInfo.listeLigne())
|
||||
.rue(adressInfo.rue())
|
||||
.ville(adressInfo.ville())
|
||||
.codePostal(adressInfo.codePostal())
|
||||
.pays(adressInfo.pays())
|
||||
.modePaiement(modePaiement)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static CommandeDTO toDTO(Commande commande) {
|
||||
return CommandeDTO.builder()
|
||||
.lignesCommande(commande.getLignesCommande())
|
||||
.rue(commande.getRue())
|
||||
.ville(commande.getVille())
|
||||
.codePostal(commande.getCodePostal())
|
||||
.pays(commande.getPays())
|
||||
.modePaiement(commande.getModePaiement())
|
||||
.commandeId(commande.getCommandeId())
|
||||
.clientId(commande.getClientId())
|
||||
.montantTotal(commande.getMontantTotal())
|
||||
.pointsFideliteGagnes(commande.getPointsFideliteGagnes())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.LigneCommande;
|
||||
|
||||
public final class LigneCommandeConverter {
|
||||
|
||||
public static LigneCommande toDomain(LigneCommandeInfo ligneCommandeInfo) {
|
||||
return LigneCommande.builder()
|
||||
.quantite(ligneCommandeInfo.quantite())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static LigneCommandeDTO toDTO(LigneCommandeInfo ligneCommandeInfo) {
|
||||
return LigneCommandeDTO.builder()
|
||||
.quantite(ligneCommandeInfo.quantite())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class Commande {
|
||||
private UUID clientId;
|
||||
private List<LigneCommandeInfo> lignesCommande;
|
||||
private String rue;
|
||||
private String ville;
|
||||
private String codePostal;
|
||||
private String pays;
|
||||
private String modePaiement;
|
||||
|
||||
private UUID commandeId ;
|
||||
private double montantTotal ;
|
||||
private Integer pointsFideliteGagnes ;
|
||||
|
||||
public void setRandomUUID() {
|
||||
this.clientId = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public void setRandomUUIDCommande() {
|
||||
this.commandeId = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public int convertPointsFidelite(){
|
||||
if (this.montantTotal==0|| this.montantTotal<=0){
|
||||
return 0;
|
||||
}
|
||||
return (int) Math.ceil(this.getMontantTotal());
|
||||
}
|
||||
|
||||
|
||||
// conversion point de fidéliter
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class LigneCommande {
|
||||
|
||||
int quantite;
|
||||
UUID livreId;
|
||||
|
||||
public void setRandomUUID() {
|
||||
this.livreId = UUID.randomUUID();
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CommandeNotFoundException extends RuntimeException {
|
||||
|
||||
public static final String THE_COMMANDE_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The commande with id {0} does not exist";
|
||||
|
||||
public CommandeNotFoundException(UUID uuid) {
|
||||
super(MessageFormat.format(THE_COMMANDE_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
|
||||
|
||||
public class NotValidAdressException extends RuntimeException {
|
||||
public NotValidAdressException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
|
||||
|
||||
public class NotValidCommandeException extends RuntimeException {
|
||||
public NotValidCommandeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
|
||||
|
||||
public class NotValidLigneCommandeException extends RuntimeException {
|
||||
public NotValidLigneCommandeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CommandeRepository {
|
||||
|
||||
private final List<Commande> commande = new ArrayList<>();
|
||||
|
||||
public List<Commande> findAll(){return commande;}
|
||||
|
||||
|
||||
public void deleteAll() {
|
||||
commande.clear();
|
||||
}
|
||||
|
||||
public Commande save(Commande newCommande){
|
||||
Optional<Commande> optionalCommandeWithSameIsbn = this.findById(newCommande.getCommandeId());
|
||||
optionalCommandeWithSameIsbn.ifPresent(commande::remove);
|
||||
this.commande.add(newCommande);
|
||||
return newCommande;
|
||||
}
|
||||
|
||||
public Optional<Commande> findById(UUID uuid){
|
||||
return this.commande.stream()
|
||||
.filter(customer -> customer.getCommandeId().equals(uuid))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsById(UUID uuid) {
|
||||
return this.commande.stream()
|
||||
.anyMatch(customer -> customer.getCommandeId().equals(uuid));
|
||||
}
|
||||
|
||||
public void delete(Commande commande) {
|
||||
this.commande.remove(commande);
|
||||
}
|
||||
|
||||
|
||||
public void deleteById(UUID uuid) {
|
||||
this.commande.removeIf(commande -> commande.getClientId().equals(uuid));
|
||||
}
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.converter.CommandeConverter;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.CommandeNotFoundException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.CommandeRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.validator.CommandeValidator;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CommandeUseCase {
|
||||
|
||||
private final CommandeRepository commandeRepository;
|
||||
|
||||
public CommandeUseCase(CommandeRepository commandeRepository) {
|
||||
this.commandeRepository = commandeRepository;
|
||||
}
|
||||
|
||||
|
||||
public UUID registerCommande(CommandeInfo validCommandeInfo, AdresseInfo validAdresseInfo){
|
||||
CommandeValidator.validate(validCommandeInfo);
|
||||
CommandeValidator.validate(validAdresseInfo);
|
||||
|
||||
Commande commandeToRegister = CommandeConverter.toDomain(validCommandeInfo,validAdresseInfo);
|
||||
Commande commandeToRegistered = commandeRepository.save(commandeToRegister) ;
|
||||
return commandeToRegistered.getCommandeId();
|
||||
|
||||
}
|
||||
|
||||
public Optional<CommandeDTO> findCommandByUUID(UUID uuid) {
|
||||
Optional<Commande> optionalCommande = commandeRepository.findById(uuid);
|
||||
return optionalCommande.map(CommandeConverter::toDTO);
|
||||
}
|
||||
|
||||
public CommandeDTO updateCommande(UUID uuid, CommandeInfo commandeInfo, AdresseInfo adresseInfo)
|
||||
throws CommandeNotFoundException, NotValidCommandeException {
|
||||
CommandeValidator.validate(commandeInfo);
|
||||
CommandeValidator.validate(adresseInfo);
|
||||
Commande commandeByUUID = getCommandeIfDoesNotExistThrowCommandeNotFoundException(
|
||||
uuid);
|
||||
Commande commande = Commande.builder()
|
||||
.clientId(commandeByUUID.getClientId())
|
||||
.lignesCommande(commandeInfo.listeLigne())
|
||||
.rue(adresseInfo.rue())
|
||||
.ville(adresseInfo.ville())
|
||||
.codePostal(adresseInfo.codePostal())
|
||||
.pays(adresseInfo.pays())
|
||||
.modePaiement(commandeInfo.modePayement())
|
||||
.commandeId(uuid)
|
||||
.build();
|
||||
Commande updatedCommande = commandeRepository.save(commande);
|
||||
return CommandeConverter.toDTO(updatedCommande);
|
||||
}
|
||||
|
||||
public void deleteCommande(UUID uuid) throws CommandeNotFoundException {
|
||||
Commande commandeToDelete = getCommandeIfDoesNotExistThrowCommandeNotFoundException(uuid);
|
||||
this.commandeRepository.delete(commandeToDelete);
|
||||
}
|
||||
|
||||
private Commande getCommandeIfDoesNotExistThrowCommandeNotFoundException(UUID uuid)
|
||||
throws CommandeNotFoundException {
|
||||
Optional<Commande> optionalCommandeById = commandeRepository.findById(uuid);
|
||||
if (optionalCommandeById.isEmpty()) {
|
||||
throw new CommandeNotFoundException(uuid);
|
||||
}
|
||||
return optionalCommandeById.get();
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class CommandeValidator {
|
||||
|
||||
public static final String LIGNE_COMMANDE_IS_NOT_VALIDE = "Ligne commande is not valide";
|
||||
public static final String MODE_PAIEMENT_IS_NOT_VALIDE = "Mode Paiement is not valide";
|
||||
public static final String LIST_LIGNE_COMANDE_IS_NOT_VALIDE = "List ligne commande is not valide";
|
||||
public static final String STREET_IS_NOT_VALIDE = "rue is not valide";
|
||||
|
||||
public static final String CITY_IS_NOT_VALIDE = "city is not valide";
|
||||
public static final String CODE_POSTAL_IS_NOT_VALIDE = "code postal is not valide";
|
||||
public static final String COUNTRY_IS_NOT_VALIDE = "Country is not valide";
|
||||
|
||||
|
||||
public static void validate(LigneCommandeInfo ligneCommandeInfo) {
|
||||
if (ligneCommandeInfo.quantite()<=0){
|
||||
throw new NotValidLigneCommandeException(LIGNE_COMMANDE_IS_NOT_VALIDE);
|
||||
}
|
||||
}
|
||||
|
||||
public static void validate(CommandeInfo commandeInfo) {
|
||||
|
||||
validateModePaiement(commandeInfo);
|
||||
validateListLigneCommande(commandeInfo);
|
||||
}
|
||||
|
||||
public static void validate(AdresseInfo adresseInfo) {
|
||||
validateStreet(adresseInfo);
|
||||
validateCity(adresseInfo);
|
||||
validateCodePostal(adresseInfo);
|
||||
validateCountry(adresseInfo);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void validateModePaiement(CommandeInfo commandeInfo) {
|
||||
if(commandeInfo.modePayement()!="CB"&& commandeInfo.modePayement()!="PAYPAL"&& commandeInfo.modePayement()!="POINTS_FIDELITE"){
|
||||
throw new NotValidCommandeException(MODE_PAIEMENT_IS_NOT_VALIDE);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateListLigneCommande(CommandeInfo commandeInfo) {
|
||||
if (commandeInfo.listeLigne() == null
|
||||
|| commandeInfo.listeLigne().isEmpty()
|
||||
|| commandeInfo.listeLigne().stream().anyMatch(Objects::isNull)){
|
||||
throw new NotValidCommandeException(LIST_LIGNE_COMANDE_IS_NOT_VALIDE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void validateStreet(AdresseInfo adresseInfo) {
|
||||
if (adresseInfo.rue() == null || adresseInfo.rue().isBlank()){
|
||||
throw new NotValidAdressException(STREET_IS_NOT_VALIDE);
|
||||
}
|
||||
}
|
||||
private static void validateCity(AdresseInfo adresseInfo) {
|
||||
if (adresseInfo.ville() == null || adresseInfo.ville().isBlank()){
|
||||
throw new NotValidAdressException(CITY_IS_NOT_VALIDE);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateCodePostal(AdresseInfo adresseInfo) {
|
||||
if (adresseInfo.codePostal() == null || adresseInfo.codePostal().isBlank()){
|
||||
throw new NotValidAdressException(CODE_POSTAL_IS_NOT_VALIDE);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateCountry(AdresseInfo adresseInfo) {
|
||||
if (adresseInfo.pays() == null || adresseInfo.pays().isBlank()){
|
||||
throw new NotValidAdressException(COUNTRY_IS_NOT_VALIDE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
|
||||
public class ReviewDTO {
|
||||
private UUID reviewId;
|
||||
private UUID customerId;
|
||||
private UUID bookId;
|
||||
private Integer note;
|
||||
private String comment;
|
||||
private LocalDate purchaseDate;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
public record ReviewInfo(Integer note, String comment, LocalDate purchaseDate) {
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
|
||||
|
||||
public class ReviewConverter {
|
||||
private ReviewConverter() {
|
||||
|
||||
}
|
||||
|
||||
public static Review toDomain(ReviewInfo newReview) {
|
||||
return Review.builder()
|
||||
.note(newReview.note())
|
||||
.comment(newReview.comment())
|
||||
.purchaseDate(newReview.purchaseDate())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static ReviewDTO toDTO(Review review) {
|
||||
return ReviewDTO.builder()
|
||||
.reviewId(review.getReviewId())
|
||||
.customerId(review.getCustomerId())
|
||||
.bookId(review.getBookId())
|
||||
.note(review.getNote())
|
||||
.comment(review.getComment())
|
||||
.purchaseDate(review.getPurchaseDate())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.entity;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
|
||||
public class Review {
|
||||
private UUID reviewId;
|
||||
private UUID customerId;
|
||||
private UUID bookId;
|
||||
private Integer note;
|
||||
private String comment;
|
||||
private LocalDate purchaseDate;
|
||||
|
||||
public void setRandomUUID() {
|
||||
this.reviewId = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public void setRandomUUIDCustomerAndBook() {
|
||||
|
||||
this.customerId = UUID.randomUUID();
|
||||
this.bookId = UUID.randomUUID();
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
|
||||
|
||||
public class NotValidReviewException extends RuntimeException {
|
||||
public NotValidReviewException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ReviewNotFoundException extends RuntimeException {
|
||||
|
||||
public static final String THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the customer id {0} does not exists";
|
||||
public static final String THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the book id {0} does not exists";
|
||||
public static final String THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE = "The review with review id {0} does not exists";
|
||||
|
||||
public ReviewNotFoundException(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> reviewUUID) {
|
||||
super(buildMessage(customerUUID, bookUUID, reviewUUID));
|
||||
}
|
||||
|
||||
private static String buildMessage(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> reviewUUID) {
|
||||
if (customerUUID.isPresent()) {
|
||||
return MessageFormat.format(THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE, customerUUID.get());
|
||||
}else if (bookUUID.isPresent()) {
|
||||
return MessageFormat.format(THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE, bookUUID.get());
|
||||
}
|
||||
return MessageFormat.format(THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE, reviewUUID.get());
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ReviewRepository {
|
||||
|
||||
private final List<Review> reviews = new ArrayList<>();
|
||||
|
||||
public List<Review> findAll() {
|
||||
|
||||
return reviews;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
|
||||
reviews.clear();
|
||||
}
|
||||
|
||||
public Review save(Review newReview) {
|
||||
Optional<Review> optionalReviewWithSameReviewId = this.findByReviewId(newReview.getReviewId());
|
||||
optionalReviewWithSameReviewId.ifPresent(reviews::remove);
|
||||
this.reviews.add(newReview);
|
||||
return newReview;
|
||||
}
|
||||
|
||||
public ArrayList<Review> findByCustomerId(UUID customerUUID) {
|
||||
return this.reviews.stream()
|
||||
.filter(review -> review.getCustomerId().equals(customerUUID))
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
public ArrayList<Review> findByBookId(UUID bookUUID) {
|
||||
return this.reviews.stream()
|
||||
.filter(review -> review.getBookId().equals(bookUUID))
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
public Optional<Review> findByReviewId(UUID reviewUUID) {
|
||||
return this.reviews.stream()
|
||||
.filter(review -> review.getReviewId().equals(reviewUUID))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsByCustomerId(UUID customerUUID) {
|
||||
return this.reviews.stream()
|
||||
.anyMatch(review -> review.getCustomerId().equals(customerUUID));
|
||||
}
|
||||
|
||||
public boolean existsByBookId(UUID bookUUID) {
|
||||
return this.reviews.stream()
|
||||
.anyMatch(review -> review.getBookId().equals(bookUUID));
|
||||
}
|
||||
|
||||
public boolean existsByReviewId(UUID reviewUUID) {
|
||||
return this.reviews.stream()
|
||||
.anyMatch(review -> review.getReviewId().equals(reviewUUID));
|
||||
}
|
||||
|
||||
public void deleteCustomerReviews(UUID customerUUID) {
|
||||
|
||||
this.reviews.removeIf(review -> review.getCustomerId().equals(customerUUID));
|
||||
}
|
||||
|
||||
public void deleteBookReviews(UUID bookUUID) {
|
||||
|
||||
this.reviews.removeIf(review -> review.getBookId().equals(bookUUID));
|
||||
}
|
||||
|
||||
public void delete(Review review) {
|
||||
|
||||
this.reviews.remove(review);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.converter.ReviewConverter;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.ReviewNotFoundException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.validator.ReviewValidator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ReviewUseCase {
|
||||
|
||||
private final ReviewRepository reviewRepository;
|
||||
|
||||
public ReviewUseCase(ReviewRepository reviewRepository) {
|
||||
this.reviewRepository = reviewRepository;
|
||||
}
|
||||
|
||||
public UUID registerReview(ReviewInfo newReview) throws NotValidReviewException {
|
||||
ReviewValidator.validate(newReview);
|
||||
Review reviewToRegister = ReviewConverter.toDomain(newReview);
|
||||
Review reviewToRegistered = reviewRepository.save(reviewToRegister);
|
||||
return reviewToRegistered.getReviewId();
|
||||
}
|
||||
|
||||
public ArrayList<ReviewDTO> findReviewByCustomerId(UUID customerId) {
|
||||
ArrayList<Review> optionalReviews = reviewRepository.findByCustomerId(customerId);
|
||||
return optionalReviews.stream()
|
||||
.map(ReviewConverter::toDTO)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
public ArrayList<ReviewDTO> findReviewByBookId(UUID bookId) {
|
||||
ArrayList<Review> optionalReviews = reviewRepository.findByBookId(bookId);
|
||||
return optionalReviews.stream()
|
||||
.map(ReviewConverter::toDTO)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
public Optional<ReviewDTO> findReviewByReviewId(UUID reviewId) {
|
||||
Optional<Review> optionalReview = reviewRepository.findByReviewId(reviewId);
|
||||
return optionalReview.map(ReviewConverter::toDTO);
|
||||
}
|
||||
|
||||
public ReviewDTO updateReview(UUID reviewUUID, ReviewInfo reviewInfo)
|
||||
throws ReviewNotFoundException, NotValidReviewException {
|
||||
ReviewValidator.validate(reviewInfo);
|
||||
Review reviewByReviewUUID = getReviewIfDoesNotExistThrowReviewNotFoundException(
|
||||
reviewUUID);
|
||||
Review review = Review.builder()
|
||||
.reviewId(reviewUUID)
|
||||
.customerId(reviewByReviewUUID.getCustomerId())
|
||||
.bookId(reviewByReviewUUID.getBookId())
|
||||
.note(reviewByReviewUUID.getNote())
|
||||
.comment(reviewByReviewUUID.getComment())
|
||||
.purchaseDate(reviewByReviewUUID.getPurchaseDate())
|
||||
.build();
|
||||
Review updatedReview = reviewRepository.save(review);
|
||||
return ReviewConverter.toDTO(updatedReview);
|
||||
}
|
||||
|
||||
public void deleteReview(UUID reviewUUID) throws ReviewNotFoundException {
|
||||
Review reviewToDelete = getReviewIfDoesNotExistThrowReviewNotFoundException(reviewUUID);
|
||||
this.reviewRepository.delete(reviewToDelete);
|
||||
}
|
||||
|
||||
public void deleteCustomerReviews(UUID customerUUID) throws ReviewNotFoundException {
|
||||
ArrayList<Review> reviewsToDelete = getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(customerUUID);
|
||||
for (Review review : reviewsToDelete) {
|
||||
reviewRepository.delete(review);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteBookReviews(UUID bookUUID) throws ReviewNotFoundException {
|
||||
ArrayList<Review> reviewsToDelete = getReviewByBookIfDoesNotExistThrowReviewNotFoundException(bookUUID);
|
||||
for (Review review : reviewsToDelete) {
|
||||
reviewRepository.delete(review);
|
||||
}
|
||||
}
|
||||
|
||||
private Review getReviewIfDoesNotExistThrowReviewNotFoundException(UUID reviewUUID)
|
||||
throws ReviewNotFoundException {
|
||||
Optional<Review> optionalReviewByReviewId = reviewRepository.findByReviewId(reviewUUID);
|
||||
if (optionalReviewByReviewId.isEmpty()) {
|
||||
throw new ReviewNotFoundException(Optional.empty(), Optional.empty(),Optional.of(reviewUUID));
|
||||
}
|
||||
return optionalReviewByReviewId.get();
|
||||
}
|
||||
|
||||
private ArrayList<Review> getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(UUID customerUUID)
|
||||
throws ReviewNotFoundException {
|
||||
ArrayList<Review> optionalReviewByReviewId = reviewRepository.findByCustomerId(customerUUID);
|
||||
if (optionalReviewByReviewId.isEmpty()) {
|
||||
throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(),Optional.empty());
|
||||
}
|
||||
return optionalReviewByReviewId;
|
||||
}
|
||||
|
||||
private ArrayList<Review> getReviewByBookIfDoesNotExistThrowReviewNotFoundException(UUID bookUUID)
|
||||
throws ReviewNotFoundException {
|
||||
ArrayList<Review> optionalReviewByReviewId = reviewRepository.findByBookId(bookUUID);
|
||||
if (optionalReviewByReviewId.isEmpty()) {
|
||||
throw new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(bookUUID));
|
||||
}
|
||||
return optionalReviewByReviewId;
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class ReviewValidator {
|
||||
|
||||
public static final String NOTE_CANNOT_BE_LOWER_THAN_1 = "Note is greater than or equal to 1";
|
||||
public static final String NOTE_CANNOT_BE_UPPER_THAN_5 = "Note is less than or equal to 5";
|
||||
public static final String COMMENT_CANNOT_BE_BLANK = "Comment cannot be blank";
|
||||
public static final String PURCHASE_DATE_IS_NOT_VALID = "Date is not valid";
|
||||
|
||||
public ReviewValidator() {
|
||||
|
||||
}
|
||||
|
||||
public static void validate(ReviewInfo newReview) throws NotValidReviewException {
|
||||
validateNoteLower1(newReview);
|
||||
validateNoteUpper5(newReview);
|
||||
validateComment(newReview);
|
||||
validatePurchaseDate(newReview);
|
||||
}
|
||||
|
||||
private static void validateNoteLower1(ReviewInfo newReview)
|
||||
throws NotValidReviewException {
|
||||
if (newReview.note() <= 1) {
|
||||
throw new NotValidReviewException(NOTE_CANNOT_BE_LOWER_THAN_1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateNoteUpper5(ReviewInfo newReview)
|
||||
throws NotValidReviewException {
|
||||
if (newReview.note() >= 5) {
|
||||
throw new NotValidReviewException("Note is less than or equal to 5");
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateComment(ReviewInfo newReview) throws NotValidReviewException {
|
||||
if (newReview.comment().isBlank()) {
|
||||
throw new NotValidReviewException(COMMENT_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePurchaseDate(ReviewInfo newReview) throws NotValidReviewException {
|
||||
if (newReview.purchaseDate().isAfter(LocalDate.now())) {
|
||||
throw new NotValidReviewException(PURCHASE_DATE_IS_NOT_VALID);
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
public enum DesiredSubscriptionDuration {
|
||||
THREE(3),
|
||||
SIX(6),
|
||||
TWELVE(12);
|
||||
|
||||
private final Integer value;
|
||||
|
||||
DesiredSubscriptionDuration(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
public record PaymentMethodInfo(String paymentType, Object details) {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
public enum PaymentType {
|
||||
CB,
|
||||
PAYPAL;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.PaymentMethod;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class SubscriptionDTO {
|
||||
|
||||
private UUID subscriptionId;
|
||||
private UUID customerId;
|
||||
private Integer desiredSubscriptionDuration;
|
||||
private PaymentMethod paymentMethod;
|
||||
private LocalDate desiredStartDate;
|
||||
private LocalDate startDate;
|
||||
private LocalDate endDate;
|
||||
private double monthlyAmount;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public record SubscriptionInfo(Integer desiredSubscriptionDuration, LocalDate desiredStartDate) {
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentMethodInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.PaymentMethod;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||
|
||||
public class SubscriptionConverter {
|
||||
|
||||
private SubscriptionConverter(){
|
||||
|
||||
}
|
||||
|
||||
public static Subscription toDomain(SubscriptionInfo newSubscription, PaymentMethodInfo newPaymentMethod) {
|
||||
PaymentMethod paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(newPaymentMethod.paymentType())
|
||||
.details(newPaymentMethod.details())
|
||||
.build();
|
||||
return Subscription.builder()
|
||||
.desiredSubscriptionDuration(newSubscription.desiredSubscriptionDuration())
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(newSubscription.desiredStartDate())
|
||||
.startDate(newSubscription.desiredStartDate())
|
||||
.endDate(newSubscription.desiredStartDate().plusMonths(newSubscription.desiredSubscriptionDuration()))
|
||||
.monthlyAmount(0)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static SubscriptionDTO toDTO(Subscription subscription) {
|
||||
return SubscriptionDTO.builder()
|
||||
.subscriptionId(subscription.getSubscriptionId())
|
||||
.customerId(subscription.getCustomerId())
|
||||
.desiredSubscriptionDuration(subscription.getDesiredSubscriptionDuration())
|
||||
.paymentMethod(subscription.getPaymentMethod())
|
||||
.desiredStartDate(subscription.getDesiredStartDate())
|
||||
.startDate(subscription.getStartDate())
|
||||
.endDate(subscription.getEndDate())
|
||||
.monthlyAmount(subscription.getMonthlyAmount())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class PaymentMethod {
|
||||
|
||||
private String paymentType;
|
||||
private Object details;
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class Subscription {
|
||||
|
||||
private UUID subscriptionId;
|
||||
private UUID customerId;
|
||||
private Integer desiredSubscriptionDuration;
|
||||
private PaymentMethod paymentMethod;
|
||||
private LocalDate desiredStartDate;
|
||||
private LocalDate startDate;
|
||||
private LocalDate endDate;
|
||||
private double monthlyAmount;
|
||||
|
||||
public void setRandomSubscriptionUUID() {
|
||||
|
||||
this.subscriptionId = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public void setRandomCustomerUUID() {
|
||||
|
||||
this.customerId = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public void setStartDate(){
|
||||
this.startDate = this.desiredStartDate;
|
||||
}
|
||||
|
||||
public void setEndDate(){
|
||||
this.endDate = this.desiredStartDate.plusMonths(this.desiredSubscriptionDuration);
|
||||
}
|
||||
|
||||
public void setDateSubscription(){
|
||||
setStartDate();
|
||||
setEndDate();
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
public class NotValidPaymentMethodException extends RuntimeException {
|
||||
|
||||
public NotValidPaymentMethodException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user