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

fixed add and delete allergy

parent f41dc970
No related branches found
No related tags found
No related merge requests found
......@@ -6,16 +6,19 @@ 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.product.Allergy;
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.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Collectors;
/**
......@@ -105,14 +108,13 @@ public class UserController {
* @return a ResponseEntity with a boolean indicating whether the operation was successful
*/
@PostMapping("/addAllergy")
public ResponseEntity<Boolean> addAllergyToUser(@RequestBody AllergyRequest allergyRequest) {
try {
return userService.addAllergyToUser(allergyRequest.getUsername(), allergyRequest.getAllergyName())
.map(user -> ResponseEntity.ok(user.getAllergies().size() > 0))
.orElseGet(() -> ResponseEntity.notFound().build());
}catch (Exception e){
return ResponseEntity.status(409).body(false);
}
public ResponseEntity<String> addAllergyToUser(@RequestBody AllergyRequest allergyRequest,
Authentication authentication) {
if(!allergyRequest.getUsername().equals(authentication.getName()) &&
!authentication.getAuthorities().contains(new SimpleGrantedAuthority(Authority.ADMIN.name())))
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
return userService.addAllergyToUser(allergyRequest.getUsername(), allergyRequest.getAllergyName());
}
/**
......@@ -122,14 +124,14 @@ public class UserController {
* @return a ResponseEntity with a boolean indicating whether the operation was successful
*/
@DeleteMapping("/deleteAllergy")
public ResponseEntity<Boolean> deleteAllergyFromUser(@RequestBody AllergyRequest allergyRequest) {
try {
return userService.deleteAllergy(allergyRequest.getUsername(), allergyRequest.getAllergyName())
.map(user -> ResponseEntity.ok(true))
.orElseGet(() -> ResponseEntity.notFound().build());
}catch (Exception e){
return ResponseEntity.status(409).body(false);
}
public ResponseEntity<String> deleteAllergyFromUser(@RequestBody AllergyRequest allergyRequest,
Authentication authentication) {
if(!allergyRequest.getUsername().equals(authentication.getName()) &&
!authentication.getAuthorities().contains(new SimpleGrantedAuthority(Authority.ADMIN.name())))
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
return userService.removeAllergyFromUser(allergyRequest.getUsername(), allergyRequest.getAllergyName());
}
......
......@@ -9,6 +9,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -49,4 +50,15 @@ public class Allergy{
@JsonIgnore
private List<User> users;
/**
* adds a user to the allergy
* @param tempuser adds a user to the list of users with this allergy
*/
public void addUser(User tempuser) {
if (users == null)
users = new ArrayList<>();
users.add(tempuser);
}
}
\ No newline at end of file
......@@ -60,7 +60,8 @@ public class User implements UserDetails {
private List<UserGroupAsso> group;
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH},
@ManyToMany(fetch = FetchType.LAZY,
cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH},
mappedBy = "users")
@JsonIgnoreProperties({"users", "products"})
private List<Allergy> allergies;
......
......@@ -8,6 +8,8 @@ import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import ntnu.idatt2016.v233.SmartMat.repository.AllergyRepository;
import ntnu.idatt2016.v233.SmartMat.repository.user.UserRepository;
import ntnu.idatt2016.v233.SmartMat.service.RecipeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
......@@ -132,23 +134,27 @@ public class UserService {
* Adds allergy to user
* @param username username of user
* @param allergyName name of allergy
* @return user with added allergy
* @throws EntityNotFoundException if user or allergy does not exist
* @return string of allergies
*/
public Optional<User> addAllergyToUser(String username, String allergyName){
public ResponseEntity<String> addAllergyToUser(String username, String allergyName){
Optional<User> user = userRepository.findByUsername(username);
Optional<Allergy> allergy = allergyRepository.findByName(allergyName);
if (user.isPresent() && allergy.isPresent()){
user.get().addAllergy(allergy.get());
return Optional.of(userRepository.save(user.get()));
} else if (!user.isPresent()) {
throw new EntityNotFoundException("User not found");
} else if (!allergy.isPresent()) {
throw new EntityNotFoundException("Allergy not found");
if(user.get().getAllergies().contains(allergy.get())
|| allergy.get().getUsers().contains(user.get()))
return ResponseEntity.status(HttpStatus.NOT_MODIFIED)
.body("User already has this allergy");
User tempuser = user.get();
allergy.get().addUser(tempuser);
tempuser.addAllergy(allergy.get());
return ResponseEntity.ok(userRepository.save(tempuser).getAllergies().stream()
.map(Allergy::getName)
.reduce("", (a, b) -> a + " " + b));
}
return Optional.empty();
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Did not find allergy, our user");
}
/**
......@@ -157,40 +163,23 @@ public class UserService {
* @param allergyName name of allergy
* @return user with removed allergy
*/
public Optional<User> removeAllergyFromUser(String username, String allergyName){
public ResponseEntity<String> removeAllergyFromUser(String username, String allergyName){
Optional<User> user = userRepository.findByUsername(username);
Optional<Allergy> allergy = allergyRepository.findByName(allergyName);
if (user.isPresent() && allergy.isPresent()){
if (!user.get().getAllergies().contains(allergy.get())
|| !allergy.get().getUsers().contains(user.get()))
return ResponseEntity.status(HttpStatus.NOT_MODIFIED)
.body("User does not have this allergy");
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");
allergy.get().getUsers().remove(user.get());
return ResponseEntity.ok(userRepository.save(user.get()).getAllergies().stream()
.map(Allergy::getName)
.reduce("", (a, b) -> a + " " + b));
}
return Optional.empty();
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Did not find allergy, our user");
}
/**
* Deletes the specified allergy from the user with the given username.
*
* @param username the username of the user to delete the allergy from
* @param allergyName the name of the allergy to delete
* @return an Optional containing the updated User object if the operation was successful, or an empty Optional otherwise
* @throws EntityNotFoundException if the specified user or allergy cannot be found
*/
public Optional<User> deleteAllergy(String username, String allergyName) {
Optional<User> user = userRepository.findByUsername(username);
Optional<Allergy> allergy = allergyRepository.findByName(allergyName);
if (user.isPresent() && allergy.isPresent()) {
if(user.get().deleteAllergy(allergy.get())) return Optional.of(userRepository.save(user.get()));
} else if (user.isEmpty()) {
throw new EntityNotFoundException("User not found");
} else {
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