diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Allergy.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Allergy.java similarity index 64% rename from src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Allergy.java rename to src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Allergy.java index 03a399badfc73352f57539e4041147c57c950306..281af706202840af1f7e76ae3d7cd27d12b8ffff 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Allergy.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Allergy.java @@ -1,18 +1,22 @@ -package ntnu.idatt2016.v233.SmartMat.entity; +package ntnu.idatt2016.v233.SmartMat.entity.product; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.util.List; + /** * Allergy is an entity class representing an allergy * * @author Stian Lyng and Anders - * @version 1.1.001 + * @version 1.2 * @since 19.04.2023 * */ @@ -29,4 +33,8 @@ public class Allergy{ String name; @Column(name = "allergy_description") String description; + + @ManyToMany(mappedBy = "allergies") + @JsonIgnoreProperties({"allergies"}) + private List<Product> products; } \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java index 87bd42e05cd80a8b11c24cdc74bdac38de7f3f74..693aa5ec2f552516abc89188563576aadc15accf 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java @@ -1,5 +1,6 @@ package ntnu.idatt2016.v233.SmartMat.entity.product; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; @@ -21,5 +22,6 @@ public class Category { String description; @OneToMany(mappedBy = "category") + @JsonIgnoreProperties({"category"}) private List<Product> products; } \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java index b43ff40d842dcfded5545e059eaa9df3410ee9d5..0484a3d5165511f72f542bfcc3efb4ba7808f15e 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java @@ -1,5 +1,6 @@ package ntnu.idatt2016.v233.SmartMat.entity.product; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import jakarta.persistence.*; import lombok.AllArgsConstructor; @@ -7,6 +8,8 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.util.List; + /** * Product is an entity class representing a product in the system. * All other info about the product is fetched from api call on fronted. @@ -42,4 +45,13 @@ public class Product{ @Column(name = "expiration_date") int expirationDate; + + @ManyToMany + @JsonIgnoreProperties({"products"}) + @JoinTable( + name = "product_allergy", + joinColumns = @JoinColumn(name = "ean"), + inverseJoinColumns = @JoinColumn(name = "allergy_name")) + List<Allergy> allergies; + } 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 1cdf7d9590ebd1e6732da2aba4997c7bbafb272f..5b6f6e482e80e9b3923054519c1ad8d4d4b74c0a 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepository.java @@ -1,16 +1,23 @@ package ntnu.idatt2016.v233.SmartMat.repository; -import ntnu.idatt2016.v233.SmartMat.entity.Allergy; +import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + /** * Repository for allergies * - * @author Stian Lyng + * @author Stian Lyng and birk * @version 1.2 - * @since 19.04.2023 + * @since 21.04.2023 */ public interface AllergyRepository extends JpaRepository<Allergy, String> { - + /** + * Finds all allergies by product id + * @param id the id of the product + * @return list of allergies + */ + List<Allergy> findAllByProductsEan(long id); } diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepositoryTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepositoryTest.java index 6870826847c981e71d0ea71f5b6f6e06dac0c3ed..8e19423f0573cf6127fbdad566b24578d9a37fed 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepositoryTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/AllergyRepositoryTest.java @@ -1,6 +1,7 @@ package ntnu.idatt2016.v233.SmartMat.repository; -import ntnu.idatt2016.v233.SmartMat.entity.Allergy; +import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy; +import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -17,6 +18,9 @@ class AllergyRepositoryTest { @Autowired private AllergyRepository allergyRepository; + @Autowired + private ProductRepository productRepository; + @Test @DisplayName("Test findByName") void testFindByName() { @@ -85,4 +89,33 @@ class AllergyRepositoryTest { Assertions.assertTrue(allergies.contains(allergy1)); Assertions.assertTrue(allergies.contains(allergy2)); } + + @Test + void findByProductId() { + Allergy allergy = Allergy.builder() + .name("testName") + .description("testDescription") + .build(); + + Product product = Product.builder() + .ean(1234567890123L) + .name("Test Product") + .description("This is a test product") + .allergies(List.of(allergy)) + .build(); + + + + allergyRepository.save(allergy); + + productRepository.save(product); + + Assertions.assertNotNull(allergyRepository.findAllByProductsEan(product.getEan())); + + allergyRepository.findAllByProductsEan(product.getEan()).forEach(allergy1 -> { + Assertions.assertNull(allergy1.getProducts()); + }); + + + } }