diff --git a/src/main/java/com/example/clientproject/services/UserStampBoardService.java b/src/main/java/com/example/clientproject/services/UserStampBoardService.java index b5391f7c6b490d671c8852b9554256065941b1e5..f29f93d80e17d806e259294e822123dd06f803d9 100644 --- a/src/main/java/com/example/clientproject/services/UserStampBoardService.java +++ b/src/main/java/com/example/clientproject/services/UserStampBoardService.java @@ -3,10 +3,12 @@ package com.example.clientproject.services; import com.example.clientproject.data.userStampBoards.UserStampBoards; import com.example.clientproject.data.userStampBoards.UserStampBoardsRepo; import com.example.clientproject.data.users.UsersRepo; +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.sql.ResultSet; import java.util.List; import java.util.Map; @@ -15,11 +17,15 @@ import java.util.Objects; @Service public class UserStampBoardService { - @Autowired JdbcTemplate jdbc; - - @Autowired UserStampBoardsRepo userRepo; + LoggingService loggingService; + + public UserStampBoardService(JdbcTemplate jdbc, UserStampBoardsRepo userRepo, LoggingService loggingService) { + this.jdbc = jdbc; + this.userRepo = userRepo; + this.loggingService = loggingService; + } /** * Select user stamp position by userId and stampBoardId @@ -28,7 +34,7 @@ public class UserStampBoardService { */ public int getUserStampPos(int userID, int stampBoardID){ - String query = "SELECT User_Stamp_Position FROM user_stamp_boards WHERE User_Id = " + userID + " AND Stamp_Board_Id = " + stampBoardID + ";"; + String query = "SELECT User_Stamp_Position FROM mydb.user_stamp_boards WHERE User_Id = " + userID + " AND Stamp_Board_Id = " + stampBoardID + ";"; try{ List<Map<String, Object>> rs = jdbc.queryForList(query); @@ -40,14 +46,32 @@ public class UserStampBoardService { } - public void changeUserStampPosition(int userID, int incrementValue, int currentUserStampPos, int stampBoardId){ + public void changeUserStampPosition(int userID, int incrementValue, int currentUserStampPos, int stampBoardId, HttpSession session){ int newStampPos = currentUserStampPos + incrementValue; - String query = "UPDATE user_stamp_boards SET User_Stamp_Position = " + newStampPos + " WHERE User_Id = " + userID + " AND Stamp_Board_Id = " + stampBoardId + ";"; + String query = "UPDATE mydb.user_stamp_boards SET User_Stamp_Position = " + newStampPos + " WHERE User_Id = " + userID + " AND Stamp_Board_Id = " + stampBoardId + ";"; jdbc.execute(query); + // Log the change + loggingService.logEvent( + "UserStampBoard Updated", + session, + "UserStampBoard updated for StampBoard Id: " + stampBoardId + + " where User Id: " + userID + + " where field: User_Stamp_Position and value: " + newStampPos + + " in UserStampBoardService.changeUserStampPosition()" + ); } - public void createStampRecord(int userID, int stampPosition, int stampBoardId){ - String query = "INSERT INTO user_stamp_boards (User_Id, Stamp_Board_Id, User_Stamp_Position) VALUES ("+userID+", "+stampBoardId+", "+ stampPosition +");"; + public void createStampRecord(int userID, int stampPosition, int stampBoardId, HttpSession session){ + String query = "INSERT INTO mydb.user_stamp_boards (User_Id, Stamp_Board_Id, User_Stamp_Position) VALUES ("+userID+", "+stampBoardId+", "+ stampPosition +");"; jdbc.execute(query); + // Log the change + loggingService.logEvent( + "UserStampBoard Inserted", + session, + "UserStampBoard Inserted where User Id: " + userID + + " StampBoard Id: " + stampBoardId + + " and Stamp Position: " + stampPosition + + " in UserStampBoardService.createStampRecord()" + ); } } diff --git a/src/main/java/com/example/clientproject/web/restControllers/UpdateUserStampPosition.java b/src/main/java/com/example/clientproject/web/restControllers/UpdateUserStampPosition.java index 3b517f939a069bce3e0c169b5bf43e4f81b3ba64..b9d6eca589b69c9635e9df0892ca03b2a8084cef 100644 --- a/src/main/java/com/example/clientproject/web/restControllers/UpdateUserStampPosition.java +++ b/src/main/java/com/example/clientproject/web/restControllers/UpdateUserStampPosition.java @@ -70,16 +70,16 @@ public class UpdateUserStampPosition { StampBoards stampBoard = shop.getStampBoard(); if(Objects.equals(direction, "subtract")){ if(currentUserStampPos != 0){ - userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), -1, currentUserStampPos, (int) stampBoard.getStampBoardId()); + userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), -1, currentUserStampPos, (int) stampBoard.getStampBoardId(), session); } } else if(Objects.equals(direction, "add")){ if(currentUserStampPos != shopStampBoardSize){ - userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), 1, currentUserStampPos, (int) stampBoard.getStampBoardId()); + userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), 1, currentUserStampPos, (int) stampBoard.getStampBoardId(), session); currentUserStampPos = userStampBoardService.getUserStampPos(jwtUtils.getLoggedInUserId(session).get(), (int) shopStampBoardId ); } if(currentUserStampPos == 0){ System.out.println("Attempting to create record for user"); - userStampBoardService.createStampRecord(jwtUtils.getLoggedInUserId(session).get(), 1, (int) shopStampBoardId); + userStampBoardService.createStampRecord(jwtUtils.getLoggedInUserId(session).get(), 1, (int) shopStampBoardId, session); } } } @@ -102,7 +102,7 @@ public class UpdateUserStampPosition { } if(userIsLinkedToStampBoard){ if(userStampPos >= reward.get().getRewardStampLocation()){ - userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), -reward.get().getRewardStampLocation(), userStampPos, stampBoardId); + userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), -reward.get().getRewardStampLocation(), userStampPos, stampBoardId, session); //credit to www.programiz.com for code generator String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//creates a string of all characters StringBuilder sb = new StringBuilder(); diff --git a/src/main/resources/database/schema.sql b/src/main/resources/database/schema.sql index eed3db25a280e04416099114ed066c11e6a8b855..200c32473964ffe83071e9324cbd52b2587e8601 100644 --- a/src/main/resources/database/schema.sql +++ b/src/main/resources/database/schema.sql @@ -354,4 +354,6 @@ INSERT INTO Events (Event_Name) VALUES ('Reward Deleted'); INSERT INTO Events (Event_Name) VALUES ('Shop Updated'); INSERT INTO Events (Event_Name) VALUES ('UserShopLink Deleted'); INSERT INTO Events (Event_Name) VALUES ('UserShopLink Inserted'); -INSERT INTO Events (Event_Name) VALUES ('ShopWebsite Updated'); \ No newline at end of file +INSERT INTO Events (Event_Name) VALUES ('ShopWebsite Updated'); +INSERT INTO Events (Event_Name) VALUES ('UserStampBoard Updated'); +INSERT INTO Events (Event_Name) VALUES ('UserStampBoard Inserted'); \ No newline at end of file