diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java index 8a449dca0341cb1548bce2ccca4379615cf5bae4..d7a0a47c1d96490ceeba7614cfb251f2c6e25dc0 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java @@ -5,14 +5,17 @@ import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.dto.request.AllergyRequest; import ntnu.idatt2016.v233.SmartMat.dto.request.RegisterUserRequest; import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority; +import ntnu.idatt2016.v233.SmartMat.dto.request.UpdateUserRequest; import ntnu.idatt2016.v233.SmartMat.entity.user.User; import ntnu.idatt2016.v233.SmartMat.service.user.UserService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; +import java.util.Optional; /** @@ -102,4 +105,71 @@ public class UserController { .map(user -> ResponseEntity.ok(user.getAllergies().size() > 0)) .orElseGet(() -> ResponseEntity.notFound().build()); } + + /** + * Update a user in the database. + * @param username The username of the user to be updated. + * @param updateUser The new values for the user. + * @param authentication The authentication object of the user. + * @return The updated user. + */ + @PutMapping("/update/{username}") + public ResponseEntity<User> updateUser(@PathVariable String username, @RequestBody UpdateUserRequest updateUser, + Authentication authentication) { + if(!username.equals(authentication.getName())) + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + + Optional<User> user = userService.getUserFromUsername(username); + + + if(user.isEmpty()) + return ResponseEntity.notFound().build(); + + User userEntity = user.get(); + + if(updateUser.firstName() != null && + !updateUser.firstName().trim().isEmpty() && updateUser.firstName().length() <= 50){ + userEntity.setFirstName(updateUser.firstName()); + } + + if(updateUser.lastName() != null && + !updateUser.lastName().trim().isEmpty() && updateUser.lastName().length() <= 50){ + userEntity.setLastName(updateUser.lastName()); + } + + if(updateUser.email() != null && + !updateUser.email().trim().isEmpty() && updateUser.email().length() <= 50){ + userEntity.setEmail(updateUser.email()); + } + + if(updateUser.password() != null && + !updateUser.password().trim().isEmpty() && updateUser.password().length() <= 50){ + userEntity.setPassword(passwordEncoder.encode(updateUser.password())); + } + + if(updateUser.birthDate() != null){ + userEntity.setDateOfBirth(updateUser.birthDate()); + } + + if(updateUser.allergies() != null){ + userEntity.getAllergies().stream().filter(allergy -> !updateUser.allergies().contains(allergy.getName())) + .forEach(allergy -> userService.removeAllergyFromUser(username, allergy.getName())); + + updateUser.allergies().stream().filter(allergy -> userEntity.getAllergies().stream() + .noneMatch(allergy1 -> allergy1.getName().equals(allergy))).forEach( + allergy -> userService.addAllergyToUser(username, allergy) + ); + + } + + if(userEntity.equals(userService.getUserFromUsername(username).get())) + return ResponseEntity.status(HttpStatus.NOT_MODIFIED).build(); + + User updateduser = userService.updateUser(userEntity); + + updateduser.setPassword(null); + + return ResponseEntity.ok(updateduser); + + } } \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/UpdateUserRequest.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/UpdateUserRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..159107c397361cc9931851ef3935e3f2cc4acd13 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/UpdateUserRequest.java @@ -0,0 +1,22 @@ +package ntnu.idatt2016.v233.SmartMat.dto.request; + +import java.sql.Date; +import java.util.List; + +/** + * This class represents a request to update a user + * @author Birk + * @version 1.0 + * @since 26.04.2023 + * + * @param firstName the first name of the user + * @param lastName the last name of the user + * @param email the email of the user + * @param password the password of the user + * @param birthDate the birth date of the user + * @param allergies the allergies of the user + */ +public record UpdateUserRequest(String firstName, String lastName, + String email, String password, Date birthDate, + List<String> allergies) { +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java index b3990b550255d423bfeeeb5a609244291a6ee399..4fce289e2eab5ffd8e83878cbb2b69883178edcb 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java @@ -173,4 +173,25 @@ public class UserService { } return Optional.empty(); } + + /** + * Removes allergy from user + * @param username username of user + * @param allergyName name of allergy + * @return user with removed allergy + */ + public Optional<User> removeAllergyFromUser(String username, String allergyName){ + Optional<User> user = userRepository.findByUsername(username); + Optional<Allergy> allergy = allergyRepository.findByName(allergyName); + + if (user.isPresent() && allergy.isPresent()){ + user.get().getAllergies().remove(allergy.get()); + return Optional.of(userRepository.save(user.get())); + } else if (user.isEmpty()) { + throw new EntityNotFoundException("User not found"); + } else if (allergy.isEmpty()) { + throw new EntityNotFoundException("Allergy not found"); + } + return Optional.empty(); + } } \ No newline at end of file