Skip to content

Commit 45cb87c

Browse files
authored
Merge pull request #92 from SWYP-9th-Team5/89/Feat
Feat: 특정 애완 식물 특정 오늘의 기록 조회 API 구현
2 parents 3da24d3 + 55aee7c commit 45cb87c

14 files changed

Lines changed: 215 additions & 7 deletions

File tree

src/main/java/swyp/team5/greening/petPlant/controller/DailyRecordController.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.RequiredArgsConstructor;
66
import org.springframework.http.HttpStatus;
77
import org.springframework.validation.annotation.Validated;
8+
import org.springframework.web.bind.annotation.GetMapping;
89
import org.springframework.web.bind.annotation.PathVariable;
910
import org.springframework.web.bind.annotation.PostMapping;
1011
import org.springframework.web.bind.annotation.RequestBody;
@@ -15,7 +16,9 @@
1516
import swyp.team5.greening.common.resolver.LogIn;
1617
import swyp.team5.greening.petPlant.dto.request.CreateDailyRecordRequestDto;
1718
import swyp.team5.greening.petPlant.dto.response.CreateDailyRecordResponseDto;
19+
import swyp.team5.greening.petPlant.dto.response.FindDailyRecordResponseDto;
1820
import swyp.team5.greening.petPlant.service.DailyRecordCommandService;
21+
import swyp.team5.greening.petPlant.service.DailyRecordQueryService;
1922

2023
@Tag(name = "애완 식물 오늘의 기록 API")
2124
@RestController
@@ -24,6 +27,7 @@
2427
public class DailyRecordController {
2528

2629
private final DailyRecordCommandService dailyRecordCommandService;
30+
private final DailyRecordQueryService dailyRecordQueryService;
2731

2832
@Operation(summary = "특정 애완 식물 오늘의 기록 작성 API")
2933
@PostMapping("/{petPlantId}/daily-record")
@@ -34,7 +38,24 @@ public ApiResponseDto<CreateDailyRecordResponseDto> createDailyRecord(
3438
@Validated @RequestBody CreateDailyRecordRequestDto requestDto
3539
) {
3640
return ApiResponseDto.of(
37-
dailyRecordCommandService.createDailyRecord(userId, petPlantId, requestDto));
41+
dailyRecordCommandService.createDailyRecord(
42+
userId,
43+
petPlantId,
44+
requestDto
45+
));
46+
}
47+
48+
@Operation(summary = "특정 애완 식물 특정 오늘의 기록 조회 API")
49+
@GetMapping("/daily-record/{dailyRecordId}")
50+
@ResponseStatus(HttpStatus.OK)
51+
public ApiResponseDto<FindDailyRecordResponseDto> getDailyRecord(
52+
@LogIn Long userId,
53+
@PathVariable Long dailyRecordId
54+
) {
55+
return ApiResponseDto.of(dailyRecordQueryService.findDailyRecord(
56+
userId,
57+
dailyRecordId
58+
));
3859
}
3960

4061
}

src/main/java/swyp/team5/greening/petPlant/domain/entity/DailyRecord.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import jakarta.persistence.CascadeType;
44
import jakarta.persistence.Column;
55
import jakarta.persistence.Entity;
6+
import jakarta.persistence.EnumType;
7+
import jakarta.persistence.Enumerated;
68
import jakarta.persistence.GeneratedValue;
79
import jakarta.persistence.GenerationType;
810
import jakarta.persistence.Id;
@@ -33,6 +35,10 @@ public class DailyRecord extends BaseTimeEntity {
3335
@Column(name = "write_date")
3436
private LocalDate writeDate;
3537

38+
@Enumerated(EnumType.STRING)
39+
@Column(name = "state")
40+
private DailyRecordState state;
41+
3642
@Column(name = "pet_plant_id")
3743
private Long petPlantId;
3844

@@ -43,10 +49,12 @@ public class DailyRecord extends BaseTimeEntity {
4349
public DailyRecord(
4450
String title,
4551
LocalDate writeDate,
52+
DailyRecordState state,
4653
Long petPlantId
4754
) {
4855
this.title = title;
4956
this.writeDate = writeDate;
57+
this.state = state;
5058
this.petPlantId = petPlantId;
5159
}
5260

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package swyp.team5.greening.petPlant.domain.entity;
2+
3+
public enum DailyRecordState {
4+
5+
IN_PROGRESS,
6+
DELETED
7+
8+
}

src/main/java/swyp/team5/greening/petPlant/domain/repository/DailyRecordQueryRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.time.LocalDate;
44
import java.util.List;
5+
import java.util.Optional;
56
import swyp.team5.greening.petPlant.domain.entity.DailyRecord;
7+
import swyp.team5.greening.petPlant.dto.data.FindDailyRecordDto;
68

79
public interface DailyRecordQueryRepository {
810

@@ -11,4 +13,6 @@ List<DailyRecord> findByPetPlantAndWriteDate(
1113
LocalDate startDate,
1214
LocalDate endDate
1315
);
16+
17+
Optional<FindDailyRecordDto> findDailyRecord(Long dailyRecordId);
1418
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package swyp.team5.greening.petPlant.dto.data;
2+
3+
import swyp.team5.greening.petPlant.domain.entity.DailyRecord;
4+
import swyp.team5.greening.petPlant.domain.entity.PetPlant;
5+
6+
public record FindDailyRecordDto(
7+
DailyRecord dailyRecord,
8+
PetPlant petPlant
9+
) {
10+
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package swyp.team5.greening.petPlant.dto.response;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import java.time.LocalDateTime;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
import swyp.team5.greening.petPlant.domain.entity.DailyRecord;
8+
import swyp.team5.greening.petPlant.domain.entity.DailyRecordContent;
9+
import swyp.team5.greening.petPlant.dto.DailyRecordContentDto;
10+
11+
public record FindDailyRecordResponseDto(
12+
13+
String title,
14+
15+
@JsonFormat(pattern = "yyyy-MM-dd'T'hh:mm:ss")
16+
LocalDateTime createdAt,
17+
18+
List<DailyRecordContentDto> content
19+
20+
) {
21+
22+
public static FindDailyRecordResponseDto of(DailyRecord dailyRecord) {
23+
24+
List<DailyRecordContent> dailyRecordContents = dailyRecord.getDailyRecordContents();
25+
26+
return new FindDailyRecordResponseDto(
27+
dailyRecord.getTitle(),
28+
dailyRecord.getCreatedAt(),
29+
dailyRecordContents.stream()
30+
.sorted(Comparator.comparing(DailyRecordContent::getSequence))
31+
.map(content ->
32+
new DailyRecordContentDto(content.getType().name(),
33+
content.getContent()))
34+
.toList()
35+
);
36+
37+
}
38+
39+
}

src/main/java/swyp/team5/greening/petPlant/exception/PetPlantExceptionMessage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ public enum PetPlantExceptionMessage implements ExceptionMessage {
1111
NOT_FOUND_PET_PLANT("존재하지 않는 애완 식물입니다.", "404"),
1212
BAD_REQUEST_PET_PLANT_WRITER("내가 등록한 애완 식물이 아닙니다.", "400"),
1313

14-
INVALID_DATE_ACCESS("해당 날짜에만 접근 가능합니다.", "400");
14+
NOT_FOUND_DAILY_RECORD("해당 날짜의 기록이 존재하지 않습니다.", "404"),
1515

16+
INVALID_DATE_ACCESS("해당 날짜에만 접근 가능합니다.", "400");
1617

1718
private final String message;
1819
private final String code;

src/main/java/swyp/team5/greening/petPlant/infrastructure/DailyRecordJpaQueryRepository.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import java.time.LocalDate;
44
import java.util.List;
5+
import java.util.Optional;
56
import org.springframework.data.jpa.repository.JpaRepository;
67
import org.springframework.data.jpa.repository.Query;
78
import org.springframework.data.repository.query.Param;
89
import swyp.team5.greening.petPlant.domain.entity.DailyRecord;
910
import swyp.team5.greening.petPlant.domain.repository.DailyRecordQueryRepository;
11+
import swyp.team5.greening.petPlant.dto.data.FindDailyRecordDto;
1012

1113
public interface DailyRecordJpaQueryRepository extends JpaRepository<DailyRecord, Long>,
1214
DailyRecordQueryRepository {
@@ -18,6 +20,7 @@ public interface DailyRecordJpaQueryRepository extends JpaRepository<DailyRecord
1820
WHERE dailyRecord.petPlantId = :petPlantId
1921
AND dailyRecord.writeDate >= :startDate
2022
AND dailyRecord.writeDate < :endDate
23+
AND dailyRecord.state = 'IN_PROGRESS'
2124
ORDER BY dailyRecord.writeDate asc
2225
""")
2326
List<DailyRecord> findByPetPlantAndWriteDate(
@@ -26,4 +29,16 @@ List<DailyRecord> findByPetPlantAndWriteDate(
2629
@Param("endDate") LocalDate endDate
2730
);
2831

32+
@Override
33+
@Query("""
34+
SELECT new swyp.team5.greening.petPlant.dto.data.FindDailyRecordDto(
35+
dailyRecord, petPlant)
36+
FROM DailyRecord dailyRecord
37+
INNER JOIN PetPlant petPlant
38+
ON petPlant.id = dailyRecord.petPlantId
39+
WHERE dailyRecord.id = :dailyRecordId
40+
AND dailyRecord.state = 'IN_PROGRESS'
41+
""")
42+
Optional<FindDailyRecordDto> findDailyRecord(@Param("dailyRecordId") Long dailyRecordId);
43+
2944
}

src/main/java/swyp/team5/greening/petPlant/service/DailyRecordCommandService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import swyp.team5.greening.petPlant.domain.entity.DailyRecord;
1212
import swyp.team5.greening.petPlant.domain.entity.DailyRecordContent;
1313
import swyp.team5.greening.petPlant.domain.entity.DailyRecordContentType;
14+
import swyp.team5.greening.petPlant.domain.entity.DailyRecordState;
1415
import swyp.team5.greening.petPlant.domain.entity.PetPlant;
1516
import swyp.team5.greening.petPlant.domain.entity.PetPlantState;
1617
import swyp.team5.greening.petPlant.domain.repository.DailyRecordRepository;
@@ -59,6 +60,7 @@ public CreateDailyRecordResponseDto createDailyRecord(
5960
DailyRecord dailyRecord = DailyRecord.builder()
6061
.title(requestDto.title())
6162
.writeDate(requestDto.today())
63+
.state(DailyRecordState.IN_PROGRESS)
6264
.petPlantId(petPlantId)
6365
.build();
6466

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package swyp.team5.greening.petPlant.service;
2+
3+
import java.util.Objects;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.transaction.annotation.Transactional;
7+
import swyp.team5.greening.common.exception.GreeningGlobalException;
8+
import swyp.team5.greening.petPlant.domain.entity.DailyRecord;
9+
import swyp.team5.greening.petPlant.domain.entity.PetPlant;
10+
import swyp.team5.greening.petPlant.domain.repository.DailyRecordQueryRepository;
11+
import swyp.team5.greening.petPlant.dto.data.FindDailyRecordDto;
12+
import swyp.team5.greening.petPlant.dto.response.FindDailyRecordResponseDto;
13+
import swyp.team5.greening.petPlant.exception.PetPlantExceptionMessage;
14+
15+
@Service
16+
@RequiredArgsConstructor
17+
public class DailyRecordQueryService {
18+
19+
private final DailyRecordQueryRepository dailyRecordQueryRepository;
20+
21+
@Transactional
22+
public FindDailyRecordResponseDto findDailyRecord(
23+
Long userId,
24+
Long dailyRecordId
25+
) {
26+
FindDailyRecordDto dailyRecordDto = dailyRecordQueryRepository.findDailyRecord(
27+
dailyRecordId)
28+
.orElseThrow(()
29+
-> new GreeningGlobalException(PetPlantExceptionMessage.NOT_FOUND_DAILY_RECORD));
30+
31+
DailyRecord dailyRecord = dailyRecordDto.dailyRecord();
32+
PetPlant petPlant = dailyRecordDto.petPlant();
33+
34+
if (!Objects.equals(petPlant.getUserId(), userId)) {
35+
throw new GreeningGlobalException(PetPlantExceptionMessage.BAD_REQUEST_PET_PLANT_WRITER);
36+
}
37+
38+
return FindDailyRecordResponseDto.of(dailyRecord);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)