Skip to content

Commit

Permalink
Merge pull request #12 from GDSC-snowflowerthon/feature/7
Browse files Browse the repository at this point in the history
Feature/7
  • Loading branch information
Haewonny authored Jan 12, 2024
2 parents b5dc491 + 83b6c2f commit 2bd7e81
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tenten.blooming.domain.subgoal.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tenten.blooming.domain.goal.entity.Goal;
import tenten.blooming.domain.goal.repository.GoalRepository;
import tenten.blooming.domain.subgoal.dto.GetSubgoalInfoByUserIdRequest;
import tenten.blooming.domain.subgoal.dto.SubgoalResponse;
import tenten.blooming.domain.subgoal.service.SubgoalService;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequiredArgsConstructor
public class SubgoalController {

@Autowired private final GoalRepository goalRepository;
@Autowired private final SubgoalService subgoalService;
@PostMapping("/subgoal/{goalId}/detail/{subgoalId}")
public ResponseEntity<List<LocalDate>> updateSubgoal(
@PathVariable("goalId") Long goalId,
@PathVariable("subgoalId") Long subgoalId
) {
List<LocalDate> doneDates = subgoalService.updateSubgoal(subgoalId);

return ResponseEntity.ok(doneDates);
}

@GetMapping("/subgoal/{goalId}/progress")
public ResponseEntity<SubgoalResponse> getSubgoalInfoByUserId(
@PathVariable Long goalId,
@RequestBody @Validated GetSubgoalInfoByUserIdRequest request
) {
SubgoalResponse subgoalResponse = subgoalService.getSubgoalInfoByUserId(request.getUserId());

return ResponseEntity.ok(subgoalResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tenten.blooming.domain.subgoal.dto;

import lombok.*;

@Data
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class GetSubgoalInfoByUserIdRequest {
private Long userId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package tenten.blooming.domain.subgoal.dto;

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;
import java.util.List;

@Data
@NoArgsConstructor
@Getter @Setter
public class SubgoalResponse {
private String goalName;
private Long goalId;
private LocalDate goalCreateDate;
private List<SubgoalInfo> SubgoalList;

@Data
@NoArgsConstructor
public static class SubgoalInfo {
private Long subgoalId;
private String subgoalName;
private List<LocalDate> doneDateList;
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tenten.blooming.domain.subgoal.dto;

import lombok.Data;

@Data
public class UpdateSubgoalRequest {
private Long goalId;
private Long subgoalId;
}
39 changes: 39 additions & 0 deletions src/main/java/tenten/blooming/domain/subgoal/entity/Subgoal.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import tenten.blooming.domain.goal.entity.Goal;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
Expand Down Expand Up @@ -39,4 +41,41 @@ public class Subgoal {
@JoinColumn(name = "goal_id")
private Goal goal;

//==생성 메서드=//
public static Subgoal createSubgoal(Goal goal) {
Subgoal subgoal = new Subgoal();
subgoal.setGoal(goal);
subgoal.setSubgoalName("작성 중");

return subgoal;
}

//==비즈니스 로직==//
/**
* 해당 subgoal의 과제 체크 정보(doneDates 리스트) 반환
*/
public List<LocalDate> getDoneDates() {
List<LocalDate> doneDates= Arrays.asList(doneDate1, doneDate2, doneDate3, doneDate4, doneDate5, doneDate6, doneDate7, doneDate8, doneDate9, doneDate10);

return doneDates;
}

/**
* doneDate를 현재 날짜로 입력하고, 업데이트된 doneDates 리스트 반환
* @return doneDateCnt
*/
public List<LocalDate> addDoneDate() {
List<LocalDate> doneDates = getDoneDates();

for(int i = 0; i < doneDates.size(); i++) {
if(doneDates.get(i) == null) {
if(i != 0 && (doneDates.get(i-1) == LocalDate.now())) {
throw new IllegalStateException("이미 체크된 TASK입니다.");
}
doneDates.set(i, LocalDate.now());
}
}

return doneDates;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package tenten.blooming.domain.subgoal.service;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tenten.blooming.domain.goal.entity.Goal;
import tenten.blooming.domain.goal.repository.GoalRepository;
import tenten.blooming.domain.subgoal.dto.SubgoalResponse;
import tenten.blooming.domain.subgoal.entity.Subgoal;
import tenten.blooming.domain.subgoal.repository.SubgoalRepository;
import tenten.blooming.domain.user.entity.User;
import tenten.blooming.domain.user.repository.UserRepository;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class SubgoalService {

@Autowired private final UserRepository userRepository;
@Autowired private final SubgoalRepository subgoalRepository;
@Autowired private final GoalRepository goalRepository;
/**
* Subgoal 생성
*/
@Transactional
public Subgoal createSubgoal(Goal goal) {
Subgoal subgoal = Subgoal.createSubgoal(goal);

return subgoalRepository.save(subgoal);
}

/**
* subgoal의 과제 한 개 완료
*/
@Transactional
public List<LocalDate> updateSubgoal(Long subgoalId) {
Subgoal findSubgoal = subgoalRepository.findById(subgoalId).orElse(null);

return findSubgoal.addDoneDate();
}

public List<Goal> getGoalByUserId(Long userId) {
User findUser = userRepository.findById(userId).orElse(null);

return findUser.getGoals();
}

public SubgoalResponse getSubgoalInfoByUserId(Long userId) {
List<Goal> goals = getGoalByUserId(userId);
Goal goal = goals.get(goals.size() - 1);
List<Subgoal> subgoals = goal.getSubgoals();
SubgoalResponse subgoalResponse = new SubgoalResponse();
List<SubgoalResponse.SubgoalInfo> subgoalInfoList = new ArrayList<>();

for(Subgoal subgoal : subgoals) {
SubgoalResponse.SubgoalInfo subgoalInfo = new SubgoalResponse.SubgoalInfo();
subgoalInfo.setSubgoalId(subgoal.getSubgoalId());
subgoalInfo.setSubgoalName(subgoal.getSubgoalName());
subgoalInfo.setDoneDateList(subgoal.getDoneDates());
subgoalInfoList.add(subgoalInfo);
}

subgoalResponse.setGoalName(goal.getGoalName());
subgoalResponse.setGoalId(goal.getGoalId());
subgoalResponse.setGoalCreateDate(goal.getCreatedAt());
subgoalResponse.setSubgoalList(subgoalInfoList);

return subgoalResponse;
}
}

0 comments on commit 2bd7e81

Please sign in to comment.