Skip to content

Commit f5b95a2

Browse files
authored
Merge pull request #62 from 9ITHON/dev
Dev
2 parents 818d5a7 + d23311d commit f5b95a2

File tree

88 files changed

+2614
-208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2614
-208
lines changed

β€Žbackend/FETCH_HEADβ€Ž

Whitespace-only changes.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.together.backend.domain.calendar.controller;
2+
3+
import com.together.backend.domain.calendar.dto.CalendarDetailResponse;
4+
import com.together.backend.domain.calendar.dto.CalendarRecordRequest;
5+
import com.together.backend.domain.calendar.dto.CalendarRecordResponse;
6+
import com.together.backend.domain.calendar.dto.CalendarSummaryResponse;
7+
import com.together.backend.domain.calendar.service.CalendarService;
8+
import com.together.backend.global.common.BaseResponse;
9+
import com.together.backend.global.common.BaseResponseStatus;
10+
import com.together.backend.global.security.oauth2.dto.CustomOAuth2User;
11+
import com.together.backend.domain.user.model.entity.User;
12+
import com.together.backend.domain.user.repository.UserRepository;
13+
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.ResponseEntity;
15+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
16+
import org.springframework.web.bind.annotation.*;
17+
18+
import java.time.DateTimeException;
19+
import java.time.LocalDate;
20+
import java.util.List;
21+
import java.util.Optional;
22+
23+
@RestController
24+
@RequestMapping("/api/calendar")
25+
public class CalendarController {
26+
private final CalendarService calendarService;
27+
private final UserRepository userRepository;
28+
29+
public CalendarController(CalendarService calendarService, UserRepository userRepository) {
30+
this.calendarService = calendarService;
31+
this.userRepository = userRepository;
32+
}
33+
34+
@PostMapping
35+
public ResponseEntity<BaseResponse<List<CalendarRecordResponse>>> saveRecord(
36+
@RequestBody CalendarRecordRequest request,
37+
@AuthenticationPrincipal CustomOAuth2User customUser
38+
) {
39+
if (customUser == null) {
40+
return ResponseEntity
41+
.status(HttpStatus.UNAUTHORIZED)
42+
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
43+
}
44+
// μ‹€μ œ DB의 User 쑰회
45+
String email = customUser.getEmail();
46+
Optional<User> userOpt = userRepository.findByEmail(email);
47+
if (userOpt.isEmpty()) {
48+
return ResponseEntity
49+
.status(HttpStatus.UNAUTHORIZED)
50+
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
51+
}
52+
User user = userOpt.get(); // λ˜λŠ” userOpt.orElseThrow(() -> ...);
53+
54+
try {
55+
calendarService.saveCalendarRecord(user, request);
56+
return ResponseEntity.ok(new BaseResponse<>(BaseResponseStatus.OK, null));
57+
} catch (Exception e) {
58+
return ResponseEntity
59+
.status(HttpStatus.INTERNAL_SERVER_ERROR)
60+
.body(new BaseResponse<>(BaseResponseStatus.INTERNAL_SERVER_ERROR));
61+
}
62+
}
63+
64+
@GetMapping("/summary")
65+
public ResponseEntity<BaseResponse<List<CalendarSummaryResponse>>> getCalendarSummary(
66+
@RequestParam("month") String month,
67+
@AuthenticationPrincipal CustomOAuth2User customUser
68+
) {
69+
if(customUser == null) {
70+
return ResponseEntity
71+
.status(HttpStatus.UNAUTHORIZED)
72+
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
73+
}
74+
75+
// DB의 User μ—”ν‹°ν‹° 쑰회
76+
Optional<User> userOpt = userRepository.findByEmail(customUser.getEmail());
77+
if(userOpt.isEmpty()) {
78+
return ResponseEntity
79+
.status(HttpStatus.UNAUTHORIZED)
80+
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
81+
}
82+
User user = userOpt.get();
83+
84+
try {
85+
List<CalendarSummaryResponse> result = calendarService.getCalendarSummary(user, month);
86+
return ResponseEntity.ok(new BaseResponse<>(result));
87+
} catch (DateTimeException e) {
88+
return ResponseEntity.badRequest()
89+
.body(new BaseResponse<>(BaseResponseStatus.BAD_REQUEST, null));
90+
} catch (Exception e) {
91+
return ResponseEntity
92+
.status(HttpStatus.INTERNAL_SERVER_ERROR)
93+
.body(new BaseResponse<>(BaseResponseStatus.INTERNAL_SERVER_ERROR));
94+
}
95+
}
96+
97+
@GetMapping("/detail")
98+
public ResponseEntity<BaseResponse<CalendarDetailResponse>> getCalendarDetail(
99+
@RequestParam("date") String dateStr,
100+
@AuthenticationPrincipal CustomOAuth2User customUser
101+
) {
102+
// 인증 확인
103+
if (customUser == null) {
104+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
105+
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
106+
}
107+
108+
// μœ μ € μ—”ν‹°ν‹° 쑰회
109+
Optional<User> userOpt = userRepository.findByEmail(customUser.getEmail());
110+
if(userOpt.isEmpty()) {
111+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
112+
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
113+
}
114+
115+
User user = userOpt.get();
116+
117+
try {
118+
LocalDate date = LocalDate.parse(dateStr);
119+
120+
CalendarDetailResponse result = calendarService.getCalendarDetail(user, date);
121+
return ResponseEntity.ok(new BaseResponse<>(result));
122+
} catch (DateTimeException e) {
123+
return ResponseEntity.badRequest()
124+
.body(new BaseResponse<>(BaseResponseStatus.BAD_REQUEST, null));
125+
} catch (Exception e) {
126+
return ResponseEntity
127+
.status(HttpStatus.INTERNAL_SERVER_ERROR)
128+
.body(new BaseResponse<>(BaseResponseStatus.INTERNAL_SERVER_ERROR));
129+
}
130+
}
131+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.together.backend.domain.calendar.dto;
2+
3+
import com.together.backend.domain.calendar.model.entity.CondomUsage;
4+
import lombok.*;
5+
6+
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
7+
public class CalendarDetailResponse {
8+
private String date;
9+
private Boolean takenPill;
10+
private CondomUsage usedCondom;
11+
private Boolean hadSex;
12+
private String moodEmoji;
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.together.backend.domain.calendar.dto;
2+
3+
import com.together.backend.domain.calendar.model.entity.CondomUsage;
4+
import lombok.*;
5+
6+
// μΊ˜λ¦°λ” 기둝 λ“±λ‘μš© DTO
7+
8+
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
9+
public class CalendarRecordRequest {
10+
private String date;
11+
private Boolean hadSex;
12+
private Boolean takenPill;
13+
private CondomUsage usedCondom;
14+
private String moodEmoji;
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.together.backend.domain.calendar.dto;
2+
3+
import com.together.backend.domain.calendar.model.entity.CondomUsage;
4+
import lombok.*;
5+
6+
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
7+
public class CalendarRecordResponse {
8+
private String date;
9+
private Boolean hadSex;
10+
private Boolean takenPill;
11+
private CondomUsage usedCondom; // Enum!
12+
private String moodEmoji;
13+
}
14+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.together.backend.domain.calendar.dto;
2+
3+
import lombok.*;
4+
5+
@Getter @Setter
6+
@NoArgsConstructor @AllArgsConstructor @Builder
7+
public class CalendarSummaryResponse {
8+
private String date;
9+
private String moodEmoji;
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.together.backend.domain.calendar.model.entity;
2+
3+
import jakarta.persistence.Column;
4+
import java.time.LocalDateTime;
5+
import com.together.backend.domain.user.model.entity.User;
6+
import jakarta.persistence.*;
7+
import lombok.*;
8+
9+
10+
@Entity
11+
@Table(name = "basic_record")
12+
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
13+
public class BasicRecord {
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long basicRecordId;
17+
18+
@ManyToOne(fetch = FetchType.LAZY)
19+
@JoinColumn(name = "user_id")
20+
private User user;
21+
22+
@Column(name = "mood_emoji", length = 10)
23+
private String moodEmoji;
24+
25+
@Column(name = "occured_at")
26+
private LocalDateTime occuredAt;
27+
28+
// 관계: 볡용 기둝
29+
@ManyToOne
30+
@JoinColumn(name = "intake_id")
31+
private IntakeRecord intakeRecord;
32+
}
33+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.together.backend.domain.calendar.model.entity;
2+
3+
public enum CondomUsage {
4+
YES,
5+
NO,
6+
UNKNOWN
7+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.together.backend.domain.calendar.model.entity;
2+
3+
4+
import java.time.LocalDate;
5+
import java.time.LocalTime;
6+
import java.util.List;
7+
8+
import com.together.backend.domain.pill.model.UserPill;
9+
import jakarta.persistence.*;
10+
import lombok.*;
11+
12+
@Entity
13+
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
14+
public class IntakeRecord {
15+
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long intakeId;
17+
18+
@ManyToOne(fetch = FetchType.LAZY)
19+
@JoinColumn(name = "user_pill_id")
20+
private UserPill userPill;
21+
22+
private LocalDate intakeDate;
23+
// private LocalTime intakeTime;
24+
private Boolean isTaken;
25+
26+
@Enumerated(EnumType.STRING)
27+
private IntakeType type; // λ³΅μš©νƒ€μž… (REAL/FAKE/BREAK)
28+
29+
@OneToMany(mappedBy = "intakeRecord", cascade = CascadeType.ALL, orphanRemoval = true)
30+
private List<BasicRecord> basicRecords;
31+
32+
}
33+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.together.backend.domain.calendar.model.entity;
2+
3+
public enum IntakeType {
4+
REAL, // μ‹€λ³΅μš©
5+
FAKE, // μœ„μ•½
6+
BREAK // νœ΄μ•½
7+
}

0 commit comments

Comments
Β (0)