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

MiscQueriesImpl fully logged

parent 4e87faae
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
......@@ -4,12 +4,13 @@ import com.example.clientproject.data.tags.Tags;
import com.example.clientproject.data.users.Users;
import com.example.clientproject.service.dtos.UsersDTO;
import javax.servlet.http.HttpSession;
import java.util.List;
public interface MiscQueries {
void saveUserFavouriteTags(Users user, Tags tag);
void saveUser(Users user);
void updateUser(int userId, String field, Object value);
void saveTag(Tags tag);
void saveUserFavouriteTags(Users user, Tags tag, HttpSession session);
void saveUser(Users user, HttpSession session);
void updateUser(int userId, String field, Object value, HttpSession session);
void saveTag(Tags tag, HttpSession session);
List<UserFavouriteTags> findAllUserFavouriteTags();
}
......@@ -2,6 +2,7 @@ package com.example.clientproject.data.misc;
import com.example.clientproject.data.tags.Tags;
import com.example.clientproject.data.users.Users;
import com.example.clientproject.service.LoggingService;
import com.example.clientproject.service.dtos.UsersDTO;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
......@@ -9,6 +10,7 @@ import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -20,6 +22,7 @@ import java.util.Map;
public class MiscQueriesImpl implements MiscQueries{
private final JdbcTemplate jdbcTemplate;
private final RowMapper<UserFavouriteTags> userFavouriteTagsRowMapper;
LoggingService loggingService;
/**
* Constructor
......@@ -40,7 +43,7 @@ public class MiscQueriesImpl implements MiscQueries{
* @param user - the user
* @param tag - the tag
*/
public void saveUserFavouriteTags(Users user, Tags tag) {
public void saveUserFavouriteTags(Users user, Tags tag, HttpSession session) {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("User_Favourite_Tags")
.usingGeneratedKeyColumns("User_Favourite_Tag_Id");
......@@ -50,13 +53,21 @@ public class MiscQueriesImpl implements MiscQueries{
parameters.put("Tag_Id", tag.getTagId());
Number id = simpleJdbcInsert.execute(parameters);
// Log the changes
loggingService.logEvent(
"UserFavouriteTag Inserted",
session,
"UserFavouriteTag Inserted with User Id: " + user.getUserId() +
" and Tag Id: " + tag.getTagId() +
" in MiscQueriesImpl.saveUserFavouriteTags()"
);
}
/**
* Insert into "Users" table
* @param user - the user to insert
*/
public void saveUser(Users user) {
public void saveUser(Users user, HttpSession session) {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("Users")
.usingGeneratedKeyColumns("User_Id");
......@@ -73,58 +84,104 @@ public class MiscQueriesImpl implements MiscQueries{
parameters.put("User_Reset_Code_Expiry", user.getUserResetCodeExpiry());
Number id = simpleJdbcInsert.execute(parameters);
// Log the change
loggingService.logEvent(
"New User",
session,
"New User Inserted with Email: " + user.getUserEmail() +
" in MiscQueriesImpl.saveUser()"
);
}
public void updateUser(int userId, String field, Object value) {
public void updateUser(int userId, String field, Object value, HttpSession session) {
switch (field) {
case "User_First_Name": {
String sql = "UPDATE Users SET User_First_Name = ? WHERE User_Id = ?";
String sql = "UPDATE mydb.Users SET User_First_Name = ? WHERE User_Id = ?";
jdbcTemplate.update(
// Script
sql,
// Arguments
value, userId
);
// Log the change
loggingService.logEvent(
"User Details Changed",
session,
"User Details Updated with User Id: " + userId +
" with field: User_First_Name and value: " + value +
" in MiscQueriesImpl.updateUser()"
);
break;
}
case "User_Last_Name": {
String sql = "UPDATE Users SET User_Last_Name = ? WHERE User_Id = ?";
String sql = "UPDATE mydb.Users SET User_Last_Name = ? WHERE User_Id = ?";
jdbcTemplate.update(
// Script
sql,
// Arguments
value, userId
);
// Log the change
loggingService.logEvent(
"User Details Changed",
session,
"User Details Updated with User Id: " + userId +
" with field: User_Last_Name and value: " + value +
" in MiscQueriesImpl.updateUser()"
);
break;
}
case "User_Email": {
String sql = "UPDATE Users SET User_Email = ? WHERE User_Id = ?";
String sql = "UPDATE mydb.Users SET User_Email = ? WHERE User_Id = ?";
jdbcTemplate.update(
// Script
sql,
// Arguments
value, userId
);
// Log the change
loggingService.logEvent(
"User Details Changed",
session,
"User Details Updated with User Id: " + userId +
" with field: User_Email and value: " + value +
" in MiscQueriesImpl.updateUser()"
);
break;
}
case "User_Profile_Picture": {
String sql = "UPDATE Users SET User_Profile_Picture = ? WHERE User_Id = ?";
String sql = "UPDATE mydb.Users SET User_Profile_Picture = ? WHERE User_Id = ?";
jdbcTemplate.update(
// Script
sql,
// Arguments
value, userId
);
// Log the change
loggingService.logEvent(
"User Details Changed",
session,
"User Details Updated with User Id: " + userId +
" with field: User_Profile_Picture and value: " + value +
" in MiscQueriesImpl.updateUser()"
);
break;
}
case "User_Password": {
String sql = "UPDATE Users SET User_Password = ? WHERE User_Id = ?";
String sql = "UPDATE mydb.Users SET User_Password = ? WHERE User_Id = ?";
jdbcTemplate.update(
// Script
sql,
// Arguments
value, userId
);
// Log the change
loggingService.logEvent(
"User Details Changed",
session,
"User Details Updated with User Id: " + userId +
" with field: User_Password in MiscQueriesImpl.updateUser()"
);
break;
}
}
......@@ -134,7 +191,7 @@ public class MiscQueriesImpl implements MiscQueries{
* Insert into the "Tags" table
* @param tag - the tag to insert
*/
public void saveTag(Tags tag) {
public void saveTag(Tags tag, HttpSession session) {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("Tags")
.usingGeneratedKeyColumns("Tag_Id");
......@@ -152,7 +209,7 @@ public class MiscQueriesImpl implements MiscQueries{
*/
public List<UserFavouriteTags> findAllUserFavouriteTags() {
return jdbcTemplate.query(
"select * from User_Favourite_Tags",
"select * from mydb.User_Favourite_Tags",
userFavouriteTagsRowMapper,
new Object[]{}
);
......
......@@ -64,7 +64,8 @@ public class UserSettingsController {
miscQueries.updateUser(
userId,
"User_First_Name",
nameEmailProfileChangeForm.getNewFirstName()
nameEmailProfileChangeForm.getNewFirstName(),
httpSession
);
}
......@@ -74,7 +75,8 @@ public class UserSettingsController {
miscQueries.updateUser(
userId,
"User_Last_Name",
nameEmailProfileChangeForm.getNewLastName()
nameEmailProfileChangeForm.getNewLastName(),
httpSession
);
}
......@@ -84,7 +86,8 @@ public class UserSettingsController {
miscQueries.updateUser(
userId,
"User_Email",
nameEmailProfileChangeForm.getNewEmail().toLowerCase()
nameEmailProfileChangeForm.getNewEmail().toLowerCase(),
httpSession
);
}
......@@ -94,7 +97,8 @@ public class UserSettingsController {
miscQueries.updateUser(
userId,
"User_Profile_Picture",
nameEmailProfileChangeForm.getNewProfilePic()
nameEmailProfileChangeForm.getNewProfilePic(),
httpSession
);
}
......@@ -165,7 +169,8 @@ public class UserSettingsController {
"User_Password",
passwordEncoder.encode(
passwordChangeForm.getNewPassword()
)
),
httpSession
);
// Add an attribute to the model
model.addAttribute("passwordChangeSuccess", true);
......
......@@ -48,7 +48,8 @@ public class UserSettings {
miscQueries.updateUser(
userId,
"User_First_Name",
nameEmailProfileChangeForm.getNewFirstName()
nameEmailProfileChangeForm.getNewFirstName(),
httpSession
);
}
......@@ -59,7 +60,8 @@ public class UserSettings {
miscQueries.updateUser(
userId,
"User_Last_Name",
nameEmailProfileChangeForm.getNewLastName()
nameEmailProfileChangeForm.getNewLastName(),
httpSession
);
}
......@@ -70,7 +72,8 @@ public class UserSettings {
miscQueries.updateUser(
userId,
"User_Email",
nameEmailProfileChangeForm.getNewEmail().toLowerCase()
nameEmailProfileChangeForm.getNewEmail().toLowerCase(),
httpSession
);
}
......@@ -81,7 +84,8 @@ public class UserSettings {
miscQueries.updateUser(
userId,
"User_Profile_Picture",
nameEmailProfileChangeForm.getNewProfilePic()
nameEmailProfileChangeForm.getNewProfilePic(),
httpSession
);
}
......@@ -130,7 +134,8 @@ public class UserSettings {
"User_Password",
passwordEncoder.encode(
passwordChangeForm.getNewPassword()
)
),
httpSession
);
// Return a success message to the settings page
......
......@@ -331,7 +331,7 @@ INSERT INTO Tags (Tag_Name) VALUES ('eco-friendly');
INSERT INTO Tags (Tag_Name) VALUES ('decorations');
INSERT INTO Tags (Tag_Name) VALUES ('photography');
INSERT INTO Events (Event_Name) VALUES ('New Account Created');
INSERT INTO Events (Event_Name) VALUES ('New User');
INSERT INTO Events (Event_Name) VALUES ('Failed Login');
INSERT INTO Events (Event_Name) VALUES ('Successful Login');
INSERT INTO Events (Event_Name) VALUES ('User Details Changed');
......@@ -356,4 +356,5 @@ INSERT INTO Events (Event_Name) VALUES ('UserShopLink Deleted');
INSERT INTO Events (Event_Name) VALUES ('UserShopLink Inserted');
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
INSERT INTO Events (Event_Name) VALUES ('UserStampBoard Inserted');
INSERT INTO Events (Event_Name) VALUES ('UserFavouriteTag Inserted');
\ No newline at end of file
......@@ -58,7 +58,7 @@ public class SelectCategoriesTests {
"", "",
LocalDateTime.now().format(formatter), twoFactorMethods);
// Save the user
miscQueries.saveUser(newUser);
miscQueries.saveUser(newUser, null);
// Get the user as a DTO object
Optional<Users> usersOptional = usersRepo.findByUserEmail(newUser.getUserEmail());
......@@ -69,11 +69,11 @@ public class SelectCategoriesTests {
// Create a new "Tags" object with that name
Tags newTag = new Tags(tagName);
// Save a new tag with that name
miscQueries.saveTag(newTag);
miscQueries.saveTag(newTag, null);
// Get the newly saved tag
Optional<Tags> tagsOptional = tagsRepo.findByTagName(tagName);
// Add a row to the "User_Favourite_Tags" table
miscQueries.saveUserFavouriteTags(usersOptional.get(), tagsOptional.get());
miscQueries.saveUserFavouriteTags(usersOptional.get(), tagsOptional.get(), null);
}
// Get the size of the table at the beginning
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment