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

added one to one connection

parent b4ad7ce7
No related branches found
No related tags found
No related merge requests found
...@@ -31,8 +31,10 @@ public class Fridge{ ...@@ -31,8 +31,10 @@ public class Fridge{
@Column(name = "fridge_id") @Column(name = "fridge_id")
long fridgeId; long fridgeId;
@Column(name = "group_id") @OneToOne
long groupId; @JoinColumn(name = "group_id")
@JsonIgnoreProperties("fridge")
Group group;
@OneToMany @OneToMany
@JoinColumn(name = "fridge_id") @JoinColumn(name = "fridge_id")
......
...@@ -61,6 +61,11 @@ public class Group { ...@@ -61,6 +61,11 @@ public class Group {
this.user.add(userGroupTable); this.user.add(userGroupTable);
} }
@OneToOne
@JoinColumn(name = "group_id")
@JsonIgnoreProperties("group")
private Fridge fridge;
@ManyToMany @ManyToMany
@JoinTable(name = "group_achievement", @JoinTable(name = "group_achievement",
joinColumns = @JoinColumn(name = "group_id"), joinColumns = @JoinColumn(name = "group_id"),
......
...@@ -12,5 +12,5 @@ public interface FridgeRepository extends JpaRepository<Fridge, Long> { ...@@ -12,5 +12,5 @@ public interface FridgeRepository extends JpaRepository<Fridge, Long> {
* @param groupId the id of the group * @param groupId the id of the group
* @return an Optional containing the fridge of the group if it exists * @return an Optional containing the fridge of the group if it exists
*/ */
Optional<Fridge> findByGroupId(long groupId); Optional<Fridge> findByGroupGroupId(long groupId);
} }
...@@ -35,7 +35,7 @@ public class FridgeService { ...@@ -35,7 +35,7 @@ public class FridgeService {
* @return the fridge of the group * @return the fridge of the group
*/ */
public Optional<Fridge> getFridgeByGroupId(long groupId) { public Optional<Fridge> getFridgeByGroupId(long groupId) {
return fridgeRepository.findByGroupId(groupId); return fridgeRepository.findByGroupGroupId(groupId);
} }
/** /**
...@@ -59,7 +59,7 @@ public class FridgeService { ...@@ -59,7 +59,7 @@ public class FridgeService {
*/ */
public boolean addProductToFridge(long groupId, long ean) { public boolean addProductToFridge(long groupId, long ean) {
Optional<Product> product = productService.getProductById(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()) { if (product.isPresent()) {
Product productToAdd = product.get(); Product productToAdd = product.get();
...@@ -84,7 +84,7 @@ public class FridgeService { ...@@ -84,7 +84,7 @@ public class FridgeService {
*/ */
public boolean removeProductFromFridge(long groupId, long ean, Date purchaseDate) { public boolean removeProductFromFridge(long groupId, long ean, Date purchaseDate) {
Optional<Product> product = productService.getProductById(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"));
......
package ntnu.idatt2016.v233.SmartMat.repository.group; package ntnu.idatt2016.v233.SmartMat.repository.group;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; 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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -18,31 +19,42 @@ class FridgeRepositoryTest { ...@@ -18,31 +19,42 @@ class FridgeRepositoryTest {
@Autowired @Autowired
TestEntityManager entityManager; TestEntityManager entityManager;
Fridge fridge; Fridge fridge;
Group group;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
fridge = new Fridge(); fridge = new Fridge();
fridge.setGroupId(1L);
group = new Group();
fridge.setGroup(group);
group.setFridge(fridge);
entityManager.persist(group);
entityManager.persist(fridge); entityManager.persist(fridge);
} }
@Test @Test
void shouldFindByGroupId() { void shouldFindByGroupId() {
assertEquals(fridge, fridgeRepository.findByGroupId(fridge.getGroupId()).get()); assertEquals(fridge, fridgeRepository.findByGroupGroupId(fridge.getGroup().getGroupId()).get());
} }
@Test @Test
void shouldNotFindByGroupId() { void shouldNotFindByGroupId() {
assertFalse(fridgeRepository.findByGroupId(2L).isPresent()); assertFalse(fridgeRepository.findByGroupGroupId(2L).isPresent());
} }
@Test @Test
void shouldSave() { void shouldSave() {
Fridge fridge2 = new Fridge(); Fridge fridge2 = new Fridge();
fridge2.setGroupId(2L); Group group2 = new Group();
entityManager.persist(group2);
fridge2.setGroup(group2);
group2.setFridge(fridge2);
fridgeRepository.save(fridge2); fridgeRepository.save(fridge2);
assertEquals(fridge2, fridgeRepository.findByGroupId(fridge2.getGroupId()).get()); assertEquals(fridge2, fridgeRepository.findByGroupGroupId(fridge2.getGroup().getGroupId()).get());
} }
......
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