From 4e87faae330d9323f3f561627364f73be8f06454 Mon Sep 17 00:00:00 2001 From: c2038058 <gillj8@cardiff.ac.uk> Date: Fri, 10 Dec 2021 12:59:18 +0000 Subject: [PATCH] UserStampBoardService fully logged --- .../services/UserStampBoardService.java | 40 +++++++++++++++---- .../UpdateUserStampPosition.java | 8 ++-- src/main/resources/database/schema.sql | 4 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/example/clientproject/services/UserStampBoardService.java b/src/main/java/com/example/clientproject/services/UserStampBoardService.java index b5391f7..f29f93d 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 3b517f9..b9d6eca 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 eed3db2..200c324 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 -- GitLab