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 c28866ba7a3356babfdb85054cdaeb456c7aae7a..8a449dca0341cb1548bce2ccca4379615cf5bae4 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 @@ -2,6 +2,7 @@ package ntnu.idatt2016.v233.SmartMat.controller.user; 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.entity.user.User; @@ -95,4 +96,10 @@ public class UserController { } + @PostMapping("/addAllergy") + public ResponseEntity<Boolean> addAllergyToUser(@RequestBody AllergyRequest allergyRequest) { + return userService.addAllergyToUser(allergyRequest.getUsername(), allergyRequest.getAllergyName()) + .map(user -> ResponseEntity.ok(user.getAllergies().size() > 0)) + .orElseGet(() -> ResponseEntity.notFound().build()); + } } \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/AllergyRequest.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/AllergyRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..88b9e45f9f5e5bf513f1309697c6ea7f8a8b39ba --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/AllergyRequest.java @@ -0,0 +1,29 @@ +package ntnu.idatt2016.v233.SmartMat.dto.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * This class represents a request for a shopping list. + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class AllergyRequest { + + /** + * The name of the user. + */ + private String username; + + /** + * The unique name of the allergy. + */ + private String allergyName; + +} + + diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepository.java index 5b6f6e482e80e9b3923054519c1ad8d4d4b74c0a..94a4bf58a8ff87f2765a36abf4c3510971ef566d 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepository.java @@ -5,6 +5,7 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; +import java.util.Optional; /** * Repository for allergies @@ -20,4 +21,12 @@ public interface AllergyRepository extends JpaRepository<Allergy, String> { * @return list of allergies */ List<Allergy> findAllByProductsEan(long id); - } + + /** + * Finds a allergy by its name + * @param name the name of the allergy + * @return optional of allergy if found + */ + Optional<Allergy> findByName(String name); + +} 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 6dc2d77222dbf744fa5f6a10f525b5416d0c5e84..b3990b550255d423bfeeeb5a609244291a6ee399 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 @@ -3,14 +3,18 @@ package ntnu.idatt2016.v233.SmartMat.service.user; import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; +import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy; import ntnu.idatt2016.v233.SmartMat.entity.user.AuthorityTable; import ntnu.idatt2016.v233.SmartMat.entity.user.User; +import ntnu.idatt2016.v233.SmartMat.repository.AllergyRepository; import ntnu.idatt2016.v233.SmartMat.repository.user.AuthoritesRepository; import ntnu.idatt2016.v233.SmartMat.repository.user.UserRepository; import ntnu.idatt2016.v233.SmartMat.service.RecipeService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; +import jakarta.persistence.EntityNotFoundException; + import java.util.List; import java.util.Optional; @@ -31,7 +35,8 @@ public class UserService { private AuthoritesService authoritesService; private AuthoritesRepository authoritesRepository; - + + AllergyRepository allergyRepository; /** * gets user from username out of database @@ -145,4 +150,27 @@ public class UserService { return userRepository.save(user); } -} + + /** + * 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 + */ + public Optional<User> 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"); + } + return Optional.empty(); + } +} \ No newline at end of file