forked from pierront/mylibrary-template
🎉
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer;
|
||||
|
||||
import java.util.UUID;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class CustomerDTO {
|
||||
private final UUID id;
|
||||
private final String firstName;
|
||||
private final String lastName;
|
||||
private final String phoneNumber;
|
||||
private final int loyaltyPoints;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer;
|
||||
|
||||
public record CustomerInfo(String firstName, String lastName, String phoneNumber) {
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||
|
||||
public final class CustomerConverter {
|
||||
private CustomerConverter(){
|
||||
|
||||
}
|
||||
|
||||
public static Customer toDomain(CustomerInfo newCustomer) {
|
||||
return Customer.builder()
|
||||
.firstName(newCustomer.firstName())
|
||||
.lastName(newCustomer.lastName())
|
||||
.phoneNumber(newCustomer.phoneNumber())
|
||||
.loyaltyPoints(0)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static CustomerDTO toDTO(Customer customer) {
|
||||
return CustomerDTO.builder()
|
||||
.id(customer.getId())
|
||||
.firstName(customer.getFirstName())
|
||||
.lastName(customer.getLastName())
|
||||
.phoneNumber(customer.getPhoneNumber())
|
||||
.loyaltyPoints(customer.getLoyaltyPoints())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class Customer {
|
||||
private UUID id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String phoneNumber;
|
||||
private int loyaltyPoints;
|
||||
|
||||
public void setRandomUUID() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public void addLoyaltyPoints(int loyaltyPointToAdd) {
|
||||
this.loyaltyPoints += loyaltyPointToAdd;
|
||||
}
|
||||
|
||||
public void removeLoyaltyPoints(int loyaltyPointToRemove) throws IllegalCustomerPointException {
|
||||
if (loyaltyPointToRemove > this.loyaltyPoints) throw new IllegalCustomerPointException(loyaltyPointToRemove, this.loyaltyPoints);
|
||||
this.loyaltyPoints -= loyaltyPointToRemove;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CustomerNotFoundException extends Exception {
|
||||
|
||||
public static final String THE_CUSTOMER_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The customer with id {0} does not exist";
|
||||
|
||||
public CustomerNotFoundException(UUID uuid) {
|
||||
super(MessageFormat.format(THE_CUSTOMER_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
public class IllegalCustomerPointException extends Exception {
|
||||
|
||||
public static final String CANNOT_REMOVE_LOYALTY_POINTS = "Cannot remove {0} points from {1} points";
|
||||
|
||||
public IllegalCustomerPointException(int needed, int actual) {
|
||||
super(MessageFormat.format(CANNOT_REMOVE_LOYALTY_POINTS, needed,
|
||||
actual));
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer.exception;
|
||||
|
||||
public class NotValidCustomerException extends Exception {
|
||||
|
||||
public NotValidCustomerException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor
|
||||
public final class CustomerRepository {
|
||||
private final List<Customer> customers = new ArrayList<>();
|
||||
|
||||
public List<Customer> findAll() {
|
||||
return customers;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
customers.clear();
|
||||
}
|
||||
|
||||
public Customer save(Customer newCustomer) {
|
||||
Optional<Customer> optionalCustomerWithSameId = this.findById(newCustomer.getId());
|
||||
optionalCustomerWithSameId.ifPresentOrElse(customers::remove, newCustomer::setRandomUUID);
|
||||
this.customers.add(newCustomer);
|
||||
return newCustomer;
|
||||
}
|
||||
|
||||
public Optional<Customer> findById(UUID uuid) {
|
||||
return this.customers.stream()
|
||||
.filter(customer -> customer.getId().equals(uuid))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsById(UUID uuid) {
|
||||
return this.customers.stream()
|
||||
.anyMatch(customer -> customer.getId().equals(uuid));
|
||||
}
|
||||
|
||||
public Optional<Customer> findByPhoneNumber(String phoneNumber) {
|
||||
return this.customers.stream()
|
||||
.filter(customer -> customer.getPhoneNumber().equals(phoneNumber))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public void delete(Customer customer) {
|
||||
this.customers.remove(customer);
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.converter.CustomerConverter;
|
||||
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.exception.IllegalCustomerPointException;
|
||||
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;
|
||||
|
||||
public final class CustomerUseCase {
|
||||
|
||||
private final CustomerRepository customerRepository;
|
||||
|
||||
public CustomerUseCase(CustomerRepository customerRepository) {
|
||||
this.customerRepository = customerRepository;
|
||||
}
|
||||
|
||||
public UUID registerCustomer(CustomerInfo newCustomer) throws NotValidCustomerException {
|
||||
CustomerValidator.validate(newCustomer);
|
||||
Customer customerToRegister = CustomerConverter.toDomain(newCustomer);
|
||||
Customer customerToRegistered = customerRepository.save(customerToRegister);
|
||||
return customerToRegistered.getId();
|
||||
}
|
||||
|
||||
public Optional<CustomerDTO> findCustomerByPhoneNumber(String phoneNumber) {
|
||||
Optional<Customer> optionalCustomer = customerRepository.findByPhoneNumber(phoneNumber);
|
||||
return optionalCustomer.map(CustomerConverter::toDTO);
|
||||
}
|
||||
|
||||
public CustomerDTO updateCustomer(UUID uuid, CustomerInfo customerInfo)
|
||||
throws CustomerNotFoundException, NotValidCustomerException {
|
||||
CustomerValidator.validate(customerInfo);
|
||||
Customer customerByUUID = getCustomerIfDoesNotExistThrowCustomerNotFoundException(
|
||||
uuid);
|
||||
Customer customer = Customer.builder()
|
||||
.id(uuid)
|
||||
.firstName(customerInfo.firstName())
|
||||
.lastName(customerInfo.lastName())
|
||||
.phoneNumber(customerInfo.phoneNumber())
|
||||
.loyaltyPoints(customerByUUID.getLoyaltyPoints())
|
||||
.build();
|
||||
Customer updatedCustomer = customerRepository.save(customer);
|
||||
return CustomerConverter.toDTO(updatedCustomer);
|
||||
}
|
||||
|
||||
public void deleteCustomer(UUID uuid) throws CustomerNotFoundException {
|
||||
Customer customerToDelete = getCustomerIfDoesNotExistThrowCustomerNotFoundException(uuid);
|
||||
this.customerRepository.delete(customerToDelete);
|
||||
}
|
||||
|
||||
public int addLoyaltyPoints(UUID uuid, int loyaltyPointToAdd) throws CustomerNotFoundException {
|
||||
Customer customerToAddLoyaltyPoints = getCustomerIfDoesNotExistThrowCustomerNotFoundException(
|
||||
uuid);
|
||||
customerToAddLoyaltyPoints.addLoyaltyPoints(loyaltyPointToAdd);
|
||||
customerRepository.save(customerToAddLoyaltyPoints);
|
||||
return customerToAddLoyaltyPoints.getLoyaltyPoints();
|
||||
}
|
||||
|
||||
public int subtractLoyaltyPoints(UUID uuid, int loyaltyPointToRemove)
|
||||
throws CustomerNotFoundException, IllegalCustomerPointException {
|
||||
Customer customerToSubtractLoyaltyPoints = getCustomerIfDoesNotExistThrowCustomerNotFoundException(
|
||||
uuid);
|
||||
customerToSubtractLoyaltyPoints.removeLoyaltyPoints(loyaltyPointToRemove);
|
||||
customerRepository.save(customerToSubtractLoyaltyPoints);
|
||||
return customerToSubtractLoyaltyPoints.getLoyaltyPoints();
|
||||
}
|
||||
|
||||
private Customer getCustomerIfDoesNotExistThrowCustomerNotFoundException(UUID uuid)
|
||||
throws CustomerNotFoundException {
|
||||
Optional<Customer> optionalCustomerById = customerRepository.findById(uuid);
|
||||
if (optionalCustomerById.isEmpty()) {
|
||||
throw new CustomerNotFoundException(uuid);
|
||||
}
|
||||
return optionalCustomerById.get();
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.customer.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException;
|
||||
|
||||
public final class CustomerValidator {
|
||||
|
||||
public static final String PHONE_NUMBER_IS_NOT_VALID = "Phone number is not valid";
|
||||
public static final String LAST_NAME_CANNOT_BE_BLANK = "Last name cannot be blank";
|
||||
public static final String FIRST_NAME_CANNOT_BE_BLANK = "First name cannot be blank";
|
||||
public static final String PHONE_NUMBER_REGEX = "0([67])\\d{8}";
|
||||
|
||||
private CustomerValidator() {
|
||||
|
||||
}
|
||||
|
||||
public static void validate(CustomerInfo newCustomer) throws NotValidCustomerException {
|
||||
validateFirstName(newCustomer);
|
||||
validateLastName(newCustomer);
|
||||
validatePhoneNumber(newCustomer);
|
||||
}
|
||||
|
||||
private static void validatePhoneNumber(CustomerInfo newCustomer)
|
||||
throws NotValidCustomerException {
|
||||
if (newCustomer.phoneNumber().isBlank()) {
|
||||
throw new NotValidCustomerException("Phone number cannot be blank");
|
||||
}
|
||||
if (!newCustomer.phoneNumber().matches(PHONE_NUMBER_REGEX)) {
|
||||
throw new NotValidCustomerException(PHONE_NUMBER_IS_NOT_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateLastName(CustomerInfo newCustomer) throws NotValidCustomerException {
|
||||
if (newCustomer.lastName().isBlank()) {
|
||||
throw new NotValidCustomerException(LAST_NAME_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateFirstName(CustomerInfo newCustomer) throws NotValidCustomerException {
|
||||
if (newCustomer.firstName().isBlank()) {
|
||||
throw new NotValidCustomerException(FIRST_NAME_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user