From 98a29b0dd6f4df2f653d39a540de4dd60ff030a0 Mon Sep 17 00:00:00 2001
From: c2038058 <gillj8@cardiff.ac.uk>
Date: Fri, 10 Dec 2021 13:16:53 +0000
Subject: [PATCH] MiscQueriesImpl fully logged

---
 .../clientproject/data/misc/MiscQueries.java  |  9 ++-
 .../data/misc/MiscQueriesImpl.java            | 77 ++++++++++++++++---
 .../UserSettingsController.java               | 15 ++--
 .../web/restControllers/UserSettings.java     | 15 ++--
 src/main/resources/database/schema.sql        |  5 +-
 .../controllers/SelectCategoriesTests.java    |  6 +-
 6 files changed, 98 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/example/clientproject/data/misc/MiscQueries.java b/src/main/java/com/example/clientproject/data/misc/MiscQueries.java
index 4273750..b172cc5 100644
--- a/src/main/java/com/example/clientproject/data/misc/MiscQueries.java
+++ b/src/main/java/com/example/clientproject/data/misc/MiscQueries.java
@@ -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();
 }
diff --git a/src/main/java/com/example/clientproject/data/misc/MiscQueriesImpl.java b/src/main/java/com/example/clientproject/data/misc/MiscQueriesImpl.java
index 3ada75e..d2354af 100644
--- a/src/main/java/com/example/clientproject/data/misc/MiscQueriesImpl.java
+++ b/src/main/java/com/example/clientproject/data/misc/MiscQueriesImpl.java
@@ -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[]{}
         );
diff --git a/src/main/java/com/example/clientproject/web/controllers/userSettingsPage/UserSettingsController.java b/src/main/java/com/example/clientproject/web/controllers/userSettingsPage/UserSettingsController.java
index a228ad4..55be605 100644
--- a/src/main/java/com/example/clientproject/web/controllers/userSettingsPage/UserSettingsController.java
+++ b/src/main/java/com/example/clientproject/web/controllers/userSettingsPage/UserSettingsController.java
@@ -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);
diff --git a/src/main/java/com/example/clientproject/web/restControllers/UserSettings.java b/src/main/java/com/example/clientproject/web/restControllers/UserSettings.java
index e611e2e..a26473f 100644
--- a/src/main/java/com/example/clientproject/web/restControllers/UserSettings.java
+++ b/src/main/java/com/example/clientproject/web/restControllers/UserSettings.java
@@ -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
diff --git a/src/main/resources/database/schema.sql b/src/main/resources/database/schema.sql
index 200c324..f573223 100644
--- a/src/main/resources/database/schema.sql
+++ b/src/main/resources/database/schema.sql
@@ -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
diff --git a/src/test/java/com/example/clientproject/web/controllers/SelectCategoriesTests.java b/src/test/java/com/example/clientproject/web/controllers/SelectCategoriesTests.java
index 1f1a5a9..e8750b0 100644
--- a/src/test/java/com/example/clientproject/web/controllers/SelectCategoriesTests.java
+++ b/src/test/java/com/example/clientproject/web/controllers/SelectCategoriesTests.java
@@ -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
-- 
GitLab