Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 804a064cb3 | |||
| 8e604b632c | |||
| f510b0b019 | |||
| 13b0c92161 | |||
| 785900645f | |||
| 1c15427966 | |||
| 2657186aed | |||
| a9c88b9910 | |||
| aee690817b | |||
| c11f549e83 | |||
| 33fcdcee0c | |||
| 8040300291 | |||
| f6652fb25c | |||
| ed39f635bf | |||
| 2c777021b9 | |||
| 45bb71b480 | |||
| 28010e306e | |||
| 9c6add29a9 | |||
| 247a28dcf0 | |||
| c7fd2e8311 |
@@ -0,0 +1,12 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class AvisDTO {
|
||||
private final UUID avisId;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
public record AvisInfo(
|
||||
UUID clientId,
|
||||
UUID livreId,
|
||||
int note,
|
||||
String commentaire,
|
||||
LocalDate dateAchat
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||
|
||||
public final class AvisConverter {
|
||||
|
||||
private AvisConverter() {
|
||||
}
|
||||
|
||||
public static Avis toDomain(AvisInfo avisInfo) {
|
||||
return Avis.builder()
|
||||
.clientId(avisInfo.clientId())
|
||||
.livreId(avisInfo.livreId())
|
||||
.note(avisInfo.note())
|
||||
.commentaire(avisInfo.commentaire())
|
||||
.dateAchat(avisInfo.dateAchat())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static AvisDTO toDTO(Avis avis) {
|
||||
return AvisDTO.builder()
|
||||
.avisId(avis.getId())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.entity;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class Avis {
|
||||
private UUID id;
|
||||
private UUID clientId;
|
||||
private UUID livreId;
|
||||
private int note;
|
||||
private String commentaire;
|
||||
private LocalDate dateAchat;
|
||||
|
||||
public void setRandomUUID() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
public class AvisNotFoundException extends Exception {
|
||||
|
||||
public static final String THE_AVIS_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The avis with id {0} does not exist";
|
||||
|
||||
public AvisNotFoundException(UUID uuid) {
|
||||
super(MessageFormat.format(THE_AVIS_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.exception;
|
||||
|
||||
public class NotValidAvisException extends Exception {
|
||||
|
||||
public NotValidAvisException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@NoArgsConstructor
|
||||
public final class AvisRepository {
|
||||
|
||||
private final List<Avis> avisList = new ArrayList<>();
|
||||
|
||||
public List<Avis> findAll() {
|
||||
return avisList;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
avisList.clear();
|
||||
}
|
||||
|
||||
public Avis save(Avis newAvis) {
|
||||
Optional<Avis> existing = this.findById(newAvis.getId());
|
||||
existing.ifPresentOrElse(avisList::remove, newAvis::setRandomUUID);
|
||||
this.avisList.add(newAvis);
|
||||
return newAvis;
|
||||
}
|
||||
|
||||
public Optional<Avis> findById(UUID uuid) {
|
||||
return this.avisList.stream()
|
||||
.filter(avis -> avis.getId().equals(uuid))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsById(UUID uuid) {
|
||||
return this.avisList.stream()
|
||||
.anyMatch(avis -> avis.getId().equals(uuid));
|
||||
}
|
||||
|
||||
public List<Avis> findByLivreId(UUID livreId) {
|
||||
return this.avisList.stream()
|
||||
.filter(avis -> avis.getLivreId().equals(livreId))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<Avis> findByClientId(UUID clientId) {
|
||||
return this.avisList.stream()
|
||||
.filter(avis -> avis.getClientId().equals(clientId))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public void delete(Avis avis) {
|
||||
avisList.remove(avis);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.converter.AvisConverter;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.exception.NotValidAvisException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.repository.AvisRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.validator.AvisValidator;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class AvisUseCase {
|
||||
|
||||
private final AvisRepository avisRepository;
|
||||
private final CustomerRepository customerRepository;
|
||||
|
||||
public AvisUseCase(AvisRepository avisRepository, CustomerRepository customerRepository) {
|
||||
this.avisRepository = avisRepository;
|
||||
this.customerRepository = customerRepository;
|
||||
}
|
||||
|
||||
public AvisDTO gererAvis(AvisInfo avisInfo) throws NotValidAvisException, CustomerNotFoundException {
|
||||
AvisValidator.validate(avisInfo);
|
||||
|
||||
customerRepository.findById(avisInfo.clientId())
|
||||
.orElseThrow(() -> new CustomerNotFoundException(avisInfo.clientId()));
|
||||
|
||||
Avis avis = AvisConverter.toDomain(avisInfo);
|
||||
Avis savedAvis = avisRepository.save(avis);
|
||||
|
||||
return AvisConverter.toDTO(savedAvis);
|
||||
}
|
||||
|
||||
public Optional<AvisDTO> findAvisById(UUID uuid) {
|
||||
return avisRepository.findById(uuid).map(AvisConverter::toDTO);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.exception.NotValidAvisException;
|
||||
|
||||
public final class AvisValidator {
|
||||
|
||||
public static final String CLIENT_ID_CANNOT_BE_NULL = "Client id cannot be null";
|
||||
public static final String LIVRE_ID_CANNOT_BE_NULL = "Livre id cannot be null";
|
||||
public static final String NOTE_MUST_BE_BETWEEN_1_AND_5 = "Note must be between 1 and 5";
|
||||
public static final String COMMENTAIRE_CANNOT_BE_BLANK = "Commentaire cannot be blank";
|
||||
public static final String DATE_ACHAT_CANNOT_BE_NULL = "Date achat cannot be null";
|
||||
|
||||
private AvisValidator() {
|
||||
}
|
||||
|
||||
public static void validate(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
validateClientId(avisInfo);
|
||||
validateLivreId(avisInfo);
|
||||
validateNote(avisInfo);
|
||||
validateCommentaire(avisInfo);
|
||||
validateDateAchat(avisInfo);
|
||||
}
|
||||
|
||||
private static void validateClientId(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.clientId() == null) {
|
||||
throw new NotValidAvisException(CLIENT_ID_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateLivreId(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.livreId() == null) {
|
||||
throw new NotValidAvisException(LIVRE_ID_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateNote(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.note() < 1 || avisInfo.note() > 5) {
|
||||
throw new NotValidAvisException(NOTE_MUST_BE_BETWEEN_1_AND_5);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateCommentaire(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.commentaire() == null || avisInfo.commentaire().isBlank()) {
|
||||
throw new NotValidAvisException(COMMENTAIRE_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateDateAchat(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.dateAchat() == null) {
|
||||
throw new NotValidAvisException(DATE_ACHAT_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class BookDTO {
|
||||
private final UUID id;
|
||||
private final String isbn;
|
||||
private final String title;
|
||||
private final String author;
|
||||
private final String publisher;
|
||||
private final LocalDate publicationDate;
|
||||
private final BigDecimal price;
|
||||
private final int stock;
|
||||
private final List<String> categories;
|
||||
private final String description;
|
||||
private final String language;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
public record BookInfo(
|
||||
String isbn,
|
||||
String title,
|
||||
String author,
|
||||
String publisher,
|
||||
LocalDate publicationDate,
|
||||
BigDecimal price,
|
||||
int initialStock,
|
||||
List<String> categories,
|
||||
String description,
|
||||
String language
|
||||
) {
|
||||
}
|
||||
@@ -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.BookInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||
|
||||
public final class BookConverter {
|
||||
|
||||
private BookConverter() {
|
||||
}
|
||||
|
||||
public static Book toDomain(BookInfo bookInfo) {
|
||||
return Book.builder()
|
||||
.isbn(bookInfo.isbn())
|
||||
.title(bookInfo.title())
|
||||
.author(bookInfo.author())
|
||||
.publisher(bookInfo.publisher())
|
||||
.publicationDate(bookInfo.publicationDate())
|
||||
.price(bookInfo.price())
|
||||
.stock(bookInfo.initialStock())
|
||||
.categories(bookInfo.categories())
|
||||
.description(bookInfo.description())
|
||||
.language(bookInfo.language())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static BookDTO toDTO(Book book) {
|
||||
return BookDTO.builder()
|
||||
.id(book.getId())
|
||||
.isbn(book.getIsbn())
|
||||
.title(book.getTitle())
|
||||
.author(book.getAuthor())
|
||||
.publisher(book.getPublisher())
|
||||
.publicationDate(book.getPublicationDate())
|
||||
.price(book.getPrice())
|
||||
.stock(book.getStock())
|
||||
.categories(book.getCategories())
|
||||
.description(book.getDescription())
|
||||
.language(book.getLanguage())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class Book {
|
||||
|
||||
private UUID id;
|
||||
private String isbn;
|
||||
private String title;
|
||||
private String author;
|
||||
private String publisher;
|
||||
private LocalDate publicationDate;
|
||||
private BigDecimal price;
|
||||
private int stock;
|
||||
private List<String> categories;
|
||||
private String description;
|
||||
private String language;
|
||||
|
||||
public void setRandomUUID() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BookNotFoundException extends Exception {
|
||||
|
||||
public static final String THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The book with id {0} does not exist";
|
||||
|
||||
public BookNotFoundException(UUID uuid) {
|
||||
super(MessageFormat.format(THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||
|
||||
public class NotValidBookException extends Exception {
|
||||
|
||||
public NotValidBookException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@NoArgsConstructor
|
||||
public final 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> optionalBookWithSameId = this.findById(newBook.getId());
|
||||
optionalBookWithSameId.ifPresentOrElse(books::remove, newBook::setRandomUUID);
|
||||
this.books.add(newBook);
|
||||
return newBook;
|
||||
}
|
||||
|
||||
public Optional<Book> findById(UUID uuid) {
|
||||
return this.books.stream()
|
||||
.filter(book -> book.getId().equals(uuid))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsById(UUID uuid) {
|
||||
return this.books.stream()
|
||||
.anyMatch(book -> book.getId().equals(uuid));
|
||||
}
|
||||
|
||||
public Optional<Book> findByIsbn(String isbn) {
|
||||
return this.books.stream()
|
||||
.filter(book -> book.getIsbn().equals(isbn))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public void delete(Book book) {
|
||||
this.books.remove(book);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
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.BookInfo;
|
||||
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.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;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class BookUseCase {
|
||||
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
public BookUseCase(BookRepository bookRepository) {
|
||||
this.bookRepository = bookRepository;
|
||||
}
|
||||
|
||||
public String registerBook(BookInfo bookInfo) throws NotValidBookException {
|
||||
BookValidator.validate(bookInfo);
|
||||
Book bookToRegister = BookConverter.toDomain(bookInfo);
|
||||
Book registeredBook = bookRepository.save(bookToRegister);
|
||||
return registeredBook.getIsbn();
|
||||
}
|
||||
|
||||
public Optional<BookDTO> findBookByIsbn(String isbn) {
|
||||
Optional<Book> optionalBook = bookRepository.findByIsbn(isbn);
|
||||
return optionalBook.map(BookConverter::toDTO);
|
||||
}
|
||||
|
||||
public BookDTO updateBook(UUID uuid, BookInfo bookInfo) throws BookNotFoundException, NotValidBookException {
|
||||
BookValidator.validate(bookInfo);
|
||||
Book existingBook = getBookIfNotFoundThrowException(uuid);
|
||||
Book updatedBook = Book.builder()
|
||||
.id(uuid)
|
||||
.isbn(bookInfo.isbn())
|
||||
.title(bookInfo.title())
|
||||
.author(bookInfo.author())
|
||||
.publisher(bookInfo.publisher())
|
||||
.publicationDate(bookInfo.publicationDate())
|
||||
.price(bookInfo.price())
|
||||
.stock(existingBook.getStock())
|
||||
.categories(bookInfo.categories())
|
||||
.description(bookInfo.description())
|
||||
.language(bookInfo.language())
|
||||
.build();
|
||||
Book saved = bookRepository.save(updatedBook);
|
||||
return BookConverter.toDTO(saved);
|
||||
}
|
||||
|
||||
public void deleteBook(UUID uuid) throws BookNotFoundException {
|
||||
Book bookToDelete = getBookIfNotFoundThrowException(uuid);
|
||||
bookRepository.delete(bookToDelete);
|
||||
}
|
||||
|
||||
private Book getBookIfNotFoundThrowException(UUID uuid) throws BookNotFoundException {
|
||||
Optional<Book> optionalBook = bookRepository.findById(uuid);
|
||||
if (optionalBook.isEmpty()) {
|
||||
throw new BookNotFoundException(uuid);
|
||||
}
|
||||
return optionalBook.get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public final class BookValidator {
|
||||
|
||||
public static final String ISBN_IS_NOT_VALID = "ISBN is not valid";
|
||||
public static final String PRICE_MUST_BE_POSITIVE = "Price must be positive";
|
||||
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 PUBLISHER_CANNOT_BE_BLANK = "Publisher cannot be blank";
|
||||
public static final String ISBN_REGEX = "\\d{13}";
|
||||
|
||||
private BookValidator() {
|
||||
}
|
||||
|
||||
public static void validate(BookInfo bookInfo) throws NotValidBookException {
|
||||
validateIsbn(bookInfo);
|
||||
validateTitle(bookInfo);
|
||||
validateAuthor(bookInfo);
|
||||
validatePublisher(bookInfo);
|
||||
validatePrice(bookInfo);
|
||||
}
|
||||
|
||||
private static void validateIsbn(BookInfo bookInfo) throws NotValidBookException {
|
||||
if (bookInfo.isbn() == null || bookInfo.isbn().isBlank()) {
|
||||
throw new NotValidBookException(ISBN_IS_NOT_VALID);
|
||||
}
|
||||
if (!bookInfo.isbn().matches(ISBN_REGEX)) {
|
||||
throw new NotValidBookException(ISBN_IS_NOT_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateTitle(BookInfo bookInfo) throws NotValidBookException {
|
||||
if (bookInfo.title() == null || bookInfo.title().isBlank()) {
|
||||
throw new NotValidBookException(TITLE_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateAuthor(BookInfo bookInfo) throws NotValidBookException {
|
||||
if (bookInfo.author() == null || bookInfo.author().isBlank()) {
|
||||
throw new NotValidBookException(AUTHOR_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePublisher(BookInfo bookInfo) throws NotValidBookException {
|
||||
if (bookInfo.publisher() == null || bookInfo.publisher().isBlank()) {
|
||||
throw new NotValidBookException(PUBLISHER_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePrice(BookInfo bookInfo) throws NotValidBookException {
|
||||
if (bookInfo.price() == null || bookInfo.price().compareTo(BigDecimal.ZERO) <= 0) {
|
||||
throw new NotValidBookException(PRICE_MUST_BE_POSITIVE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer;
|
||||
|
||||
import java.util.UUID;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class CustomerDTO {
|
||||
|
||||
@@ -2,10 +2,11 @@ package fr.iut_fbleau.but3.dev62.mylibrary.customer.entity;
|
||||
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.IllegalCustomerPointException;
|
||||
import java.util.UUID;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class Customer {
|
||||
|
||||
+2
-1
@@ -1,11 +1,12 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor
|
||||
public final class CustomerRepository {
|
||||
|
||||
@@ -9,6 +9,7 @@ import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.IllegalCustomerPoin
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.validator.CustomerValidator;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order;
|
||||
|
||||
public record AdresseLivraison(String rue, String ville, String codePostal, String pays) {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record LigneCommandeInfo(UUID livreId, int quantite) {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order;
|
||||
|
||||
public enum ModePaiement {
|
||||
CB,
|
||||
PAYPAL,
|
||||
POINTS_FIDELITE
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class OrderDTO {
|
||||
private final UUID commandeId;
|
||||
private final BigDecimal montantTotal;
|
||||
private final int pointsFideliteGagnes;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public record OrderInfo(
|
||||
UUID clientId,
|
||||
List<LigneCommandeInfo> lignesCommande,
|
||||
AdresseLivraison adresseLivraison,
|
||||
ModePaiement modePaiement
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.LigneCommande;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
public final class OrderConverter {
|
||||
|
||||
private OrderConverter() {
|
||||
}
|
||||
|
||||
public static Order toDomain(OrderInfo orderInfo, List<LigneCommande> lignes,
|
||||
BigDecimal montantTotal, int pointsFideliteGagnes) {
|
||||
return Order.builder()
|
||||
.clientId(orderInfo.clientId())
|
||||
.lignesCommande(lignes)
|
||||
.adresseLivraison(orderInfo.adresseLivraison())
|
||||
.modePaiement(orderInfo.modePaiement())
|
||||
.montantTotal(montantTotal)
|
||||
.pointsFideliteGagnes(pointsFideliteGagnes)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static OrderDTO toDTO(Order order) {
|
||||
return OrderDTO.builder()
|
||||
.commandeId(order.getId())
|
||||
.montantTotal(order.getMontantTotal())
|
||||
.pointsFideliteGagnes(order.getPointsFideliteGagnes())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order.entity;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class LigneCommande {
|
||||
private UUID livreId;
|
||||
private int quantite;
|
||||
private BigDecimal prixUnitaire;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order.entity;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class Order {
|
||||
private UUID id;
|
||||
private UUID clientId;
|
||||
private List<LigneCommande> lignesCommande;
|
||||
private AdresseLivraison adresseLivraison;
|
||||
private ModePaiement modePaiement;
|
||||
private BigDecimal montantTotal;
|
||||
private int pointsFideliteGagnes;
|
||||
|
||||
public void setRandomUUID() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order.exception;
|
||||
|
||||
public class NotValidOrderException extends Exception {
|
||||
|
||||
public NotValidOrderException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
public class OrderNotFoundException extends Exception {
|
||||
|
||||
public static final String THE_ORDER_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The order with id {0} does not exist";
|
||||
|
||||
public OrderNotFoundException(UUID uuid) {
|
||||
super(MessageFormat.format(THE_ORDER_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
|
||||
}
|
||||
}
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@NoArgsConstructor
|
||||
public final class OrderRepository {
|
||||
|
||||
private final List<Order> orders = new ArrayList<>();
|
||||
|
||||
public List<Order> findAll() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
orders.clear();
|
||||
}
|
||||
|
||||
public Order save(Order newOrder) {
|
||||
Optional<Order> existing = this.findById(newOrder.getId());
|
||||
existing.ifPresentOrElse(orders::remove, newOrder::setRandomUUID);
|
||||
this.orders.add(newOrder);
|
||||
return newOrder;
|
||||
}
|
||||
|
||||
public Optional<Order> findById(UUID uuid) {
|
||||
return this.orders.stream()
|
||||
.filter(order -> order.getId().equals(uuid))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsById(UUID uuid) {
|
||||
return this.orders.stream()
|
||||
.anyMatch(order -> order.getId().equals(uuid));
|
||||
}
|
||||
|
||||
public List<Order> findByClientId(UUID clientId) {
|
||||
return this.orders.stream()
|
||||
.filter(order -> order.getClientId().equals(clientId))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public void delete(Order order) {
|
||||
this.orders.remove(order);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.converter.OrderConverter;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.LigneCommande;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.repository.OrderRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.validator.OrderValidator;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class OrderUseCase {
|
||||
|
||||
private final OrderRepository orderRepository;
|
||||
private final CustomerRepository customerRepository;
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
public OrderUseCase(OrderRepository orderRepository, CustomerRepository customerRepository,
|
||||
BookRepository bookRepository) {
|
||||
this.orderRepository = orderRepository;
|
||||
this.customerRepository = customerRepository;
|
||||
this.bookRepository = bookRepository;
|
||||
}
|
||||
|
||||
public OrderDTO passerCommande(OrderInfo orderInfo)
|
||||
throws NotValidOrderException, CustomerNotFoundException {
|
||||
OrderValidator.validate(orderInfo);
|
||||
|
||||
Customer customer = customerRepository.findById(orderInfo.clientId())
|
||||
.orElseThrow(() -> new CustomerNotFoundException(orderInfo.clientId()));
|
||||
|
||||
List<LigneCommande> lignes = new ArrayList<>();
|
||||
BigDecimal montantTotal = BigDecimal.ZERO;
|
||||
|
||||
for (LigneCommandeInfo ligneInfo : orderInfo.lignesCommande()) {
|
||||
Book book = bookRepository.findById(ligneInfo.livreId())
|
||||
.orElseThrow(() -> new NotValidOrderException("Book not found: " + ligneInfo.livreId()));
|
||||
|
||||
BigDecimal prixLigne = book.getPrice().multiply(BigDecimal.valueOf(ligneInfo.quantite()));
|
||||
montantTotal = montantTotal.add(prixLigne);
|
||||
|
||||
lignes.add(LigneCommande.builder()
|
||||
.livreId(ligneInfo.livreId())
|
||||
.quantite(ligneInfo.quantite())
|
||||
.prixUnitaire(book.getPrice())
|
||||
.build());
|
||||
}
|
||||
|
||||
int pointsGagnes = montantTotal.intValue();
|
||||
|
||||
if (orderInfo.modePaiement() == ModePaiement.POINTS_FIDELITE) {
|
||||
customer.addLoyaltyPoints(-montantTotal.intValue());
|
||||
} else {
|
||||
customer.addLoyaltyPoints(pointsGagnes);
|
||||
}
|
||||
customerRepository.save(customer);
|
||||
|
||||
Order order = OrderConverter.toDomain(orderInfo, lignes, montantTotal, pointsGagnes);
|
||||
Order savedOrder = orderRepository.save(order);
|
||||
|
||||
return OrderConverter.toDTO(savedOrder);
|
||||
}
|
||||
|
||||
public Optional<OrderDTO> findOrderById(UUID uuid) {
|
||||
return orderRepository.findById(uuid).map(OrderConverter::toDTO);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.order.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException;
|
||||
|
||||
public final class OrderValidator {
|
||||
|
||||
public static final String CLIENT_ID_CANNOT_BE_NULL = "Client id cannot be null";
|
||||
public static final String LIGNES_COMMANDE_CANNOT_BE_EMPTY = "Order must have at least one item";
|
||||
public static final String QUANTITE_MUST_BE_POSITIVE = "Quantity must be positive";
|
||||
public static final String MODE_PAIEMENT_CANNOT_BE_NULL = "Payment method cannot be null";
|
||||
public static final String ADRESSE_LIVRAISON_CANNOT_BE_NULL = "Delivery address cannot be null";
|
||||
|
||||
private OrderValidator() {
|
||||
}
|
||||
|
||||
public static void validate(OrderInfo orderInfo) throws NotValidOrderException {
|
||||
validateClientId(orderInfo);
|
||||
validateLignesCommande(orderInfo);
|
||||
validateAdresseLivraison(orderInfo);
|
||||
validateModePaiement(orderInfo);
|
||||
}
|
||||
|
||||
private static void validateClientId(OrderInfo orderInfo) throws NotValidOrderException {
|
||||
if (orderInfo.clientId() == null) {
|
||||
throw new NotValidOrderException(CLIENT_ID_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateLignesCommande(OrderInfo orderInfo) throws NotValidOrderException {
|
||||
if (orderInfo.lignesCommande() == null || orderInfo.lignesCommande().isEmpty()) {
|
||||
throw new NotValidOrderException(LIGNES_COMMANDE_CANNOT_BE_EMPTY);
|
||||
}
|
||||
for (LigneCommandeInfo ligne : orderInfo.lignesCommande()) {
|
||||
if (ligne.quantite() <= 0) {
|
||||
throw new NotValidOrderException(QUANTITE_MUST_BE_POSITIVE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateAdresseLivraison(OrderInfo orderInfo) throws NotValidOrderException {
|
||||
if (orderInfo.adresseLivraison() == null) {
|
||||
throw new NotValidOrderException(ADRESSE_LIVRAISON_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateModePaiement(OrderInfo orderInfo) throws NotValidOrderException {
|
||||
if (orderInfo.modePaiement() == null) {
|
||||
throw new NotValidOrderException(MODE_PAIEMENT_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class ModePaiement {
|
||||
private final TypePaiement type;
|
||||
private final Object details;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class SubscriptionDTO {
|
||||
private final UUID abonnementId;
|
||||
private final Date dateDebut;
|
||||
private final Date dateFin;
|
||||
private final BigDecimal montantMensuel;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public enum SubscriptionDuree {
|
||||
M3,M6,M12;
|
||||
|
||||
|
||||
private Integer[] subValues = {3,6,12};
|
||||
private BigDecimal[] subPrices = {BigDecimal.valueOf(9.99), BigDecimal.valueOf(9.50), BigDecimal.valueOf(8.99)};
|
||||
|
||||
public int getValue(){
|
||||
return subValues[this.ordinal()];
|
||||
}
|
||||
|
||||
public BigDecimal getMonthlyPricing(){
|
||||
return subPrices[this.ordinal()];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public record SubscriptionInfo(
|
||||
UUID clientId,
|
||||
SubscriptionDuree duree,
|
||||
ModePaiement modePaiement,
|
||||
Date dateDebutSouhaitee
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
|
||||
public enum TypePaiement {
|
||||
CB,
|
||||
Paypal
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.converter;
|
||||
|
||||
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.Subscription;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
public final class SubscriptionConverter {
|
||||
|
||||
private SubscriptionConverter() {
|
||||
}
|
||||
|
||||
public static Subscription toDomain(SubscriptionInfo subscriptionInfo) {
|
||||
Subscription sub = Subscription.builder()
|
||||
.clientId(subscriptionInfo.clientId())
|
||||
.duree(subscriptionInfo.duree())
|
||||
.modePaiement(subscriptionInfo.modePaiement())
|
||||
.dateDebutSouhaitee(subscriptionInfo.dateDebutSouhaitee())
|
||||
.build();
|
||||
sub.setRandomUUID();
|
||||
return sub;
|
||||
}
|
||||
|
||||
public static SubscriptionDTO toDTO(Subscription subscription) {
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(subscription.getDateDebutSouhaitee());
|
||||
cal.add(Calendar.MONTH, subscription.getDuree().getValue());
|
||||
|
||||
return SubscriptionDTO.builder()
|
||||
.abonnementId(subscription.getAbonnementId())
|
||||
.dateDebut(subscription.getDateDebutSouhaitee())
|
||||
.dateFin(cal.getTime())
|
||||
.montantMensuel(subscription.getDuree().getMonthlyPricing())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
|
||||
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.ModePaiement;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDuree;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class Subscription {
|
||||
private UUID abonnementId;
|
||||
private UUID clientId;
|
||||
private SubscriptionDuree duree;
|
||||
private ModePaiement modePaiement;
|
||||
private Date dateDebutSouhaitee;
|
||||
|
||||
public void setRandomUUID() {
|
||||
this.abonnementId = UUID.randomUUID();
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
public class NotValidSubscriptionException extends Exception {
|
||||
|
||||
public NotValidSubscriptionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SubscriptionNotFoundException extends Exception {
|
||||
|
||||
public static final String THE_SUBSCRIPTION_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The Subscription with id {0} does not exist";
|
||||
|
||||
public SubscriptionNotFoundException(UUID uuid) {
|
||||
super(MessageFormat.format(THE_SUBSCRIPTION_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
|
||||
}
|
||||
}
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@NoArgsConstructor
|
||||
public final class SubscriptionRepository {
|
||||
|
||||
private final List<Subscription> subscriptions = new ArrayList<>();
|
||||
|
||||
public List<Subscription> findAll() {
|
||||
return subscriptions;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
subscriptions.clear();
|
||||
}
|
||||
|
||||
public Subscription save(Subscription newSubscription) {
|
||||
Optional<Subscription> optionalBookWithSameId = this.findByUuid(newSubscription.getAbonnementId());
|
||||
optionalBookWithSameId.ifPresentOrElse(subscriptions::remove, newSubscription::setRandomUUID);
|
||||
this.subscriptions.add(newSubscription);
|
||||
return newSubscription;
|
||||
}
|
||||
|
||||
public Optional<Subscription> findByUuid(UUID uuid) {
|
||||
return this.subscriptions.stream()
|
||||
.filter(subscription -> subscription.getAbonnementId().equals(uuid))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsById(UUID uuid) {
|
||||
return this.subscriptions.stream()
|
||||
.anyMatch(subscription -> subscription.getAbonnementId().equals(uuid));
|
||||
}
|
||||
|
||||
public Optional<Subscription> findByClientUuid(UUID uuid) {
|
||||
return this.subscriptions.stream()
|
||||
.filter(subscription -> subscription.getClientId().equals(uuid))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public void delete(Subscription subscription) {
|
||||
this.subscriptions.remove(subscription);
|
||||
}
|
||||
}
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase;
|
||||
|
||||
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.converter.SubscriptionConverter;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.SubscriptionNotFoundException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository.SubscriptionRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator.SubscriptionValidator;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class SubscriptionUseCase {
|
||||
|
||||
private final SubscriptionRepository subscriptionRepository;
|
||||
|
||||
public SubscriptionUseCase(SubscriptionRepository subscriptionRepository) {
|
||||
this.subscriptionRepository = subscriptionRepository;
|
||||
}
|
||||
|
||||
public SubscriptionDTO registerSubscription(SubscriptionInfo subscriptionInfo) throws NotValidSubscriptionException {
|
||||
SubscriptionValidator.validate(subscriptionInfo);
|
||||
Subscription subscriptionToRegister = SubscriptionConverter.toDomain(subscriptionInfo);
|
||||
Subscription registeredSubscription = subscriptionRepository.save(subscriptionToRegister);
|
||||
return SubscriptionConverter.toDTO(registeredSubscription);
|
||||
}
|
||||
|
||||
public Optional<SubscriptionDTO> findSubscriptionByUuid(UUID uuid) {
|
||||
Optional<Subscription> optionalSubscription = subscriptionRepository.findByClientUuid(uuid);
|
||||
return optionalSubscription.map(SubscriptionConverter::toDTO);
|
||||
}
|
||||
|
||||
public SubscriptionDTO updateSubscription(UUID uuid, SubscriptionInfo subscriptionInfo) throws SubscriptionNotFoundException, NotValidSubscriptionException {
|
||||
SubscriptionValidator.validate(subscriptionInfo);
|
||||
Subscription existingSubscription = getSubscriptionIfNotFoundThrowException(uuid);
|
||||
Subscription updatedSubscription = Subscription.builder()
|
||||
.abonnementId(uuid)
|
||||
.clientId(subscriptionInfo.clientId())
|
||||
.duree(subscriptionInfo.duree())
|
||||
.modePaiement(subscriptionInfo.modePaiement())
|
||||
.dateDebutSouhaitee(subscriptionInfo.dateDebutSouhaitee())
|
||||
.build();
|
||||
Subscription saved = subscriptionRepository.save(updatedSubscription);
|
||||
return SubscriptionConverter.toDTO(saved);
|
||||
}
|
||||
|
||||
public void deleteSubscription(UUID uuid) throws SubscriptionNotFoundException {
|
||||
Subscription subscriptionToDelete = getSubscriptionIfNotFoundThrowException(uuid);
|
||||
subscriptionRepository.delete(subscriptionToDelete);
|
||||
}
|
||||
|
||||
private Subscription getSubscriptionIfNotFoundThrowException(UUID uuid) throws SubscriptionNotFoundException {
|
||||
Optional<Subscription> optionalSubscription = subscriptionRepository.findByUuid(uuid);
|
||||
if (optionalSubscription.isEmpty()) {
|
||||
throw new SubscriptionNotFoundException(uuid);
|
||||
}
|
||||
return optionalSubscription.get();
|
||||
}
|
||||
}
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.TypePaiement;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||
|
||||
public final class SubscriptionValidator {
|
||||
|
||||
public static final String Client_ID_NOT_VALID = "l'identifient du client est incorect" ;
|
||||
public static final String LA_DUREE_RENSEIGNEE_N_EST_PAS_CORRECTE = "La durée renseignée n'est pas correcte";
|
||||
public static final String LE_TYPE_DE_PAYEMENT_N_EST_PAS_PRIS_EN_CHARGE = "Le type de payement n'est pas pris en charge";
|
||||
public static final String LA_DATE_DE_DEBUT_N_A_PAS_ETE_RENSEIGNEE = "La date de début n'a pas été renseignée";
|
||||
|
||||
private SubscriptionValidator() {
|
||||
}
|
||||
|
||||
public static void validate(SubscriptionInfo bookInfo) throws NotValidSubscriptionException {
|
||||
validateClientID(bookInfo);
|
||||
validateDuree(bookInfo);
|
||||
validateModePaiement(bookInfo);
|
||||
validateDateDebut(bookInfo);
|
||||
}
|
||||
|
||||
private static void validateClientID(SubscriptionInfo bookInfo) throws NotValidSubscriptionException {
|
||||
if (bookInfo.clientId() == null){
|
||||
throw new NotValidSubscriptionException(Client_ID_NOT_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateDuree(SubscriptionInfo bookInfo) throws NotValidSubscriptionException {
|
||||
try {
|
||||
if (bookInfo.duree().getValue() != 3 && bookInfo.duree().getValue() != 6 && bookInfo.duree().getValue() != 12){
|
||||
throw new NotValidSubscriptionException(LA_DUREE_RENSEIGNEE_N_EST_PAS_CORRECTE);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new NotValidSubscriptionException(LA_DUREE_RENSEIGNEE_N_EST_PAS_CORRECTE);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateModePaiement(SubscriptionInfo bookInfo) throws NotValidSubscriptionException{
|
||||
if (bookInfo.modePaiement().getType() != TypePaiement.CB && bookInfo.modePaiement().getType() != TypePaiement.Paypal){
|
||||
throw new NotValidSubscriptionException(LE_TYPE_DE_PAYEMENT_N_EST_PAS_PRIS_EN_CHARGE);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateDateDebut(SubscriptionInfo bookInfo) throws NotValidSubscriptionException{
|
||||
if (bookInfo.dateDebutSouhaitee() == null){
|
||||
throw new NotValidSubscriptionException(LA_DATE_DE_DEBUT_N_A_PAS_ETE_RENSEIGNEE);
|
||||
}
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@DisplayName("AvisConverter Unit Tests")
|
||||
class AvisConverterTest {
|
||||
|
||||
private final UUID clientId = UUID.randomUUID();
|
||||
private final UUID livreId = UUID.randomUUID();
|
||||
|
||||
@Nested
|
||||
@DisplayName("toDomain() method tests")
|
||||
class ToDomainTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should convert AvisInfo to Avis domain object")
|
||||
void shouldConvertAvisInfoToDomain() {
|
||||
AvisInfo avisInfo = new AvisInfo(
|
||||
clientId, livreId, 5, "Excellent livre !", LocalDate.of(2024, 1, 15)
|
||||
);
|
||||
|
||||
Avis result = AvisConverter.toDomain(avisInfo);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(clientId, result.getClientId());
|
||||
assertEquals(livreId, result.getLivreId());
|
||||
assertEquals(5, result.getNote());
|
||||
assertEquals("Excellent livre !", result.getCommentaire());
|
||||
assertEquals(LocalDate.of(2024, 1, 15), result.getDateAchat());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should have null ID after toDomain (set by repository)")
|
||||
void shouldHaveNullIdAfterToDomain() {
|
||||
AvisInfo avisInfo = new AvisInfo(clientId, livreId, 3, "Commentaire", LocalDate.now());
|
||||
|
||||
Avis result = AvisConverter.toDomain(avisInfo);
|
||||
|
||||
assertNull(result.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("toDTO() method tests")
|
||||
class ToDTOTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should convert Avis domain object to AvisDTO")
|
||||
void shouldConvertAvisToDTO() {
|
||||
UUID avisId = UUID.randomUUID();
|
||||
Avis avis = Avis.builder()
|
||||
.id(avisId)
|
||||
.clientId(clientId)
|
||||
.livreId(livreId)
|
||||
.note(5)
|
||||
.commentaire("Excellent !")
|
||||
.dateAchat(LocalDate.of(2024, 1, 15))
|
||||
.build();
|
||||
|
||||
AvisDTO result = AvisConverter.toDTO(avis);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(avisId, result.getAvisId());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.entity;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AvisTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Builder should create a valid Avis instance")
|
||||
void testAvisBuilder() {
|
||||
UUID id = UUID.randomUUID();
|
||||
UUID clientId = UUID.randomUUID();
|
||||
UUID livreId = UUID.randomUUID();
|
||||
|
||||
Avis avis = Avis.builder()
|
||||
.id(id)
|
||||
.clientId(clientId)
|
||||
.livreId(livreId)
|
||||
.note(5)
|
||||
.commentaire("Excellent livre !")
|
||||
.dateAchat(LocalDate.of(2024, 1, 15))
|
||||
.build();
|
||||
|
||||
assertEquals(id, avis.getId());
|
||||
assertEquals(clientId, avis.getClientId());
|
||||
assertEquals(livreId, avis.getLivreId());
|
||||
assertEquals(5, avis.getNote());
|
||||
assertEquals("Excellent livre !", avis.getCommentaire());
|
||||
assertEquals(LocalDate.of(2024, 1, 15), avis.getDateAchat());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("setRandomUUID should set a new non-null UUID")
|
||||
void testSetRandomUUID() {
|
||||
Avis avis = Avis.builder().build();
|
||||
UUID originalId = avis.getId();
|
||||
|
||||
avis.setRandomUUID();
|
||||
|
||||
assertNotNull(avis.getId());
|
||||
assertNotEquals(originalId, avis.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Two setRandomUUID calls should produce different UUIDs")
|
||||
void testSetRandomUUIDTwice() {
|
||||
Avis avis = Avis.builder().build();
|
||||
avis.setRandomUUID();
|
||||
UUID firstId = avis.getId();
|
||||
|
||||
avis.setRandomUUID();
|
||||
|
||||
assertNotEquals(firstId, avis.getId());
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.exception;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class AvisNotFoundExceptionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the UUID provided")
|
||||
void testExceptionMessageContainsUUID() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
AvisNotFoundException exception = new AvisNotFoundException(uuid);
|
||||
|
||||
String expectedMessage = String.format("The avis with id %s does not exist", uuid);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format")
|
||||
void testExceptionUsesConstantMessageFormat() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
AvisNotFoundException exception = new AvisNotFoundException(uuid);
|
||||
|
||||
assertEquals("The avis with id {0} does not exist",
|
||||
AvisNotFoundException.THE_AVIS_WITH_ID_DOES_NOT_EXIST_MESSAGE);
|
||||
assertTrue(exception.getMessage().contains(uuid.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be properly thrown and caught")
|
||||
void testExceptionCanBeThrownAndCaught() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
try {
|
||||
throw new AvisNotFoundException(uuid);
|
||||
} catch (AvisNotFoundException e) {
|
||||
assertTrue(e.getMessage().contains(uuid.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user