Skip to content
Snippets Groups Projects
Commit f968fd4b authored by Joshua Gill's avatar Joshua Gill
Browse files

ShopActiveService fully logged, other classes changed to no longer use autowiring for attributes

parent c7216f02
No related branches found
No related tags found
2 merge requests!114LoggingService service class, new method to add a log to the "Logs" table when...,!107Issue complete
...@@ -22,22 +22,20 @@ import java.util.*; ...@@ -22,22 +22,20 @@ import java.util.*;
@Service @Service
public class RecommendationGenerator { public class RecommendationGenerator {
public JWTUtils jwtUtils; JWTUtils jwtUtils;
public UserFavouriteToggle favouriteToggle; UserFavouriteToggle favouriteToggle;
public StampBoardsRepo stampBoardsRepo; StampBoardsRepo stampBoardsRepo;
public UserStampBoardsRepo userStampBoardsRepo; UserStampBoardsRepo userStampBoardsRepo;
@Autowired
UsersRepo usersRepo; UsersRepo usersRepo;
@Autowired
ShopsRepo shopsRepo; ShopsRepo shopsRepo;
public RecommendationGenerator(JWTUtils jwt, UserFavouriteToggle uft, StampBoardsRepo sbr, UserStampBoardsRepo usbr){ public RecommendationGenerator(JWTUtils jwtUtils, UserFavouriteToggle favouriteToggle, StampBoardsRepo stampBoardsRepo, UserStampBoardsRepo userStampBoardsRepo, UsersRepo usersRepo, ShopsRepo shopsRepo) {
jwtUtils = jwt; this.jwtUtils = jwtUtils;
favouriteToggle = uft; this.favouriteToggle = favouriteToggle;
stampBoardsRepo = sbr; this.stampBoardsRepo = stampBoardsRepo;
userStampBoardsRepo = usbr; this.userStampBoardsRepo = userStampBoardsRepo;
this.usersRepo = usersRepo;
this.shopsRepo = shopsRepo;
} }
public List<Shops> getRecommendations(HttpSession session, List<Shops> shopsToRecommend) throws Exception { public List<Shops> getRecommendations(HttpSession session, List<Shops> shopsToRecommend) throws Exception {
......
package com.example.clientproject.services; package com.example.clientproject.services;
import com.example.clientproject.service.LoggingService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Service @Service
public class ShopActiveService { public class ShopActiveService {
@Autowired
JdbcTemplate jdbc; 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 * @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 * @return - an int of Shop activity 1 = active || 0 = not active
*/ */
public int isShopActive(Integer shopId){ 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{ try{
List<Map<String, Object>> rs = jdbc.queryForList(query); List<Map<String, Object>> rs = jdbc.queryForList(query);
...@@ -35,10 +41,18 @@ public class ShopActiveService { ...@@ -35,10 +41,18 @@ public class ShopActiveService {
* @param active - will either be 1 or 0 and will update shops "Shop_Active" field accordingly * @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 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); 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()"
);
} }
} }
......
...@@ -45,9 +45,9 @@ public class ToggleShopActive { ...@@ -45,9 +45,9 @@ public class ToggleShopActive {
if (shopPermissionLevel == 2 || shopPermissionLevel == 3) { if (shopPermissionLevel == 2 || shopPermissionLevel == 3) {
System.out.println("shop is being deleted"); System.out.println("shop is being deleted");
if(shopActiveService.isShopActive(shopId) == 0){//if shop is currently un-active 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 } 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 { } else {
System.out.println("an error has occured updating shop activity, shop may potentially have an active value other than 1 or 0"); System.out.println("an error has occured updating shop activity, shop may potentially have an active value other than 1 or 0");
} }
......
...@@ -37,7 +37,7 @@ public class ShopActivityTests { ...@@ -37,7 +37,7 @@ public class ShopActivityTests {
@Test @Test
public void activeShopsDecreasedBy1AfterMethodCalled(){ public void activeShopsDecreasedBy1AfterMethodCalled(){
List<Shops> activeShopsListBeforeChange = shopsRepo.findActiveShops(); List<Shops> activeShopsListBeforeChange = shopsRepo.findActiveShops();
shopActiveService.updateShopActive(6, 0); shopActiveService.updateShopActive(6, 0, null);
List<Shops> activeShopsListAfterChange = shopsRepo.findActiveShops(); List<Shops> activeShopsListAfterChange = shopsRepo.findActiveShops();
assertEquals(activeShopsListBeforeChange.size()-1, activeShopsListAfterChange.size()); assertEquals(activeShopsListBeforeChange.size()-1, activeShopsListAfterChange.size());
//size after change should be equal to size before change minus one //size after change should be equal to size before change minus one
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment