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

UserStampBoardService fully logged

parent bbd5f372
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
......@@ -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()"
);
}
}
......@@ -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();
......
......@@ -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
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