From 5b7e940b917ebf42d6b8e9990e621f70f2a3bfff Mon Sep 17 00:00:00 2001 From: AichaELYamlahi <aicha.elyamalhi@marketingconfort.com> Date: Thu, 29 Aug 2024 10:20:48 +0100 Subject: [PATCH 1/9] get all sessions --- .../mydressin/controllers/SaleSessionController.java | 6 +++++- .../mydressin/services/SaleSessionService.java | 2 ++ .../mydressin/services/SaleSessionServiceImp.java | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java index 2fe0704..95d1125 100644 --- a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java +++ b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java @@ -23,5 +23,9 @@ public class SaleSessionController { return new ResponseEntity<>(createdSaleSession, HttpStatus.CREATED); } - + @GetMapping("/saleSessions") + public ResponseEntity<List<SaleSessionDTO>> getAllSaleSessions() { + List<SaleSessionDTO> saleSessions = saleSessionService.getAllSaleSessions(); + return new ResponseEntity<>(saleSessions, HttpStatus.OK); + } } diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java index ee56517..3176ceb 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java @@ -7,4 +7,6 @@ import java.util.List; public interface SaleSessionService { SaleSessionDTO createSaleSession(SaleSession saleSession); + List<SaleSessionDTO> getAllSaleSessions(); + } diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java index ebda438..2e82eba 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java @@ -21,6 +21,12 @@ public class SaleSessionServiceImp implements SaleSessionService{ SaleSession savedSaleSession = saleSessionRepository.save(saleSession); return saleSessionMapper.toDTO(savedSaleSession); } + @Override + public List<SaleSessionDTO> getAllSaleSessions() { + return saleSessionRepository.findAll().stream() + .map(saleSessionMapper::toDTO) + .collect(Collectors.toList()); + } } -- GitLab From bb0a19c410e54c5e960bf8b8fbd2442bc036fcdb Mon Sep 17 00:00:00 2001 From: AichaELYamlahi <aicha.elyamalhi@marketingconfort.com> Date: Fri, 30 Aug 2024 17:24:46 +0100 Subject: [PATCH 2/9] get list of sessions --- pom.xml | 2 +- .../mydressin/controllers/CartController.java | 13 ++++++----- .../dtos/ProductStockRequestDTO.java | 17 ++++++++++++++ .../mydressin/services/CartServiceImp.java | 18 +++++++-------- .../services/ItemCartServiceImp.java | 9 ++++++-- .../services/ProductStockService.java | 5 +++-- .../services/ProductStockServiceImp.java | 22 ++++++++++++++----- .../services/SaleSessionService.java | 1 + .../services/SaleSessionServiceImp.java | 3 +++ src/main/resources/application.yml | 2 +- 10 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java diff --git a/pom.xml b/pom.xml index ce77680..477c864 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ <dependency> <groupId>com.marketingconfort</groupId> <artifactId>mydressin-common</artifactId> - <version>1.0.60-SNAPSHOT-test3</version> + <version>1.0.66-RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> diff --git a/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java b/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java index acc6f83..0a0be10 100644 --- a/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java +++ b/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java @@ -3,6 +3,7 @@ package com.marketingconfort.mydressin.controllers; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; import com.marketingconfort.mydressin.dtos.CartDTO; import com.marketingconfort.mydressin.dtos.LocalStorageRequest; +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.exceptions.CartNotFoundException; import com.marketingconfort.mydressin.services.CartService; import com.marketingconfort.mydressin.services.ProductStockService; @@ -23,15 +24,15 @@ public class CartController { private final ProductStockService productStockService; private final CartService cartService; - @PostMapping("/check-stock") - public ResponseEntity<List<ProductStockDTO>> checkProductStockForIds(@RequestBody Map<Long, Long> productQuantities) { - List<ProductStockDTO> productStockDTOs = productStockService.checkProductStockForIds(productQuantities); + @PostMapping("/checkProductsStock") + public ResponseEntity<List<ProductStockDTO>> checkProductStockForIds(@RequestBody List<ProductStockRequestDTO> productStockRequestDTOS) { + List<ProductStockDTO> productStockDTOs = productStockService.checkProductStockForIds(productStockRequestDTOS); return ResponseEntity.ok(productStockDTOs); } - @GetMapping("/check-stock/{id}/{quantityToAdd}") - public ResponseEntity<ProductStockDTO> checkStock(@PathVariable Long id, @PathVariable Long quantityToAdd) { - ProductStockDTO productStockDTO = productStockService.checkStock(id, quantityToAdd); + @PostMapping("/checkProductStock") + public ResponseEntity<ProductStockDTO> checkStock(@RequestBody ProductStockRequestDTO productStockRequestDTO) { + ProductStockDTO productStockDTO = productStockService.checkStock(productStockRequestDTO); return ResponseEntity.ok(productStockDTO); } diff --git a/src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java b/src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java new file mode 100644 index 0000000..bfbeae9 --- /dev/null +++ b/src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java @@ -0,0 +1,17 @@ +package com.marketingconfort.mydressin.dtos; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@AllArgsConstructor +@Getter +@Setter +@NoArgsConstructor +public class ProductStockRequestDTO { + private Long productId; + private Long quantity; + private String source; +} diff --git a/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java index f3a7bd4..7cd33f9 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java @@ -7,6 +7,7 @@ import com.marketingconfort.mydressin.common.cart.models.Cart; import com.marketingconfort.mydressin.common.cart.models.ItemCart; import com.marketingconfort.mydressin.dtos.CartDTO; import com.marketingconfort.mydressin.dtos.ItemCartDTO; +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.exceptions.CartNotFoundException; import com.marketingconfort.mydressin.mappers.CartMapper; import com.marketingconfort.mydressin.repositories.CartRepository; @@ -66,15 +67,15 @@ public class CartServiceImp implements CartService { return null; } List<String> messages = new ArrayList<>(); - Map<Long, Long> productQuantities = collectProductQuantities(cart); + List<ProductStockRequestDTO> productStockRequestDTOS= collectProductQuantities(cart); - if (productQuantities != null) { + if (productStockRequestDTOS!= null) { // Create a map for quick lookup of item carts by product ID Map<Long, ItemCart> itemCartMap = cart.getItems().stream() .collect(Collectors.toMap(ItemCart::getProductId, itemCart -> itemCart)); // Check stock for all products in the cart - List<ProductStockDTO> stockResults = productStockService.checkProductStockForIds(productQuantities); + List<ProductStockDTO> stockResults = productStockService.checkProductStockForIds(productStockRequestDTOS); stockResults .forEach(stockResult -> { @@ -96,18 +97,17 @@ public class CartServiceImp implements CartService { return cart; } - private Map<Long, Long> collectProductQuantities(Cart cart) { + private List<ProductStockRequestDTO> collectProductQuantities(Cart cart) { if (cart.getItems() == null) { - return null; + return Collections.emptyList(); } return cart.getItems().stream() - .collect(Collectors.toMap( - ItemCart::getProductId, - ItemCart::getQuantity - )); + .map(item -> new ProductStockRequestDTO(item.getProductId(), item.getQuantity(), item.getSource())) + .collect(Collectors.toList()); } + @Override public CartDTO reload(Long clientId) { Cart cart = cartRepository.findByClientIdAndStatus(clientId, Status.open) diff --git a/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java index 4b7c6ac..9e702f4 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java @@ -3,11 +3,13 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; + import com.marketingconfort.mydressin.common.cart.models.Cart; import com.marketingconfort.mydressin.common.cart.models.ItemCart; import com.marketingconfort.mydressin.dtos.CartDTO; +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.exceptions.ItemNotFoundException; import com.marketingconfort.mydressin.mappers.CartMapper; @@ -66,7 +68,8 @@ public class ItemCartServiceImp implements ItemCartService { } cart.getInfos().clear(); cart = cartService.updateCartWithStockAvailability(cart); - ProductStockDTO stockResult = productStockService.checkStock(productId, quantity); + ProductStockRequestDTO productStockRequestDTO=new ProductStockRequestDTO(productId,quantity, "WEB"); + ProductStockDTO stockResult = productStockService.checkStock(productStockRequestDTO); if (!stockResult.isAvailable()) { cart.getInfos().add(stockResult.getProductDTO().getName() + " non disponible"); @@ -122,7 +125,9 @@ public class ItemCartServiceImp implements ItemCartService { } else { if (quantity > itemCart.getQuantity()) { Long quantityToAdd = quantity - itemCart.getQuantity(); - ProductStockDTO stockResult = productStockService.checkStock(itemCart.getProductId(), quantityToAdd); + ProductStockRequestDTO productStockRequestDTO=new ProductStockRequestDTO(itemCart.getProductId(),quantityToAdd, "WEB"); + + ProductStockDTO stockResult = productStockService.checkStock(productStockRequestDTO); if (!stockResult.isAvailable()) { Message = "Le produit" + " " + stockResult.getProductDTO().getName() + " " + "n'est pas disponible en quantité suffisante."; diff --git a/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java b/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java index 49f2cd6..32ed128 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java @@ -1,12 +1,13 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import java.util.List; import java.util.Map; public interface ProductStockService { - List<ProductStockDTO> checkProductStockForIds(Map<Long, Long> productQuantities); - ProductStockDTO checkStock(Long id, Long quantityToAdd); + List<ProductStockDTO> checkProductStockForIds(List<ProductStockRequestDTO> productStockRequestDTOS); + ProductStockDTO checkStock(ProductStockRequestDTO productStockRequestDTO); } diff --git a/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java index 004503d..fdfb856 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java @@ -1,6 +1,10 @@ package com.marketingconfort.mydressin.services; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; + +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; @@ -24,19 +28,25 @@ public class ProductStockServiceImp implements ProductStockService { } @Override - public List<ProductStockDTO> checkProductStockForIds(Map<Long, Long> productQuantities) { + public List<ProductStockDTO> checkProductStockForIds(List<ProductStockRequestDTO> productStockRequestDTOS) { ResponseEntity<List<ProductStockDTO>> responseEntity = restTemplate.exchange( - productStockServiceUrl, + productStockServiceUrl+ "/checkProductsStock", HttpMethod.POST, - new HttpEntity<>(productQuantities), + new HttpEntity<>(productStockRequestDTOS), new ParameterizedTypeReference<>() {} ); return responseEntity.getBody(); } @Override - public ProductStockDTO checkStock(Long id, Long quantityToAdd) { - String url = productStockServiceUrl + "/" + id + "/" + quantityToAdd; - return restTemplate.getForObject(url, ProductStockDTO.class); + public ProductStockDTO checkStock(ProductStockRequestDTO productStockRequestDTO) { + String url = productStockServiceUrl + "/checkProductStock"; + try { + ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, productStockRequestDTO, String.class); + return new ObjectMapper().readValue(responseEntity.getBody(), ProductStockDTO.class); + } catch (JsonProcessingException e) { + throw new RuntimeException("Failed to parse JSON response", e); + } } + } diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java index 3176ceb..e69be2d 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java @@ -1,6 +1,7 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.models.SaleSession; + import com.marketingconfort.mydressin.dtos.SaleSessionDTO; import java.util.List; diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java index 2e82eba..3c600d0 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java @@ -1,12 +1,14 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.models.SaleSession; + import com.marketingconfort.mydressin.dtos.SaleSessionDTO; import com.marketingconfort.mydressin.mappers.SaleSessionMapper; import com.marketingconfort.mydressin.repositories.SaleSessionRepository; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; + import java.util.List; import java.util.stream.Collectors; @@ -16,6 +18,7 @@ public class SaleSessionServiceImp implements SaleSessionService{ private final SaleSessionRepository saleSessionRepository; private final SaleSessionMapper saleSessionMapper; + @Override public SaleSessionDTO createSaleSession(SaleSession saleSession) { SaleSession savedSaleSession = saleSessionRepository.save(saleSession); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 526375a..ab90609 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -33,7 +33,7 @@ spring: product: stock: service: - url: https://mydressin-stock-management-service.mc-test.xyz/api/stock/products/checkStock + url: http://localhost:8080/api/stock/products promoCode: service: -- GitLab From 1a05df51edfac424be34933900abfc05e90d978c Mon Sep 17 00:00:00 2001 From: AichaELYamlahi <aicha.elyamalhi@marketingconfort.com> Date: Fri, 30 Aug 2024 17:39:32 +0100 Subject: [PATCH 3/9] resolve changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5204e35..ce76250 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Added - New features that have been added. - MYD-168/create sale session +- MYD-173/get all sessions ### Changed - Changes in existing functionality. -- GitLab From b59acd31db229ea5436d869afec7691aafddda65 Mon Sep 17 00:00:00 2001 From: AichaELYamlahi <aicha.elyamalhi@marketingconfort.com> Date: Thu, 29 Aug 2024 10:20:48 +0100 Subject: [PATCH 4/9] get all sessions --- .../mydressin/controllers/SaleSessionController.java | 6 +++++- .../mydressin/services/SaleSessionService.java | 2 ++ .../mydressin/services/SaleSessionServiceImp.java | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java index 2fe0704..95d1125 100644 --- a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java +++ b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java @@ -23,5 +23,9 @@ public class SaleSessionController { return new ResponseEntity<>(createdSaleSession, HttpStatus.CREATED); } - + @GetMapping("/saleSessions") + public ResponseEntity<List<SaleSessionDTO>> getAllSaleSessions() { + List<SaleSessionDTO> saleSessions = saleSessionService.getAllSaleSessions(); + return new ResponseEntity<>(saleSessions, HttpStatus.OK); + } } diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java index ee56517..3176ceb 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java @@ -7,4 +7,6 @@ import java.util.List; public interface SaleSessionService { SaleSessionDTO createSaleSession(SaleSession saleSession); + List<SaleSessionDTO> getAllSaleSessions(); + } diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java index ebda438..2e82eba 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java @@ -21,6 +21,12 @@ public class SaleSessionServiceImp implements SaleSessionService{ SaleSession savedSaleSession = saleSessionRepository.save(saleSession); return saleSessionMapper.toDTO(savedSaleSession); } + @Override + public List<SaleSessionDTO> getAllSaleSessions() { + return saleSessionRepository.findAll().stream() + .map(saleSessionMapper::toDTO) + .collect(Collectors.toList()); + } } -- GitLab From 46441b34af4c162ec2bf202bf5658ad113239db2 Mon Sep 17 00:00:00 2001 From: AichaELYamlahi <aicha.elyamalhi@marketingconfort.com> Date: Fri, 30 Aug 2024 17:24:46 +0100 Subject: [PATCH 5/9] get list of sessions --- pom.xml | 2 +- .../mydressin/controllers/CartController.java | 13 ++++++----- .../dtos/ProductStockRequestDTO.java | 17 ++++++++++++++ .../mydressin/services/CartServiceImp.java | 18 +++++++-------- .../services/ItemCartServiceImp.java | 9 ++++++-- .../services/ProductStockService.java | 5 +++-- .../services/ProductStockServiceImp.java | 22 ++++++++++++++----- .../services/SaleSessionService.java | 1 + .../services/SaleSessionServiceImp.java | 3 +++ src/main/resources/application.yml | 2 +- 10 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java diff --git a/pom.xml b/pom.xml index ce77680..477c864 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ <dependency> <groupId>com.marketingconfort</groupId> <artifactId>mydressin-common</artifactId> - <version>1.0.60-SNAPSHOT-test3</version> + <version>1.0.66-RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> diff --git a/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java b/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java index acc6f83..0a0be10 100644 --- a/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java +++ b/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java @@ -3,6 +3,7 @@ package com.marketingconfort.mydressin.controllers; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; import com.marketingconfort.mydressin.dtos.CartDTO; import com.marketingconfort.mydressin.dtos.LocalStorageRequest; +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.exceptions.CartNotFoundException; import com.marketingconfort.mydressin.services.CartService; import com.marketingconfort.mydressin.services.ProductStockService; @@ -23,15 +24,15 @@ public class CartController { private final ProductStockService productStockService; private final CartService cartService; - @PostMapping("/check-stock") - public ResponseEntity<List<ProductStockDTO>> checkProductStockForIds(@RequestBody Map<Long, Long> productQuantities) { - List<ProductStockDTO> productStockDTOs = productStockService.checkProductStockForIds(productQuantities); + @PostMapping("/checkProductsStock") + public ResponseEntity<List<ProductStockDTO>> checkProductStockForIds(@RequestBody List<ProductStockRequestDTO> productStockRequestDTOS) { + List<ProductStockDTO> productStockDTOs = productStockService.checkProductStockForIds(productStockRequestDTOS); return ResponseEntity.ok(productStockDTOs); } - @GetMapping("/check-stock/{id}/{quantityToAdd}") - public ResponseEntity<ProductStockDTO> checkStock(@PathVariable Long id, @PathVariable Long quantityToAdd) { - ProductStockDTO productStockDTO = productStockService.checkStock(id, quantityToAdd); + @PostMapping("/checkProductStock") + public ResponseEntity<ProductStockDTO> checkStock(@RequestBody ProductStockRequestDTO productStockRequestDTO) { + ProductStockDTO productStockDTO = productStockService.checkStock(productStockRequestDTO); return ResponseEntity.ok(productStockDTO); } diff --git a/src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java b/src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java new file mode 100644 index 0000000..bfbeae9 --- /dev/null +++ b/src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java @@ -0,0 +1,17 @@ +package com.marketingconfort.mydressin.dtos; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@AllArgsConstructor +@Getter +@Setter +@NoArgsConstructor +public class ProductStockRequestDTO { + private Long productId; + private Long quantity; + private String source; +} diff --git a/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java index f3a7bd4..7cd33f9 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java @@ -7,6 +7,7 @@ import com.marketingconfort.mydressin.common.cart.models.Cart; import com.marketingconfort.mydressin.common.cart.models.ItemCart; import com.marketingconfort.mydressin.dtos.CartDTO; import com.marketingconfort.mydressin.dtos.ItemCartDTO; +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.exceptions.CartNotFoundException; import com.marketingconfort.mydressin.mappers.CartMapper; import com.marketingconfort.mydressin.repositories.CartRepository; @@ -66,15 +67,15 @@ public class CartServiceImp implements CartService { return null; } List<String> messages = new ArrayList<>(); - Map<Long, Long> productQuantities = collectProductQuantities(cart); + List<ProductStockRequestDTO> productStockRequestDTOS= collectProductQuantities(cart); - if (productQuantities != null) { + if (productStockRequestDTOS!= null) { // Create a map for quick lookup of item carts by product ID Map<Long, ItemCart> itemCartMap = cart.getItems().stream() .collect(Collectors.toMap(ItemCart::getProductId, itemCart -> itemCart)); // Check stock for all products in the cart - List<ProductStockDTO> stockResults = productStockService.checkProductStockForIds(productQuantities); + List<ProductStockDTO> stockResults = productStockService.checkProductStockForIds(productStockRequestDTOS); stockResults .forEach(stockResult -> { @@ -96,18 +97,17 @@ public class CartServiceImp implements CartService { return cart; } - private Map<Long, Long> collectProductQuantities(Cart cart) { + private List<ProductStockRequestDTO> collectProductQuantities(Cart cart) { if (cart.getItems() == null) { - return null; + return Collections.emptyList(); } return cart.getItems().stream() - .collect(Collectors.toMap( - ItemCart::getProductId, - ItemCart::getQuantity - )); + .map(item -> new ProductStockRequestDTO(item.getProductId(), item.getQuantity(), item.getSource())) + .collect(Collectors.toList()); } + @Override public CartDTO reload(Long clientId) { Cart cart = cartRepository.findByClientIdAndStatus(clientId, Status.open) diff --git a/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java index 4b7c6ac..9e702f4 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java @@ -3,11 +3,13 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; + import com.marketingconfort.mydressin.common.cart.models.Cart; import com.marketingconfort.mydressin.common.cart.models.ItemCart; import com.marketingconfort.mydressin.dtos.CartDTO; +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.exceptions.ItemNotFoundException; import com.marketingconfort.mydressin.mappers.CartMapper; @@ -66,7 +68,8 @@ public class ItemCartServiceImp implements ItemCartService { } cart.getInfos().clear(); cart = cartService.updateCartWithStockAvailability(cart); - ProductStockDTO stockResult = productStockService.checkStock(productId, quantity); + ProductStockRequestDTO productStockRequestDTO=new ProductStockRequestDTO(productId,quantity, "WEB"); + ProductStockDTO stockResult = productStockService.checkStock(productStockRequestDTO); if (!stockResult.isAvailable()) { cart.getInfos().add(stockResult.getProductDTO().getName() + " non disponible"); @@ -122,7 +125,9 @@ public class ItemCartServiceImp implements ItemCartService { } else { if (quantity > itemCart.getQuantity()) { Long quantityToAdd = quantity - itemCart.getQuantity(); - ProductStockDTO stockResult = productStockService.checkStock(itemCart.getProductId(), quantityToAdd); + ProductStockRequestDTO productStockRequestDTO=new ProductStockRequestDTO(itemCart.getProductId(),quantityToAdd, "WEB"); + + ProductStockDTO stockResult = productStockService.checkStock(productStockRequestDTO); if (!stockResult.isAvailable()) { Message = "Le produit" + " " + stockResult.getProductDTO().getName() + " " + "n'est pas disponible en quantité suffisante."; diff --git a/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java b/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java index 49f2cd6..32ed128 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java @@ -1,12 +1,13 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import java.util.List; import java.util.Map; public interface ProductStockService { - List<ProductStockDTO> checkProductStockForIds(Map<Long, Long> productQuantities); - ProductStockDTO checkStock(Long id, Long quantityToAdd); + List<ProductStockDTO> checkProductStockForIds(List<ProductStockRequestDTO> productStockRequestDTOS); + ProductStockDTO checkStock(ProductStockRequestDTO productStockRequestDTO); } diff --git a/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java index 004503d..fdfb856 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java @@ -1,6 +1,10 @@ package com.marketingconfort.mydressin.services; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; + +import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; @@ -24,19 +28,25 @@ public class ProductStockServiceImp implements ProductStockService { } @Override - public List<ProductStockDTO> checkProductStockForIds(Map<Long, Long> productQuantities) { + public List<ProductStockDTO> checkProductStockForIds(List<ProductStockRequestDTO> productStockRequestDTOS) { ResponseEntity<List<ProductStockDTO>> responseEntity = restTemplate.exchange( - productStockServiceUrl, + productStockServiceUrl+ "/checkProductsStock", HttpMethod.POST, - new HttpEntity<>(productQuantities), + new HttpEntity<>(productStockRequestDTOS), new ParameterizedTypeReference<>() {} ); return responseEntity.getBody(); } @Override - public ProductStockDTO checkStock(Long id, Long quantityToAdd) { - String url = productStockServiceUrl + "/" + id + "/" + quantityToAdd; - return restTemplate.getForObject(url, ProductStockDTO.class); + public ProductStockDTO checkStock(ProductStockRequestDTO productStockRequestDTO) { + String url = productStockServiceUrl + "/checkProductStock"; + try { + ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, productStockRequestDTO, String.class); + return new ObjectMapper().readValue(responseEntity.getBody(), ProductStockDTO.class); + } catch (JsonProcessingException e) { + throw new RuntimeException("Failed to parse JSON response", e); + } } + } diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java index 3176ceb..e69be2d 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java @@ -1,6 +1,7 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.models.SaleSession; + import com.marketingconfort.mydressin.dtos.SaleSessionDTO; import java.util.List; diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java index 2e82eba..3c600d0 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java @@ -1,12 +1,14 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.models.SaleSession; + import com.marketingconfort.mydressin.dtos.SaleSessionDTO; import com.marketingconfort.mydressin.mappers.SaleSessionMapper; import com.marketingconfort.mydressin.repositories.SaleSessionRepository; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; + import java.util.List; import java.util.stream.Collectors; @@ -16,6 +18,7 @@ public class SaleSessionServiceImp implements SaleSessionService{ private final SaleSessionRepository saleSessionRepository; private final SaleSessionMapper saleSessionMapper; + @Override public SaleSessionDTO createSaleSession(SaleSession saleSession) { SaleSession savedSaleSession = saleSessionRepository.save(saleSession); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 526375a..ab90609 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -33,7 +33,7 @@ spring: product: stock: service: - url: https://mydressin-stock-management-service.mc-test.xyz/api/stock/products/checkStock + url: http://localhost:8080/api/stock/products promoCode: service: -- GitLab From b6cc472c7f4dc5635a6fa26e2d1882376c38486a Mon Sep 17 00:00:00 2001 From: AichaELYamlahi <aicha.elyamalhi@marketingconfort.com> Date: Fri, 30 Aug 2024 17:39:32 +0100 Subject: [PATCH 6/9] resolve changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5204e35..ce76250 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Added - New features that have been added. - MYD-168/create sale session +- MYD-173/get all sessions ### Changed - Changes in existing functionality. -- GitLab From 032d3755c37e0a63f53cf9b0e37d3af4f1336155 Mon Sep 17 00:00:00 2001 From: "aicha.elyamalhi" <aicha.elyamalhi@marketingconfort.com> Date: Mon, 2 Sep 2024 09:39:46 +0000 Subject: [PATCH 7/9] Update pom.xml --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 477c864..fb01af6 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ <dependency> <groupId>com.marketingconfort</groupId> <artifactId>mydressin-common</artifactId> - <version>1.0.66-RELEASE</version> + <version>1.0.67-RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> @@ -54,4 +54,4 @@ </plugin> </plugins> </build> -</project> \ No newline at end of file +</project> -- GitLab From 19f80294d2e96c6c3a630552d945415a78d9d02d Mon Sep 17 00:00:00 2001 From: AichaELYamlahi <aicha.elyamalhi@marketingconfort.com> Date: Mon, 2 Sep 2024 10:58:42 +0100 Subject: [PATCH 8/9] get all sessions --- CHANGELOG.md | 1 + pom.xml | 2 +- .../mydressin/controllers/CartController.java | 3 +- .../controllers/SaleSessionController.java | 5 ++ .../mydressin/dtos/ItemCartDTO.java | 3 +- .../dtos/ProductStockRequestDTO.java | 17 ------ .../mydressin/dtos/SaleSessionDTO.java | 5 +- .../mydressin/dtos/SessionRequest.java | 13 +++++ .../ProductOutOfStockException.java | 7 +++ .../SaleSessionExpiredException.java | 12 ++++ .../repositories/SessionOrderRepository.java | 10 ++++ .../mydressin/services/CartServiceImp.java | 2 +- .../services/ItemCartServiceImp.java | 8 ++- .../services/ProductStockService.java | 3 +- .../services/ProductStockServiceImp.java | 3 +- .../services/SaleSessionService.java | 3 + .../services/SaleSessionServiceImp.java | 58 ++++++++++++++++++- 17 files changed, 123 insertions(+), 32 deletions(-) delete mode 100644 src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java create mode 100644 src/main/java/com/marketingconfort/mydressin/dtos/SessionRequest.java create mode 100644 src/main/java/com/marketingconfort/mydressin/exceptions/ProductOutOfStockException.java create mode 100644 src/main/java/com/marketingconfort/mydressin/exceptions/SaleSessionExpiredException.java create mode 100644 src/main/java/com/marketingconfort/mydressin/repositories/SessionOrderRepository.java diff --git a/CHANGELOG.md b/CHANGELOG.md index ce76250..84023af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Changed - Changes in existing functionality. +- MYD-173/change in existing classes ### Deprecated - Soon-to-be removed features. diff --git a/pom.xml b/pom.xml index 477c864..498f025 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ <dependency> <groupId>com.marketingconfort</groupId> <artifactId>mydressin-common</artifactId> - <version>1.0.66-RELEASE</version> + <version>1.0.67-RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> diff --git a/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java b/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java index 0a0be10..2ec3a28 100644 --- a/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java +++ b/src/main/java/com/marketingconfort/mydressin/controllers/CartController.java @@ -1,9 +1,9 @@ package com.marketingconfort.mydressin.controllers; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; +import com.marketingconfort.mydressin.common.cart.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.dtos.CartDTO; import com.marketingconfort.mydressin.dtos.LocalStorageRequest; -import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.exceptions.CartNotFoundException; import com.marketingconfort.mydressin.services.CartService; import com.marketingconfort.mydressin.services.ProductStockService; @@ -13,7 +13,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; -import java.util.Map; @RestController @RequestMapping(value = "/api/cart", method = RequestMethod.OPTIONS) diff --git a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java index 95d1125..3e6453c 100644 --- a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java +++ b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java @@ -1,7 +1,12 @@ package com.marketingconfort.mydressin.controllers; import com.marketingconfort.mydressin.common.cart.models.SaleSession; +import com.marketingconfort.mydressin.common.cart.models.SessionOrder; import com.marketingconfort.mydressin.dtos.SaleSessionDTO; +import com.marketingconfort.mydressin.dtos.SessionRequest; +import com.marketingconfort.mydressin.exceptions.CartNotFoundException; +import com.marketingconfort.mydressin.exceptions.ProductOutOfStockException; +import com.marketingconfort.mydressin.exceptions.SaleSessionNotFoundException; import com.marketingconfort.mydressin.services.SaleSessionService; import lombok.AllArgsConstructor; import org.springframework.http.HttpStatus; diff --git a/src/main/java/com/marketingconfort/mydressin/dtos/ItemCartDTO.java b/src/main/java/com/marketingconfort/mydressin/dtos/ItemCartDTO.java index 6eeae88..27c3388 100644 --- a/src/main/java/com/marketingconfort/mydressin/dtos/ItemCartDTO.java +++ b/src/main/java/com/marketingconfort/mydressin/dtos/ItemCartDTO.java @@ -3,6 +3,7 @@ package com.marketingconfort.mydressin.dtos; import com.marketingconfort.mydressin.common.cart.dtos.ProductCartDTO; +import com.marketingconfort.mydressin.common.cart.enumurations.ItemSource; import com.marketingconfort.mydressin.common.stockmanagement.enumurations.ProductType; import lombok.Getter; import lombok.Setter; @@ -17,5 +18,5 @@ public class ItemCartDTO { private Long productId; private double totalPrice; private ProductCartDTO product; - private String source; + private ItemSource source; } diff --git a/src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java b/src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java deleted file mode 100644 index bfbeae9..0000000 --- a/src/main/java/com/marketingconfort/mydressin/dtos/ProductStockRequestDTO.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.marketingconfort.mydressin.dtos; - - -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -@AllArgsConstructor -@Getter -@Setter -@NoArgsConstructor -public class ProductStockRequestDTO { - private Long productId; - private Long quantity; - private String source; -} diff --git a/src/main/java/com/marketingconfort/mydressin/dtos/SaleSessionDTO.java b/src/main/java/com/marketingconfort/mydressin/dtos/SaleSessionDTO.java index c4c6c5f..c66bdf2 100644 --- a/src/main/java/com/marketingconfort/mydressin/dtos/SaleSessionDTO.java +++ b/src/main/java/com/marketingconfort/mydressin/dtos/SaleSessionDTO.java @@ -14,7 +14,10 @@ public class SaleSessionDTO { private LocalDateTime lastModification; private LiveType type; private long live; - private long paidProductNumber; + private long paidProductsNumber; private long clientsNumber; + private long deletedProductsNumber; + private long consultedProductsNumber; + } diff --git a/src/main/java/com/marketingconfort/mydressin/dtos/SessionRequest.java b/src/main/java/com/marketingconfort/mydressin/dtos/SessionRequest.java new file mode 100644 index 0000000..b0a1764 --- /dev/null +++ b/src/main/java/com/marketingconfort/mydressin/dtos/SessionRequest.java @@ -0,0 +1,13 @@ +package com.marketingconfort.mydressin.dtos; + +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class SessionRequest { + private Long clientId; + private Long productId; + private Long sessionId; + private Long quantity; +} diff --git a/src/main/java/com/marketingconfort/mydressin/exceptions/ProductOutOfStockException.java b/src/main/java/com/marketingconfort/mydressin/exceptions/ProductOutOfStockException.java new file mode 100644 index 0000000..d0ba084 --- /dev/null +++ b/src/main/java/com/marketingconfort/mydressin/exceptions/ProductOutOfStockException.java @@ -0,0 +1,7 @@ +package com.marketingconfort.mydressin.exceptions; + +public class ProductOutOfStockException extends RuntimeException { + public ProductOutOfStockException(String message) { + super(message); + } +} diff --git a/src/main/java/com/marketingconfort/mydressin/exceptions/SaleSessionExpiredException.java b/src/main/java/com/marketingconfort/mydressin/exceptions/SaleSessionExpiredException.java new file mode 100644 index 0000000..f40f357 --- /dev/null +++ b/src/main/java/com/marketingconfort/mydressin/exceptions/SaleSessionExpiredException.java @@ -0,0 +1,12 @@ +package com.marketingconfort.mydressin.exceptions; + +public class SaleSessionExpiredException extends RuntimeException { + + public SaleSessionExpiredException(String message) { + super(message); + } + + public SaleSessionExpiredException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/marketingconfort/mydressin/repositories/SessionOrderRepository.java b/src/main/java/com/marketingconfort/mydressin/repositories/SessionOrderRepository.java new file mode 100644 index 0000000..27b40b0 --- /dev/null +++ b/src/main/java/com/marketingconfort/mydressin/repositories/SessionOrderRepository.java @@ -0,0 +1,10 @@ +package com.marketingconfort.mydressin.repositories; + +import com.marketingconfort.mydressin.common.cart.models.SessionOrder; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface SessionOrderRepository extends JpaRepository<SessionOrder,Long> { + +} diff --git a/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java index 7cd33f9..a5c8bdb 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/CartServiceImp.java @@ -2,12 +2,12 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; +import com.marketingconfort.mydressin.common.cart.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.common.cart.enumurations.Status; import com.marketingconfort.mydressin.common.cart.models.Cart; import com.marketingconfort.mydressin.common.cart.models.ItemCart; import com.marketingconfort.mydressin.dtos.CartDTO; import com.marketingconfort.mydressin.dtos.ItemCartDTO; -import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.exceptions.CartNotFoundException; import com.marketingconfort.mydressin.mappers.CartMapper; import com.marketingconfort.mydressin.repositories.CartRepository; diff --git a/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java index 9e702f4..5e60f82 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/ItemCartServiceImp.java @@ -4,12 +4,13 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; +import com.marketingconfort.mydressin.common.cart.dtos.ProductStockRequestDTO; +import com.marketingconfort.mydressin.common.cart.enumurations.ItemSource; import com.marketingconfort.mydressin.common.cart.models.Cart; import com.marketingconfort.mydressin.common.cart.models.ItemCart; import com.marketingconfort.mydressin.dtos.CartDTO; -import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; import com.marketingconfort.mydressin.exceptions.ItemNotFoundException; import com.marketingconfort.mydressin.mappers.CartMapper; @@ -68,7 +69,7 @@ public class ItemCartServiceImp implements ItemCartService { } cart.getInfos().clear(); cart = cartService.updateCartWithStockAvailability(cart); - ProductStockRequestDTO productStockRequestDTO=new ProductStockRequestDTO(productId,quantity, "WEB"); + ProductStockRequestDTO productStockRequestDTO=new ProductStockRequestDTO(productId,quantity, ItemSource.WEB); ProductStockDTO stockResult = productStockService.checkStock(productStockRequestDTO); if (!stockResult.isAvailable()) { @@ -125,7 +126,8 @@ public class ItemCartServiceImp implements ItemCartService { } else { if (quantity > itemCart.getQuantity()) { Long quantityToAdd = quantity - itemCart.getQuantity(); - ProductStockRequestDTO productStockRequestDTO=new ProductStockRequestDTO(itemCart.getProductId(),quantityToAdd, "WEB"); + + ProductStockRequestDTO productStockRequestDTO=new ProductStockRequestDTO(itemCart.getProductId(),quantityToAdd, ItemSource.WEB); ProductStockDTO stockResult = productStockService.checkStock(productStockRequestDTO); diff --git a/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java b/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java index 32ed128..8f1ff46 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/ProductStockService.java @@ -1,10 +1,9 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; -import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; +import com.marketingconfort.mydressin.common.cart.dtos.ProductStockRequestDTO; import java.util.List; -import java.util.Map; public interface ProductStockService { List<ProductStockDTO> checkProductStockForIds(List<ProductStockRequestDTO> productStockRequestDTOS); diff --git a/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java index fdfb856..6d141bc 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/ProductStockServiceImp.java @@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; -import com.marketingconfort.mydressin.dtos.ProductStockRequestDTO; +import com.marketingconfort.mydressin.common.cart.dtos.ProductStockRequestDTO; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; @@ -14,7 +14,6 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.List; -import java.util.Map; @Service public class ProductStockServiceImp implements ProductStockService { diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java index e69be2d..9d4e711 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java @@ -2,7 +2,10 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.common.cart.models.SaleSession; +import com.marketingconfort.mydressin.common.cart.models.SessionOrder; import com.marketingconfort.mydressin.dtos.SaleSessionDTO; +import com.marketingconfort.mydressin.dtos.SessionRequest; +import com.marketingconfort.mydressin.exceptions.SaleSessionExpiredException; import java.util.List; diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java index 3c600d0..8a21a17 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionServiceImp.java @@ -1,22 +1,43 @@ package com.marketingconfort.mydressin.services; +import com.marketingconfort.mydressin.common.cart.dtos.ProductStockDTO; +import com.marketingconfort.mydressin.common.cart.dtos.ProductStockRequestDTO; +import com.marketingconfort.mydressin.common.cart.enumurations.ItemCartStatus; +import com.marketingconfort.mydressin.common.cart.enumurations.ItemSource; +import com.marketingconfort.mydressin.common.cart.enumurations.OrderStatus; +import com.marketingconfort.mydressin.common.cart.models.Cart; +import com.marketingconfort.mydressin.common.cart.models.ItemCart; import com.marketingconfort.mydressin.common.cart.models.SaleSession; +import com.marketingconfort.mydressin.common.cart.models.SessionOrder; import com.marketingconfort.mydressin.dtos.SaleSessionDTO; +import com.marketingconfort.mydressin.dtos.SessionRequest; +import com.marketingconfort.mydressin.exceptions.CartNotFoundException; +import com.marketingconfort.mydressin.exceptions.ProductOutOfStockException; +import com.marketingconfort.mydressin.exceptions.SaleSessionExpiredException; +import com.marketingconfort.mydressin.exceptions.SaleSessionNotFoundException; import com.marketingconfort.mydressin.mappers.SaleSessionMapper; import com.marketingconfort.mydressin.repositories.SaleSessionRepository; +import com.marketingconfort.mydressin.repositories.SessionOrderRepository; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; @AllArgsConstructor @Service -public class SaleSessionServiceImp implements SaleSessionService{ +public class SaleSessionServiceImp implements SaleSessionService { private final SaleSessionRepository saleSessionRepository; private final SaleSessionMapper saleSessionMapper; + private final ItemCartService itemCartService; + private final CartService cartService; + private final ProductStockService productStockService; + private final SessionOrderRepository orderRepository; @Override @@ -24,12 +45,45 @@ public class SaleSessionServiceImp implements SaleSessionService{ SaleSession savedSaleSession = saleSessionRepository.save(saleSession); return saleSessionMapper.toDTO(savedSaleSession); } + @Override public List<SaleSessionDTO> getAllSaleSessions() { return saleSessionRepository.findAll().stream() - .map(saleSessionMapper::toDTO) + .map(this::getSessionStatistics) .collect(Collectors.toList()); } + public SaleSessionDTO getSessionStatistics(SaleSession saleSession) { + List<SessionOrder> orders = saleSession.getOrders(); + + + Set<Long> uniqueClientIds = orders.stream() + .map(SessionOrder::getClientId) + .collect(Collectors.toSet()); + int numberOfClients = uniqueClientIds.size(); + + + long numberOfPaidProducts = orders.stream() + .filter(order -> order.getItemCart().getStatus() == ItemCartStatus.PAYER) + .mapToLong(order -> order.getItemCart().getQuantity()) + .sum(); + long numberOfDeletedProducts = orders.stream() + .filter(order -> order.getItemCart().getStatus() == ItemCartStatus.SUPPRIMER) + .mapToLong(order -> order.getItemCart().getQuantity()) + .sum(); + + long numberOfConsultedProducts = orders.stream() + .filter(order -> order.getItemCart().getStatus() == ItemCartStatus.CONSULTER) + .mapToLong(order -> order.getItemCart().getQuantity()) + .sum(); + + SaleSessionDTO saleSessionDTO=saleSessionMapper.toDTO(saleSession); + saleSessionDTO.setClientsNumber(numberOfClients); + saleSessionDTO.setPaidProductsNumber(numberOfPaidProducts); + saleSessionDTO.setDeletedProductsNumber(numberOfDeletedProducts); + saleSessionDTO.setConsultedProductsNumber(numberOfConsultedProducts); + + return saleSessionDTO; + } } -- GitLab From 8f79f2355017a2a2c580168cc485898f4b7e4b05 Mon Sep 17 00:00:00 2001 From: AichaELYamlahi <aicha.elyamalhi@marketingconfort.com> Date: Mon, 2 Sep 2024 11:06:13 +0100 Subject: [PATCH 9/9] get all sessions --- .../com/marketingconfort/mydressin/mappers/ItemCartMapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/marketingconfort/mydressin/mappers/ItemCartMapper.java b/src/main/java/com/marketingconfort/mydressin/mappers/ItemCartMapper.java index 3eef6e6..f08337c 100644 --- a/src/main/java/com/marketingconfort/mydressin/mappers/ItemCartMapper.java +++ b/src/main/java/com/marketingconfort/mydressin/mappers/ItemCartMapper.java @@ -15,7 +15,7 @@ public class ItemCartMapper { itemCartDTO.setId(itemCart.getId()); itemCartDTO.setProductId(itemCart.getProductId()); itemCartDTO.setQuantity(itemCart.getQuantity()); - itemCartDTO.setSource(itemCart.getSource()); + //itemCartDTO.setSource(itemCart.getSource()); if (itemCart.getProduct() != null) { itemCartDTO.setProduct( itemCart.getProduct()); } -- GitLab