From 5370797e557fa492f298127ced948ed25db52704 Mon Sep 17 00:00:00 2001
From: Stian Lyng <stianlyng@protonmail.com>
Date: Wed, 26 Apr 2023 11:09:49 +0200
Subject: [PATCH] create endpoint for linking allergy to user

---
 .../controller/user/UserController.java       |  7 ++++
 .../SmartMat/dto/request/AllergyRequest.java  | 29 +++++++++++++++++
 .../repository/AllergyRepository.java         | 11 ++++++-
 .../SmartMat/service/user/UserService.java    | 32 +++++++++++++++++--
 4 files changed, 76 insertions(+), 3 deletions(-)
 create mode 100644 src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/AllergyRequest.java

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 c28866ba..8a449dca 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 00000000..88b9e45f
--- /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 5b6f6e48..94a4bf58 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 6dc2d772..b3990b55 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
-- 
GitLab