From f968fd4b8379f003d1d74242751b1e8143a86980 Mon Sep 17 00:00:00 2001 From: c2038058 <gillj8@cardiff.ac.uk> Date: Fri, 10 Dec 2021 11:59:49 +0000 Subject: [PATCH] ShopActiveService fully logged, other classes changed to no longer use autowiring for attributes --- .../services/RecommendationGenerator.java | 24 +++++++++---------- .../services/ShopActiveService.java | 22 +++++++++++++---- .../web/restControllers/ToggleShopActive.java | 4 ++-- .../clientproject/data/ShopActivityTests.java | 2 +- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/example/clientproject/services/RecommendationGenerator.java b/src/main/java/com/example/clientproject/services/RecommendationGenerator.java index 320f171..1fc7207 100644 --- a/src/main/java/com/example/clientproject/services/RecommendationGenerator.java +++ b/src/main/java/com/example/clientproject/services/RecommendationGenerator.java @@ -22,22 +22,20 @@ import java.util.*; @Service public class RecommendationGenerator { - public JWTUtils jwtUtils; - public UserFavouriteToggle favouriteToggle; - public StampBoardsRepo stampBoardsRepo; - public UserStampBoardsRepo userStampBoardsRepo; - - @Autowired + JWTUtils jwtUtils; + UserFavouriteToggle favouriteToggle; + StampBoardsRepo stampBoardsRepo; + UserStampBoardsRepo userStampBoardsRepo; UsersRepo usersRepo; - - @Autowired ShopsRepo shopsRepo; - public RecommendationGenerator(JWTUtils jwt, UserFavouriteToggle uft, StampBoardsRepo sbr, UserStampBoardsRepo usbr){ - jwtUtils = jwt; - favouriteToggle = uft; - stampBoardsRepo = sbr; - userStampBoardsRepo = usbr; + public RecommendationGenerator(JWTUtils jwtUtils, UserFavouriteToggle favouriteToggle, StampBoardsRepo stampBoardsRepo, UserStampBoardsRepo userStampBoardsRepo, UsersRepo usersRepo, ShopsRepo shopsRepo) { + this.jwtUtils = jwtUtils; + this.favouriteToggle = favouriteToggle; + this.stampBoardsRepo = stampBoardsRepo; + this.userStampBoardsRepo = userStampBoardsRepo; + this.usersRepo = usersRepo; + this.shopsRepo = shopsRepo; } public List<Shops> getRecommendations(HttpSession session, List<Shops> shopsToRecommend) throws Exception { diff --git a/src/main/java/com/example/clientproject/services/ShopActiveService.java b/src/main/java/com/example/clientproject/services/ShopActiveService.java index 9c7ed39..8ec00b7 100644 --- a/src/main/java/com/example/clientproject/services/ShopActiveService.java +++ b/src/main/java/com/example/clientproject/services/ShopActiveService.java @@ -1,25 +1,31 @@ package com.example.clientproject.services; +import com.example.clientproject.service.LoggingService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; +import javax.servlet.http.HttpSession; import java.util.List; import java.util.Map; @Service public class ShopActiveService { - @Autowired JdbcTemplate jdbc; + LoggingService loggingService; + public ShopActiveService(JdbcTemplate jdbc, LoggingService loggingService) { + this.jdbc = jdbc; + this.loggingService = loggingService; + } /** * @param shopId - uses shopId to select which shop will have its active field checked * @return - an int of Shop activity 1 = active || 0 = not active */ public int isShopActive(Integer shopId){ - String query = "SELECT Shop_Active FROM shops WHERE Shop_Id = " + shopId + ";"; + String query = "SELECT Shop_Active FROM mydb.shops WHERE Shop_Id = " + shopId + ";"; try{ List<Map<String, Object>> rs = jdbc.queryForList(query); @@ -35,10 +41,18 @@ public class ShopActiveService { * @param active - will either be 1 or 0 and will update shops "Shop_Active" field accordingly */ - public void updateShopActive(Integer shopId, Integer active){ + public void updateShopActive(Integer shopId, Integer active, HttpSession session){ if(active == 0 || active == 1){ //only allows active values of 0 or 1 - String query = "UPDATE shops SET Shop_Active = " + active + " WHERE Shop_Id = " + shopId + ";"; + String query = "UPDATE mydb.shops SET Shop_Active = " + active + " WHERE Shop_Id = " + shopId + ";"; jdbc.execute(query); + // Log the change + loggingService.logEvent( + "Shop Update", + session, + "Update to Shop: " + shopId + + " with field: Shop_Active with value: " + active + + " in ShopActiveService.updateShopActive()" + ); } } diff --git a/src/main/java/com/example/clientproject/web/restControllers/ToggleShopActive.java b/src/main/java/com/example/clientproject/web/restControllers/ToggleShopActive.java index 49629f6..8ee236a 100644 --- a/src/main/java/com/example/clientproject/web/restControllers/ToggleShopActive.java +++ b/src/main/java/com/example/clientproject/web/restControllers/ToggleShopActive.java @@ -45,9 +45,9 @@ public class ToggleShopActive { if (shopPermissionLevel == 2 || shopPermissionLevel == 3) { System.out.println("shop is being deleted"); if(shopActiveService.isShopActive(shopId) == 0){//if shop is currently un-active - shopActiveService.updateShopActive(shopId, 1);//enables shop + shopActiveService.updateShopActive(shopId, 1, session);//enables shop } else if(shopActiveService.isShopActive(shopId) == 1){//if shop is currently active - shopActiveService.updateShopActive(shopId, 0);//disables shop + shopActiveService.updateShopActive(shopId, 0, session);//disables shop } else { System.out.println("an error has occured updating shop activity, shop may potentially have an active value other than 1 or 0"); } diff --git a/src/test/java/com/example/clientproject/data/ShopActivityTests.java b/src/test/java/com/example/clientproject/data/ShopActivityTests.java index 0bcd3f0..32ebb62 100644 --- a/src/test/java/com/example/clientproject/data/ShopActivityTests.java +++ b/src/test/java/com/example/clientproject/data/ShopActivityTests.java @@ -37,7 +37,7 @@ public class ShopActivityTests { @Test public void activeShopsDecreasedBy1AfterMethodCalled(){ List<Shops> activeShopsListBeforeChange = shopsRepo.findActiveShops(); - shopActiveService.updateShopActive(6, 0); + shopActiveService.updateShopActive(6, 0, null); List<Shops> activeShopsListAfterChange = shopsRepo.findActiveShops(); assertEquals(activeShopsListBeforeChange.size()-1, activeShopsListAfterChange.size()); //size after change should be equal to size before change minus one -- GitLab