Skip to content

Commit 347c5f8

Browse files
committed
feat: 기초 조회기능 구현 및 엔티티 일부 수정
1 parent e3e975b commit 347c5f8

File tree

7 files changed

+207
-12
lines changed

7 files changed

+207
-12
lines changed
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
package com.back.domain.study.plan.controller;
22

3+
import com.back.domain.study.plan.dto.StudyPlanCreateRequest;
4+
import com.back.domain.study.plan.dto.StudyPlanResponse;
35
import com.back.domain.study.plan.service.StudyPlanService;
6+
import com.back.global.common.dto.RsData;
47
import lombok.RequiredArgsConstructor;
5-
import org.springframework.web.bind.annotation.RequestMapping;
6-
import org.springframework.web.bind.annotation.RestController;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
10+
import org.springframework.web.bind.annotation.*;
711

812
@RestController
913
@RequiredArgsConstructor
1014
@RequestMapping("/api/plans")
1115
public class StudyPlanController {
1216
private final StudyPlanService studyPlanService;
1317

18+
@PostMapping
19+
public ResponseEntity<RsData<StudyPlanResponse>> createStudyPlan(
20+
// 로그인 유저 정보 받기 @AuthenticationPrincipal CustomUserDetails user,
21+
@RequestBody StudyPlanCreateRequest request) {
22+
//커스텀 디테일 구현 시 사용 int userId = user.getId();
23+
Long userId = 1L; // 임시로 userId를 1로 설정
24+
StudyPlanResponse response = studyPlanService.createStudyPlan(userId, request);
25+
return ResponseEntity.ok(RsData.success("학습 계획이 성공적으로 생성되었습니다.", response));
26+
}
27+
28+
@DeleteMapping("/{planId}")
29+
public ResponseEntity<RsData<Void>> deleteStudyPlan(@PathVariable Long planId) {
30+
//studyPlanService.deleteStudyPlan(planId);
31+
return ResponseEntity.ok(RsData.success("학습 계획이 성공적으로 삭제되었습니다.", null));
32+
}
33+
34+
35+
1436

1537
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.back.domain.study.plan.dto;
2+
3+
import com.back.domain.study.plan.entity.Color;
4+
import com.back.domain.study.plan.entity.Frequency;
5+
import com.fasterxml.jackson.annotation.JsonFormat;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
9+
import lombok.Setter;
10+
11+
import java.time.LocalDateTime;
12+
13+
@Getter
14+
@Setter
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class StudyPlanCreateRequest {
18+
private String subject;
19+
20+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
21+
private LocalDateTime startDate;
22+
23+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
24+
private LocalDateTime endDate;
25+
26+
private Color color;
27+
28+
// RepeatRule 중첩 객체
29+
private RepeatRuleRequest repeatRule;
30+
31+
@Getter
32+
@Setter
33+
@NoArgsConstructor
34+
@AllArgsConstructor
35+
public static class RepeatRuleRequest {
36+
private Frequency frequency;
37+
private Integer intervalValue;
38+
private String byDay; // "MON" 형태의 문자열
39+
40+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
41+
private String untilDate; // "2025-12-31" 형태
42+
}
43+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.back.domain.study.plan.dto;
2+
3+
import com.back.domain.study.plan.entity.Color;
4+
import com.back.domain.study.plan.entity.Frequency;
5+
import com.back.domain.study.plan.entity.StudyPlan;
6+
import com.fasterxml.jackson.annotation.JsonFormat;
7+
import lombok.AllArgsConstructor;
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
import lombok.Setter;
11+
12+
import java.time.LocalDateTime;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
16+
@Getter
17+
@Setter
18+
@NoArgsConstructor
19+
@AllArgsConstructor
20+
public class StudyPlanResponse {
21+
private Long id;
22+
private String subject;
23+
24+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
25+
private LocalDateTime startDate;
26+
27+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
28+
private LocalDateTime endDate;
29+
30+
private Color color;
31+
32+
private Long parentPlanId;
33+
private List<StudyPlanResponse> childPlans;
34+
35+
// RepeatRule 정보
36+
private RepeatRuleResponse repeatRule;
37+
38+
@Getter
39+
@Setter
40+
@NoArgsConstructor
41+
@AllArgsConstructor
42+
public static class RepeatRuleResponse {
43+
private Frequency frequency;
44+
private Integer repeatInterval;
45+
private String byDay; // "MON" 형태의 문자열
46+
47+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
48+
private LocalDateTime until;
49+
50+
public RepeatRuleResponse(com.back.domain.study.plan.entity.RepeatRule repeatRule) {
51+
if (repeatRule != null) {
52+
this.frequency = repeatRule.getFrequency();
53+
this.repeatInterval = repeatRule.getRepeatInterval();
54+
this.byDay = repeatRule.getByDay();
55+
this.until = repeatRule.getUntilDate();
56+
}
57+
}
58+
}
59+
//엔티티를 DTO로 변환하는 생성자
60+
public StudyPlanResponse(StudyPlan studyPlan) {
61+
if (studyPlan != null) {
62+
this.id = studyPlan.getId();
63+
this.subject = studyPlan.getSubject();
64+
this.startDate = studyPlan.getStartDate();
65+
this.endDate = studyPlan.getEndDate();
66+
this.color = studyPlan.getColor();
67+
68+
// 부모 계획 ID 설정
69+
if (studyPlan.getParentPlan() != null) {
70+
this.parentPlanId = studyPlan.getParentPlan().getId();
71+
}
72+
73+
// 자식 계획들 변환
74+
if (studyPlan.getChildPlans() != null && !studyPlan.getChildPlans().isEmpty()) {
75+
this.childPlans = studyPlan.getChildPlans().stream()
76+
.map(StudyPlanResponse::new)
77+
.collect(Collectors.toList());
78+
}
79+
80+
// RepeatRule 변환
81+
if (studyPlan.getRepeatRule() != null) {
82+
this.repeatRule = new RepeatRuleResponse(studyPlan.getRepeatRule());
83+
}
84+
}
85+
}
86+
}

src/main/java/com/back/domain/study/plan/entity/RepeatRule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class RepeatRule extends BaseEntity {
2727

2828
//필요 시 요일 지정
2929
@Enumerated(EnumType.STRING)
30-
private DayOfWeek byDay;
30+
private String byDay;
3131

32-
private LocalDateTime until;
32+
private LocalDateTime untilDate;
3333
}

src/main/java/com/back/domain/study/plan/entity/StudyPlan.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import jakarta.persistence.*;
66
import lombok.Getter;
77
import lombok.NoArgsConstructor;
8+
import lombok.Setter;
89

910
import java.time.LocalDateTime;
1011
import java.util.ArrayList;
@@ -13,6 +14,7 @@
1314
@Entity
1415
@NoArgsConstructor
1516
@Getter
17+
@Setter
1618
public class StudyPlan extends BaseEntity {
1719
@ManyToOne(fetch = FetchType.LAZY)
1820
@JoinColumn(name = "user_id", nullable = false)
@@ -21,8 +23,6 @@ public class StudyPlan extends BaseEntity {
2123
@Column(nullable = false, length = 100)
2224
private String subject;
2325

24-
@Enumerated(EnumType.STRING)
25-
private StudyStatus status;
2626

2727
@Column(name = "start_date", nullable = false)
2828
private LocalDateTime startDate;

src/main/java/com/back/domain/study/plan/entity/StudyStatus.java

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,65 @@
11
package com.back.domain.study.plan.service;
22

3+
import com.back.domain.study.plan.dto.StudyPlanCreateRequest;
4+
import com.back.domain.study.plan.dto.StudyPlanResponse;
5+
import com.back.domain.study.plan.entity.RepeatRule;
6+
import com.back.domain.study.plan.entity.StudyPlan;
7+
import com.back.domain.study.plan.entity.StudyStatus;
38
import com.back.domain.study.plan.repository.StudyPlanRepository;
49
import lombok.RequiredArgsConstructor;
510
import org.springframework.stereotype.Service;
611
import org.springframework.transaction.annotation.Transactional;
712

13+
import java.time.LocalDateTime;
14+
815
@Service
916
@RequiredArgsConstructor
10-
@Transactional
17+
@Transactional(readOnly = true)
1118
public class StudyPlanService{
1219
private final StudyPlanRepository studyPlanRepository;
1320

21+
@Transactional
22+
public StudyPlanResponse createStudyPlan(Long userId, StudyPlanCreateRequest request) {
23+
24+
StudyPlan studyPlan = new StudyPlan();
25+
//studyPlan.setUser(user);
26+
studyPlan.setSubject(request.getSubject());
27+
studyPlan.setStatus(StudyStatus.TODO);
28+
studyPlan.setStartDate(request.getStartDate());
29+
studyPlan.setEndDate(request.getEndDate());
30+
studyPlan.setColor(request.getColor());
31+
studyPlan.setParentPlan(null);
32+
33+
// 반복 규칙 설정
34+
if (request.getRepeatRule() != null) {
35+
StudyPlanCreateRequest.RepeatRuleRequest repeatRuleRequest = request.getRepeatRule();
36+
RepeatRule repeatRule = new RepeatRule();
37+
repeatRule.setStudyPlan(studyPlan);
38+
repeatRule.setFrequency(repeatRuleRequest.getFrequency());
39+
repeatRule.setRepeatInterval(repeatRuleRequest.getIntervalValue() != null ? repeatRuleRequest.getIntervalValue() : 1);
40+
41+
// byDay 문자열 그대로 저장
42+
if (repeatRuleRequest.getByDay() != null && !repeatRuleRequest.getByDay().isEmpty()) {
43+
repeatRule.setByDay(repeatRuleRequest.getByDay());
44+
}
45+
46+
// untilDate 문자열을 LocalDateTime으로 변환
47+
if (repeatRuleRequest.getUntilDate() != null && !repeatRuleRequest.getUntilDate().isEmpty()) {
48+
try {
49+
LocalDateTime untilDateTime = LocalDateTime.parse(repeatRuleRequest.getUntilDate() + "T23:59:59");
50+
repeatRule.setUntilDate(untilDateTime);
51+
} catch (Exception e) {
52+
// 날짜 파싱 실패 시 무시하거나 예외 처리
53+
}
54+
}
55+
56+
studyPlan.setRepeatRule(repeatRule);
57+
}
58+
59+
StudyPlan savedStudyPlan = studyPlanRepository.save(studyPlan);
60+
return convertToResponse(savedStudyPlan);
61+
}
62+
1463

1564

1665
}

0 commit comments

Comments
 (0)