Skip to content

Commit 8c57bda

Browse files
authored
통증 기록 api 수정 (#132)
* fix: 통증 강도 필드 삭제 * fix: 통증 부위 신체도 기반 수정 * fix: 통증 기록 오류 수정
1 parent abbb282 commit 8c57bda

File tree

8 files changed

+115
-29
lines changed

8 files changed

+115
-29
lines changed

backend/ongi/src/main/java/ongi/health/controller/HealthRecordController.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ public ResponseEntity<PainRecordResponse> addPainRecord(
3737

3838
FamilyInfo familyInfo = familyService.getFamily(userDetails.getUser());
3939
String familyId = familyInfo.code();
40-
41-
PainRecord record = healthRecordService.addPainRecord(
40+
41+
// 여러 통증 부위를 한 번에 저장
42+
List<PainRecord.PainArea> painAreas = request.painArea().stream()
43+
.map(PainRecord.PainArea::valueOf)
44+
.toList();
45+
46+
PainRecord record = healthRecordService.addOrUpdatePainRecord(
4247
userDetails.getUser().getUuid(),
4348
request.date(),
44-
PainRecord.PainArea.valueOf(request.painArea()),
45-
PainRecord.PainLevel.valueOf(request.painLevel())
49+
painAreas
4650
);
4751

4852
// 온도 상승
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package ongi.health.dto;
22

33
import java.time.LocalDate;
4+
import java.util.List;
45

56
public record PainRecordRequest(
67
LocalDate date,
7-
String painArea,
8-
String painLevel
8+
List<String> painArea
99
) {}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package ongi.health.dto;
22

33
import java.time.LocalDate;
4+
import java.util.List;
45
import ongi.health.entity.PainRecord;
56

67
public record PainRecordResponse(
78
Long id,
89
LocalDate date,
9-
String painArea,
10-
String painLevel
10+
List<String> painArea
1111
) {
1212
public PainRecordResponse(PainRecord entity) {
1313
this(
1414
entity.getId(),
1515
entity.getDate(),
16-
entity.getPainArea().name(),
17-
entity.getPainLevel().name()
16+
entity.getPainArea().stream()
17+
.map(PainRecord.PainArea::name)
18+
.toList()
1819
);
1920
}
2021
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ongi.health.entity;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import jakarta.persistence.AttributeConverter;
7+
import jakarta.persistence.Converter;
8+
import java.util.List;
9+
10+
@Converter
11+
public class PainAreaListToJsonConverter implements AttributeConverter<List<PainRecord.PainArea>, String> {
12+
private final ObjectMapper objectMapper = new ObjectMapper();
13+
14+
@Override
15+
public String convertToDatabaseColumn(List<PainRecord.PainArea> painAreas) {
16+
try {
17+
return objectMapper.writeValueAsString(painAreas);
18+
} catch (JsonProcessingException e) {
19+
throw new RuntimeException("Error converting pain areas to JSON", e);
20+
}
21+
}
22+
23+
@Override
24+
public List<PainRecord.PainArea> convertToEntityAttribute(String json) {
25+
try {
26+
if (json == null || json.isEmpty()) {
27+
return List.of();
28+
}
29+
30+
// 기존 단일 값 데이터 처리 (예: "LEFT_SHOULDER")
31+
if (!json.startsWith("[")) {
32+
try {
33+
PainRecord.PainArea singleArea = PainRecord.PainArea.valueOf(json);
34+
return List.of(singleArea);
35+
} catch (IllegalArgumentException e) {
36+
// 기존 데이터가 유효하지 않은 경우 빈 리스트 반환
37+
return List.of();
38+
}
39+
}
40+
41+
// 새로운 JSON 배열 데이터 처리
42+
return objectMapper.readValue(json, new TypeReference<List<PainRecord.PainArea>>() {});
43+
} catch (JsonProcessingException e) {
44+
// JSON 파싱 실패 시 빈 리스트 반환
45+
return List.of();
46+
}
47+
}
48+
}

backend/ongi/src/main/java/ongi/health/entity/PainRecord.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import lombok.*;
55
import java.time.LocalDate;
66
import java.util.UUID;
7+
import java.util.List;
78

89
@Entity
910
@Getter
@@ -22,19 +23,44 @@ public class PainRecord {
2223
@Column(nullable = false)
2324
private LocalDate date;
2425

25-
@Enumerated(EnumType.STRING)
26-
@Column(nullable = false)
27-
private PainArea painArea;
26+
@Column(columnDefinition = "json", nullable = false)
27+
@Convert(converter = PainAreaListToJsonConverter.class)
28+
private List<PainArea> painArea;
2829

29-
@Enumerated(EnumType.STRING)
30-
@Column(nullable = false)
31-
private PainLevel painLevel;
3230

3331
public enum PainArea {
34-
HEAD, NECK, SHOULDER, CHEST, BACK, ARM, HAND, ABDOMEN, WAIST, LEG, KNEE, FOOT, NONE
32+
// 머리
33+
HEAD,
34+
// 목
35+
NECK,
36+
// 어깨 (좌우 구분)
37+
LEFT_SHOULDER, RIGHT_SHOULDER,
38+
// 가슴
39+
CHEST,
40+
// 등
41+
BACK,
42+
// 팔 (좌우 구분, 윗팔/아랫팔 구분)
43+
LEFT_UPPER_ARM, RIGHT_UPPER_ARM,
44+
LEFT_FOREARM, RIGHT_FOREARM,
45+
// 손 (좌우 구분)
46+
LEFT_HAND, RIGHT_HAND,
47+
// 배
48+
ABDOMEN,
49+
// 허리
50+
WAIST,
51+
// 골반 (좌우 구분)
52+
PELVIS,
53+
// 엉덩이 (좌우 구분)
54+
HIP,
55+
// 다리 (좌우 구분, 허벅지/종아리 구분)
56+
LEFT_THIGH, RIGHT_THIGH,
57+
LEFT_CALF, RIGHT_CALF,
58+
// 무릎 (좌우 구분)
59+
LEFT_KNEE, RIGHT_KNEE,
60+
// 발 (좌우 구분)
61+
LEFT_FOOT, RIGHT_FOOT,
62+
// 없음
63+
NONE
3564
}
3665

37-
public enum PainLevel {
38-
STRONG, MID_STRONG, MID_WEAK, WEAK
39-
}
4066
}

backend/ongi/src/main/java/ongi/health/repository/ExerciseRecordRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
public interface ExerciseRecordRepository extends JpaRepository<ExerciseRecord, Long> {
1313
// 최근 7일간 운동 기록 조회
1414
List<ExerciseRecord> findByParentIdAndDateBetweenOrderByDateDesc(UUID parentId, LocalDate startDate, LocalDate endDate);
15-
// date로 단일 ExerciseRecord 조회
15+
// 특정 날짜의 통증 기록 조회
1616
java.util.Optional<ExerciseRecord> findByParentIdAndDate(UUID parentId, LocalDate date);
1717
}

backend/ongi/src/main/java/ongi/health/repository/PainRecordRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212
public interface PainRecordRepository extends JpaRepository<PainRecord, Long> {
1313
// 최근 7일간 통증 기록 조회
1414
List<PainRecord> findByParentIdAndDateBetweenOrderByDateDesc(UUID parentId, LocalDate startDate, LocalDate endDate);
15+
// 특정 날짜의 통증 기록 조회
16+
java.util.Optional<PainRecord> findByParentIdAndDate(UUID parentId, LocalDate date);
1517
}

backend/ongi/src/main/java/ongi/health/service/HealthRecordService.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ public class HealthRecordService {
1818
private final PainRecordRepository painRecordRepository;
1919
private final ExerciseRecordRepository exerciseRecordRepository;
2020

21-
// 통증 기록 추가
21+
// 통증 기록 추가/수정
2222
@Transactional
23-
public PainRecord addPainRecord(UUID parentId, LocalDate date, PainRecord.PainArea area, PainRecord.PainLevel level) {
24-
PainRecord record = PainRecord.builder()
25-
.parentId(parentId)
26-
.date(date)
27-
.painArea(area)
28-
.painLevel(level)
29-
.build();
23+
public PainRecord addOrUpdatePainRecord(UUID parentId, LocalDate date, List<PainRecord.PainArea> areas) {
24+
PainRecord record = painRecordRepository.findByParentIdAndDate(parentId, date)
25+
.orElse(PainRecord.builder().parentId(parentId).date(date).build());
26+
record.setPainArea(areas);
3027
return painRecordRepository.save(record);
3128
}
3229

@@ -60,4 +57,12 @@ public ExerciseRecord getParentExerciseRecordDetail(UUID parentId, LocalDate dat
6057
return exerciseRecordRepository.findByParentIdAndDate(parentId, date)
6158
.orElse(null);
6259
}
60+
61+
// 특정 날짜 통증 기록 조회
62+
public PainRecord getParentPainRecordDetail(UUID parentId, LocalDate date) {
63+
return painRecordRepository.findByParentIdAndDate(parentId, date)
64+
.orElse(null);
65+
}
66+
67+
6368
}

0 commit comments

Comments
 (0)