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

added methods to group and fridge services

parent 81f6308c
No related branches found
No related tags found
No related merge requests found
......@@ -3,8 +3,10 @@ package ntnu.idatt2016.v233.SmartMat.service.group;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.fridgeProduct.FridgeProductAsso;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.repository.group.FridgeRepository;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository;
import ntnu.idatt2016.v233.SmartMat.service.product.ProductService;
import org.springframework.stereotype.Service;
......@@ -15,9 +17,9 @@ import java.util.Optional;
/**
* Service for management of a group fridge
*
* @author Anders Austlid
* @version 1.0.1
* @since 25.04.2023
* @author Anders Austlid & Birk
* @version 1.2
* @since 26.04.2023
*/
@AllArgsConstructor
@Service
......@@ -28,6 +30,8 @@ public class FridgeService {
private final FridgeProductAssoService fridgeProductAssoService;
private final GroupRepository groupRepository;
/**
* Gets the fridge of a group
*
......@@ -112,4 +116,20 @@ public class FridgeService {
return;
fridgeRepository.save(fridge);
}
/**
* Adds a group to a fridge
* @param fridgeId the id of the fridge
* @param groupId the id of the group
*/
public void addGroupToFridge(long fridgeId, long groupId) {
Optional<Fridge> fridge = fridgeRepository.findById(fridgeId);
Optional<Group> group = groupRepository.findByGroupId(groupId);
if (fridge.isPresent() && group.isPresent()) {
fridge.get().setGroup(group.get());
group.get().setFridge(fridge.get());
groupRepository.save(group.get());
fridgeRepository.save(fridge.get());
}
}
}
package ntnu.idatt2016.v233.SmartMat.service.group;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.repository.group.FridgeRepository;
import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository;
import ntnu.idatt2016.v233.SmartMat.util.GroupUtil;
import org.springframework.stereotype.Service;
......@@ -12,9 +14,9 @@ import java.util.Optional;
/**
* Service for groups
*
* @author Anders Austlid
* @version 1.1
* @since 25.04.2023
* @author Anders Austlid & Birk
* @version 1.2
* @since 26.04.2023
*/
@AllArgsConstructor
@Service
......@@ -22,6 +24,8 @@ public class GroupService {
private final GroupRepository groupRepository;
private final FridgeRepository fridgeRepository;
/**
* Gets a group by its name
* @param name the name of the group
......@@ -121,4 +125,20 @@ public class GroupService {
}
return Optional.empty();
}
/**
* Adds a group to a fridge
* @param fridgeId the id of the fridge
* @param groupId the id of the group
*/
public void addFridgeToGroup(long fridgeId, long groupId) {
Optional<Fridge> fridge = fridgeRepository.findById(fridgeId);
Optional<Group> group = groupRepository.findByGroupId(groupId);
if (fridge.isPresent() && group.isPresent()) {
fridge.get().setGroup(group.get());
group.get().setFridge(fridge.get());
groupRepository.save(group.get());
fridgeRepository.save(fridge.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