From 20c13bd77c19302d88cbc8fc2b3a57ae5ba3985e Mon Sep 17 00:00:00 2001
From: joalhelk <joalhelk@jyu.fi>
Date: Sun, 27 Mar 2022 17:57:19 +0300
Subject: [PATCH] comments

---
 .../watcherGui/frames/subframes/AdminFrame.java     | 13 ++++++++-----
 .../watcherServer/appuser/AppUserService.java       |  4 +++-
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/AdminFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/AdminFrame.java
index 25915ba..88b3e69 100644
--- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/AdminFrame.java
+++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/AdminFrame.java
@@ -59,9 +59,12 @@ public class AdminFrame extends JPanel implements ActionListener {
 	    revalidate();
 	}
 	
+	/*
+	 * Creates a user column for editing user details
+	 */
 	private void CreateUserField(UserType user) {
-		JLabel name = new JLabel(user.getName());
-		name.setForeground(Color.white);
+		JTextField name = new JTextField(20);
+		name.setText(user.getName());
 	    panel.add(name, c);
 
 	    JTextField email = new JTextField(20);
@@ -85,7 +88,7 @@ public class AdminFrame extends JPanel implements ActionListener {
 	    c.gridwidth = GridBagConstraints.REMAINDER;
 	    JButton save = new JButton("Save");
 	    save.addActionListener( evt -> {
-	    	performActionOnSave(name, email, enabled, locked, user.getId());
+	    	performActionOnSave(name.getText(), email.getText(), enabled.isSelected(), locked.isSelected(), user.getId());
 	    });
 	    panel.add(save, c);
 	    c.gridwidth = 1;
@@ -95,10 +98,10 @@ public class AdminFrame extends JPanel implements ActionListener {
 	 * Handles the save button press
 	 * Sends update request to server with new info
 	 */
-	private void performActionOnSave(JLabel name, JTextField email, JCheckBox enabled, JCheckBox locked, Long id) {
+	private void performActionOnSave(String name, String email, Boolean enabled, Boolean locked, Long id) {
 		if (!WelcomePage.handleSave()) return;
 		
-		if(!HttpRequests.updateUser(name.getText(), email.getText(), enabled.isSelected(), locked.isSelected(), id)) WelcomePage.ErrorDialog(); 
+		if(!HttpRequests.updateUser(name, email, enabled, locked, id)) WelcomePage.ErrorDialog(); 
 		UpdateUsers();
 	}
 
diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserService.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserService.java
index e4ca763..3acefcf 100644
--- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserService.java
+++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserService.java
@@ -80,7 +80,7 @@ public class AppUserService implements UserDetailsService {
         appUserRepository.deleteById(id);
     }
 
-    // Update users name and/or email
+    // Update users info
     @Transactional
     public void updateUser(Long id, String name, String email, Boolean enabled, Boolean locked) {
         log.info("Updating user: " + email);
@@ -89,10 +89,12 @@ public class AppUserService implements UserDetailsService {
                 .orElseThrow(() -> new IllegalStateException(
                         "person with id " + id + " not found" ));
 
+        // Check if name isn't null/empty nor it isn't current name
         if(name != null && name.length() > 0 && !Objects.equals(person.getName(), name)) {
             person.setName(name);
         }
 
+        // Check if email is given, isn't current email & isn't taken
         if(email != null && email.length() > 0 && !Objects.equals(person.getEmail(), email)) {
             Optional<AppUser> optionalPerson = appUserRepository.findByEmail(email);
             if(optionalPerson.isPresent()) throw new IllegalStateException("email taken");
-- 
GitLab