From 81f6308c446a10c83f4613df24242fde7c6548da Mon Sep 17 00:00:00 2001 From: birkon <birkon@stud.ntnu.no> Date: Wed, 26 Apr 2023 14:14:13 +0200 Subject: [PATCH] added one to one connection --- .../v233/SmartMat/entity/group/Fridge.java | 6 +++-- .../v233/SmartMat/entity/group/Group.java | 5 +++++ .../repository/group/FridgeRepository.java | 2 +- .../SmartMat/service/group/FridgeService.java | 6 ++--- .../group/FridgeRepositoryTest.java | 22 ++++++++++++++----- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java index 191f3bd3..e37e378e 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java @@ -31,8 +31,10 @@ public class Fridge{ @Column(name = "fridge_id") long fridgeId; - @Column(name = "group_id") - long groupId; + @OneToOne + @JoinColumn(name = "group_id") + @JsonIgnoreProperties("fridge") + Group group; @OneToMany @JoinColumn(name = "fridge_id") diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java index 032e2045..c5fa7789 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java @@ -61,6 +61,11 @@ public class Group { this.user.add(userGroupTable); } + @OneToOne + @JoinColumn(name = "group_id") + @JsonIgnoreProperties("group") + private Fridge fridge; + @ManyToMany @JoinTable(name = "group_achievement", joinColumns = @JoinColumn(name = "group_id"), diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepository.java index ae9b147f..8d8ed7de 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepository.java @@ -12,5 +12,5 @@ public interface FridgeRepository extends JpaRepository<Fridge, Long> { * @param groupId the id of the group * @return an Optional containing the fridge of the group if it exists */ - Optional<Fridge> findByGroupId(long groupId); + Optional<Fridge> findByGroupGroupId(long groupId); } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java index d6a0d519..0b6df88f 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java @@ -35,7 +35,7 @@ public class FridgeService { * @return the fridge of the group */ public Optional<Fridge> getFridgeByGroupId(long groupId) { - return fridgeRepository.findByGroupId(groupId); + return fridgeRepository.findByGroupGroupId(groupId); } /** @@ -59,7 +59,7 @@ public class FridgeService { */ public boolean addProductToFridge(long groupId, long ean) { Optional<Product> product = productService.getProductById(ean); - Fridge fridge = fridgeRepository.findByGroupId(groupId).orElseThrow(() -> new IllegalArgumentException("Fridge does not exist")); + Fridge fridge = fridgeRepository.findByGroupGroupId(groupId).orElseThrow(() -> new IllegalArgumentException("Fridge does not exist")); if (product.isPresent()) { Product productToAdd = product.get(); @@ -84,7 +84,7 @@ public class FridgeService { */ public boolean removeProductFromFridge(long groupId, long ean, Date purchaseDate) { Optional<Product> product = productService.getProductById(ean); - Fridge fridge = fridgeRepository.findByGroupId(groupId).orElseThrow(() -> new IllegalArgumentException("Fridge does not exist")); + Fridge fridge = fridgeRepository.findByGroupGroupId(groupId).orElseThrow(() -> new IllegalArgumentException("Fridge does not exist")); diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepositoryTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepositoryTest.java index 2d576b9e..df369735 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepositoryTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepositoryTest.java @@ -1,6 +1,7 @@ package ntnu.idatt2016.v233.SmartMat.repository.group; import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; +import ntnu.idatt2016.v233.SmartMat.entity.group.Group; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -18,31 +19,42 @@ class FridgeRepositoryTest { @Autowired TestEntityManager entityManager; Fridge fridge; + + Group group; + @BeforeEach void setUp() { fridge = new Fridge(); - fridge.setGroupId(1L); + group = new Group(); + + fridge.setGroup(group); + group.setFridge(fridge); + + entityManager.persist(group); entityManager.persist(fridge); } @Test void shouldFindByGroupId() { - assertEquals(fridge, fridgeRepository.findByGroupId(fridge.getGroupId()).get()); + assertEquals(fridge, fridgeRepository.findByGroupGroupId(fridge.getGroup().getGroupId()).get()); } @Test void shouldNotFindByGroupId() { - assertFalse(fridgeRepository.findByGroupId(2L).isPresent()); + assertFalse(fridgeRepository.findByGroupGroupId(2L).isPresent()); } @Test void shouldSave() { Fridge fridge2 = new Fridge(); - fridge2.setGroupId(2L); + Group group2 = new Group(); + entityManager.persist(group2); + fridge2.setGroup(group2); + group2.setFridge(fridge2); fridgeRepository.save(fridge2); - assertEquals(fridge2, fridgeRepository.findByGroupId(fridge2.getGroupId()).get()); + assertEquals(fridge2, fridgeRepository.findByGroupGroupId(fridge2.getGroup().getGroupId()).get()); } -- GitLab