Skip to content
Snippets Groups Projects
Commit 145de62a authored by Victor Ekholt Gunrell Kaste's avatar Victor Ekholt Gunrell Kaste
Browse files

feat: changed createGoal method to accept group goals, and added get group method.

parent e5d3ae84
No related branches found
No related tags found
1 merge request!95Feat/group saving
package no.ntnu.idi.stud.savingsapp.service;
import no.ntnu.idi.stud.savingsapp.dto.goal.GroupUserDTO;
import no.ntnu.idi.stud.savingsapp.model.goal.Goal;
import no.ntnu.idi.stud.savingsapp.model.goal.Group;
import org.springframework.stereotype.Service;
import java.util.List;
......@@ -20,7 +22,7 @@ public interface GoalService {
* @param userId The ID of the user for whom the goal is being created.
* @return The newly created and persisted Goal object.
*/
Goal createGoal(Goal goal, long userId);
Goal createGoal(Goal goal, List<GroupUserDTO> GroupUsers, long userId);
/**
* Retrieves all goals associated with a specific user.
......@@ -39,4 +41,12 @@ public interface GoalService {
* @return A goal object associated with the specified user.
*/
Goal getGoal(long goalId);
/**
* Retrieves a group associated with a specific goal
*
* @param goalId the goal that the group contains
* @return A group object associated with the specified goal
*/
Group getGroup(Long goalId);
}
......@@ -2,15 +2,20 @@ package no.ntnu.idi.stud.savingsapp.service.impl;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import no.ntnu.idi.stud.savingsapp.dto.goal.GroupUserDTO;
import no.ntnu.idi.stud.savingsapp.exception.goal.GoalNotFoundException;
import no.ntnu.idi.stud.savingsapp.exception.goal.GroupNotFoundException;
import no.ntnu.idi.stud.savingsapp.model.goal.Challenge;
import no.ntnu.idi.stud.savingsapp.model.goal.Goal;
import no.ntnu.idi.stud.savingsapp.model.goal.Group;
import no.ntnu.idi.stud.savingsapp.model.user.User;
import no.ntnu.idi.stud.savingsapp.repository.GoalRepository;
import no.ntnu.idi.stud.savingsapp.repository.GroupRepository;
import no.ntnu.idi.stud.savingsapp.service.ChallengeService;
import no.ntnu.idi.stud.savingsapp.service.GoalService;
import no.ntnu.idi.stud.savingsapp.service.UserService;
......@@ -33,6 +38,9 @@ public class GoalServiceImpl implements GoalService {
@Autowired
private ChallengeService challengeService;
@Autowired
private GroupRepository groupRepository;
/**
* Creates a new goal for a specific user and generates associated challenges.
*
......@@ -41,12 +49,33 @@ public class GoalServiceImpl implements GoalService {
* @return The newly created Goal, now populated with generated challenges and persisted in the database.
*/
@Override
public Goal createGoal(Goal goal, long userId) {
public Goal createGoal(Goal goal, List<GroupUserDTO> GroupUsers, long userId) {
User user = userService.findById(userId);
goal.setCreatedAt(Timestamp.from(Instant.now()));
goal.setUser(user);
List<Challenge> challenges = challengeService.generateChallenges(goal, user);
goal.setChallenges(challenges);
//Create group goal if GroupUsers were added
if(GroupUsers != null && !GroupUsers.isEmpty()) {
List<Goal> groupGoalList = new ArrayList<>();
for (GroupUserDTO groupUserDTO : GroupUsers) {
Goal groupGoal = new Goal();
User groupUser = userService.findById(groupUserDTO.getUserId());
groupGoal.setCreatedAt(Timestamp.from(Instant.now()));
groupGoal.setUser(groupUser);
List<Challenge> challengeList = challengeService.generateChallenges(groupGoal, groupUser);
groupGoal.setChallenges(challengeList);
goalRepository.save(groupGoal);
groupGoalList.add(groupGoal);
}
Group group = new Group();
group.setCreator(user);
group.setGoals(groupGoalList);
groupRepository.save(group);
}
return goalRepository.save(goal);
}
......@@ -61,6 +90,12 @@ public class GoalServiceImpl implements GoalService {
return goalRepository.findByUser_Id(userId);
}
/**
* Retrieves goal associated with a given goal ID.
*
* @param goalId The ID of the user whose goals are to be retrieved.
* @return The goal associated with the given goal ID.
*/
@Override
public Goal getGoal(long goalId) {
Optional<Goal> optionalGoal = goalRepository.findById(goalId);
......@@ -71,4 +106,21 @@ public class GoalServiceImpl implements GoalService {
throw new GoalNotFoundException();
}
}
/**
*
*
* @param goalId the goal that the group contains
* @return The group containing the goal
*/
@Override
public Group getGroup(Long goalId) {
Optional<Group> optionalGroup = groupRepository.findBygGoals_Id(goalId);
if (optionalGroup.isPresent()) {
return optionalGroup.get();
} else {
log.error("[GoalServiceImpl:getGoal] Group is not found from goal, id: {}", goalId);
throw new GroupNotFoundException();
}
}
}
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