Skip to content
Snippets Groups Projects
Commit a9c63941 authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

added updateUser endpoint

parent 52aba3c2
No related branches found
No related tags found
No related merge requests found
......@@ -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
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) {
}
......@@ -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
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