From f6996aea70fae200848dd1d51cc9920ba3148278 Mon Sep 17 00:00:00 2001 From: Tim <47184194+imgde@users.noreply.github.com> Date: Sun, 15 Jun 2025 20:45:56 +0200 Subject: [PATCH] Spin x,y axis on CatMonthModel --- .../controller/StatisticsController.java | 14 +++++------ ...onthlyCatModel.java => CatMonthModel.java} | 5 ++-- .../webshop/service/StatisticsService.java | 6 ++--- .../service/impl/StatisticsServiceImpl.java | 24 +++++++++---------- 4 files changed, 23 insertions(+), 26 deletions(-) rename 00-backend/src/main/java/de/htwsaar/webshop/model/{MonthlyCatModel.java => CatMonthModel.java} (64%) diff --git a/00-backend/src/main/java/de/htwsaar/webshop/controller/StatisticsController.java b/00-backend/src/main/java/de/htwsaar/webshop/controller/StatisticsController.java index 8dfddd1..bc68251 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/controller/StatisticsController.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/StatisticsController.java @@ -1,6 +1,6 @@ package de.htwsaar.webshop.controller; -import de.htwsaar.webshop.model.MonthlyCatModel; +import de.htwsaar.webshop.model.CatMonthModel; import de.htwsaar.webshop.service.SessionService; import de.htwsaar.webshop.service.StatisticsService; import jakarta.servlet.http.HttpServletRequest; @@ -32,9 +32,9 @@ public class StatisticsController { } @RequestMapping(value = STATISTICS_VOLUME, method = RequestMethod.GET, produces = "application/json") - public ResponseEntity> getMonthlySalesVolume(HttpServletRequest request, - @RequestParam(value = PARAM_SESSION) UUID session, - @RequestParam(value = PARAM_EMAIL) String email) { + public ResponseEntity> getMonthlySalesVolume(HttpServletRequest request, + @RequestParam(value = PARAM_SESSION) UUID session, + @RequestParam(value = PARAM_EMAIL) String email) { logRequest(request); if (!sessionService.isAdmin(session, email)) { log.warn("Invalid session requesting Admin {}", session); @@ -44,9 +44,9 @@ public class StatisticsController { } @RequestMapping(value = STATISTICS_REVENUE, method = RequestMethod.GET, produces = "application/json") - public ResponseEntity> getMonthlyRevenue(HttpServletRequest request, - @RequestParam(value = PARAM_SESSION) UUID token, - @RequestParam(value = PARAM_EMAIL) String email) { + public ResponseEntity> getMonthlyRevenue(HttpServletRequest request, + @RequestParam(value = PARAM_SESSION) UUID token, + @RequestParam(value = PARAM_EMAIL) String email) { logRequest(request); if (!sessionService.isAdmin(token, email)) { log.warn("Invalid session requesting Admin {}", token); diff --git a/00-backend/src/main/java/de/htwsaar/webshop/model/MonthlyCatModel.java b/00-backend/src/main/java/de/htwsaar/webshop/model/CatMonthModel.java similarity index 64% rename from 00-backend/src/main/java/de/htwsaar/webshop/model/MonthlyCatModel.java rename to 00-backend/src/main/java/de/htwsaar/webshop/model/CatMonthModel.java index 1ada3cf..22b0c06 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/model/MonthlyCatModel.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/model/CatMonthModel.java @@ -9,7 +9,6 @@ import java.util.Map; @Getter @Setter @AllArgsConstructor -public class MonthlyCatModel { - Map> monthCategoryMap; - +public class CatMonthModel { + Map> catMonthMap; } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/StatisticsService.java b/00-backend/src/main/java/de/htwsaar/webshop/service/StatisticsService.java index 50ee146..1801fbb 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/StatisticsService.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/StatisticsService.java @@ -1,10 +1,10 @@ package de.htwsaar.webshop.service; -import de.htwsaar.webshop.model.MonthlyCatModel; +import de.htwsaar.webshop.model.CatMonthModel; public interface StatisticsService { - MonthlyCatModel getSalesVolume(); + CatMonthModel getSalesVolume(); - MonthlyCatModel getSalesRevenue(); + CatMonthModel getSalesRevenue(); } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/StatisticsServiceImpl.java b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/StatisticsServiceImpl.java index 4ac9086..094c727 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/StatisticsServiceImpl.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/StatisticsServiceImpl.java @@ -1,6 +1,6 @@ package de.htwsaar.webshop.service.impl; -import de.htwsaar.webshop.model.MonthlyCatModel; +import de.htwsaar.webshop.model.CatMonthModel; import de.htwsaar.webshop.repository.entities.OrderItem; import de.htwsaar.webshop.service.OrderService; import de.htwsaar.webshop.service.StatisticsService; @@ -25,32 +25,30 @@ public class StatisticsServiceImpl implements StatisticsService { } //returns Map> - private MonthlyCatModel getMonthCategoryMap(Function mappingFunction, - BinaryOperator reduceFunction, - T defaultValue) { - Map> map = new HashMap<>(); + private CatMonthModel getMonthCategoryMap(Function mappingFunction, + BinaryOperator reduceFunction, + T defaultValue) { + Map> map = new HashMap<>(); orderService.getTimeSortedOrders(TimeUtil.nowMonthsAgo(12), 12).forEach( (k,v) -> { log.info("Month {} has {}", k, v.size()); - map.putIfAbsent(k, new HashMap<>()); v.forEach( (order) -> order.getOrderItems().forEach((item) -> { - //log.info(" OrderItem {} has cat {}", item, item.getArticle().getCategory()); - map.get(k).putIfAbsent(item.getArticle().getCategory(), defaultValue); - map.get(k).computeIfPresent(item.getArticle().getCategory(), (cat,left ) -> reduceFunction.apply(left, mappingFunction.apply(item))); + map.putIfAbsent(item.getArticle().getCategory(), new HashMap<>()); + map.get(item.getArticle().getCategory()).putIfAbsent(k, defaultValue); + map.get(item.getArticle().getCategory()).computeIfPresent(k, (timestamp,left ) -> reduceFunction.apply(left, mappingFunction.apply(item))); }) ); - log.info("Month has {} cats", map.get(k).size()); }); - return new MonthlyCatModel<>(map); + return new CatMonthModel<>(map); } @Override - public MonthlyCatModel getSalesVolume() { + public CatMonthModel getSalesVolume() { return getMonthCategoryMap(OrderItem::getAmount, Integer::sum, 0); } @Override - public MonthlyCatModel getSalesRevenue() { + public CatMonthModel getSalesRevenue() { return getMonthCategoryMap(item -> item.getArticle().getPrice100(), Integer::sum, 0); } }