diff --git a/src/main/java/com/depromeet/domain/feed/dto/response/FeedOneByProfileResponse.java b/src/main/java/com/depromeet/domain/feed/dto/response/FeedOneByProfileResponse.java index c90842bc9..83ef9ae78 100644 --- a/src/main/java/com/depromeet/domain/feed/dto/response/FeedOneByProfileResponse.java +++ b/src/main/java/com/depromeet/domain/feed/dto/response/FeedOneByProfileResponse.java @@ -3,8 +3,8 @@ import com.depromeet.domain.missionRecord.domain.MissionRecord; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.v3.oas.annotations.media.Schema; -import java.time.Duration; import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; public record FeedOneByProfileResponse( @Schema(description = "미션 ID", defaultValue = "1") Long missionId, @@ -42,7 +42,9 @@ public static FeedOneByProfileResponse of(MissionRecord record) { record.getMission().getName(), record.getImageUrl(), record.getDuration().toMinutes(), - Duration.between(record.getMission().getStartedAt(), record.getStartedAt()).toDays() + ChronoUnit.DAYS.between( + record.getMission().getStartedAt().toLocalDate(), + record.getStartedAt().toLocalDate()) + 1, record.getStartedAt(), record.getFinishedAt()); diff --git a/src/main/java/com/depromeet/domain/feed/dto/response/FeedOneResponse.java b/src/main/java/com/depromeet/domain/feed/dto/response/FeedOneResponse.java index 58ea2ae6c..b1cca5f88 100644 --- a/src/main/java/com/depromeet/domain/feed/dto/response/FeedOneResponse.java +++ b/src/main/java/com/depromeet/domain/feed/dto/response/FeedOneResponse.java @@ -5,6 +5,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import java.time.Duration; import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; public record FeedOneResponse( @Schema(description = "작성자 ID", defaultValue = "1") Long memberId, @@ -72,7 +73,7 @@ public FeedOneResponse( remark, recordImageUrl, duration.toMinutes(), - Duration.between(startedAt, recordStartedAt).toDays() + 1, + ChronoUnit.DAYS.between(startedAt, recordStartedAt) + 1, startedAt, finishedAt, recordStartedAt); diff --git a/src/main/java/com/depromeet/domain/missionRecord/application/MissionRecordService.java b/src/main/java/com/depromeet/domain/missionRecord/application/MissionRecordService.java index 4c046e487..450241496 100644 --- a/src/main/java/com/depromeet/domain/missionRecord/application/MissionRecordService.java +++ b/src/main/java/com/depromeet/domain/missionRecord/application/MissionRecordService.java @@ -83,13 +83,7 @@ public MissionRecordFindOneResponse findOneMissionRecord(Long recordId) { missionRecordRepository .findById(recordId) .orElseThrow(() -> new CustomException(ErrorCode.MISSION_RECORD_NOT_FOUND)); - long sinceDay = - Duration.between( - missionRecord.getMission().getStartedAt(), - missionRecord.getStartedAt()) - .toDays() - + DAYS_ADJUSTMENT; - return MissionRecordFindOneResponse.of(missionRecord, sinceDay); + return MissionRecordFindOneResponse.from(missionRecord); } @Transactional(readOnly = true) diff --git a/src/main/java/com/depromeet/domain/missionRecord/dto/response/MissionRecordFindOneResponse.java b/src/main/java/com/depromeet/domain/missionRecord/dto/response/MissionRecordFindOneResponse.java index 4ea6ab6c3..f9b114169 100644 --- a/src/main/java/com/depromeet/domain/missionRecord/dto/response/MissionRecordFindOneResponse.java +++ b/src/main/java/com/depromeet/domain/missionRecord/dto/response/MissionRecordFindOneResponse.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.v3.oas.annotations.media.Schema; import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; public record MissionRecordFindOneResponse( @Schema(description = "미션 기록 ID", defaultValue = "1") Long recordId, @@ -33,13 +34,16 @@ public record MissionRecordFindOneResponse( defaultValue = "2024-01-03 00:34:00", type = "string") LocalDateTime finishedAt) { - public static MissionRecordFindOneResponse of(MissionRecord missionRecord, long sinceDay) { + public static MissionRecordFindOneResponse from(MissionRecord missionRecord) { return new MissionRecordFindOneResponse( missionRecord.getId(), missionRecord.getRemark(), missionRecord.getImageUrl(), missionRecord.getDuration().toMinutes(), - sinceDay, + ChronoUnit.DAYS.between( + missionRecord.getMission().getStartedAt().toLocalDate(), + missionRecord.getStartedAt().toLocalDate()) + + 1, missionRecord.getStartedAt(), missionRecord.getFinishedAt()); } diff --git a/src/test/java/com/depromeet/domain/missionRecord/application/MissionRecordServiceTest.java b/src/test/java/com/depromeet/domain/missionRecord/application/MissionRecordServiceTest.java index a59514e3c..f83e062a8 100644 --- a/src/test/java/com/depromeet/domain/missionRecord/application/MissionRecordServiceTest.java +++ b/src/test/java/com/depromeet/domain/missionRecord/application/MissionRecordServiceTest.java @@ -1,5 +1,6 @@ package com.depromeet.domain.missionRecord.application; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.when; import com.depromeet.DatabaseCleaner; @@ -14,14 +15,13 @@ import com.depromeet.domain.missionRecord.dao.MissionRecordRepository; import com.depromeet.domain.missionRecord.domain.MissionRecord; import com.depromeet.domain.missionRecord.dto.request.MissionRecordCreateRequest; +import com.depromeet.domain.missionRecord.dto.response.MissionRecordFindOneResponse; import com.depromeet.domain.missionRecord.dto.response.MissionStatisticsResponse; import com.depromeet.global.error.exception.CustomException; import com.depromeet.global.util.SecurityUtil; import java.time.Duration; import java.time.LocalDateTime; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -78,7 +78,7 @@ void setUp() { // then Long missionId = mission.getId(); - Assertions.assertThrows( + assertThrows( CustomException.class, () -> missionRecordService.findOneMissionRecord(missionId)); } @@ -121,12 +121,59 @@ void setUp() { missionRecordService.findMissionStatistics(mission.getId()); // then - Assertions.assertEquals(missionStatistics.totalMissionHour(), 1); - Assertions.assertEquals(missionStatistics.totalMissionMinute(), 59); - Assertions.assertEquals(missionStatistics.totalSymbolStack(), 7); - Assertions.assertEquals(missionStatistics.continuousSuccessDay(), 4); - Assertions.assertEquals(missionStatistics.totalSuccessDay(), 7); - Assertions.assertEquals(missionStatistics.totalMissionAttainRate(), 46.7); - Assertions.assertEquals(missionStatistics.timeTable().size(), 7); + assertEquals(missionStatistics.totalMissionHour(), 1); + assertEquals(missionStatistics.totalMissionMinute(), 59); + assertEquals(missionStatistics.totalSymbolStack(), 7); + assertEquals(missionStatistics.continuousSuccessDay(), 4); + assertEquals(missionStatistics.totalSuccessDay(), 7); + assertEquals(missionStatistics.totalMissionAttainRate(), 46.7); + assertEquals(missionStatistics.timeTable().size(), 7); + } + + @Nested + class 미션기록_단건_조회할_때 { + @Test + void 미션이_생성된_당일에_미션기록을_완료한_경우라면_sinceDay가_1이된다() { + // given + LocalDateTime missionRecordStartedAt = missionStartedAt; + LocalDateTime missionRecordFinishedAt = + missionRecordStartedAt.plusMinutes(32).plusSeconds(14); + Duration duration = Duration.ofMinutes(32).plusSeconds(14); + MissionRecord missionRecord = + MissionRecord.createMissionRecord( + duration, missionRecordStartedAt, missionRecordFinishedAt, mission); + missionRecord.updateUploadStatusPending(); + missionRecord.updateUploadStatusComplete("remark", "imageUrl"); + MissionRecord savedMissionRecord = missionRecordRepository.save(missionRecord); + + // when + MissionRecordFindOneResponse queryMissionRecord = + missionRecordService.findOneMissionRecord(savedMissionRecord.getId()); + + // then + assertEquals(1, queryMissionRecord.sinceDay()); + } + + @Test + void 미션이_생성되고_일주일후에_미션기록을_완료한_경우라면_sinceDay가_8이된다() { + // given + LocalDateTime missionRecordStartedAt = missionStartedAt.plusDays(7); + LocalDateTime missionRecordFinishedAt = + missionRecordStartedAt.plusMinutes(32).plusSeconds(14); + Duration duration = Duration.ofMinutes(32).plusSeconds(14); + MissionRecord missionRecord = + MissionRecord.createMissionRecord( + duration, missionRecordStartedAt, missionRecordFinishedAt, mission); + missionRecord.updateUploadStatusPending(); + missionRecord.updateUploadStatusComplete("remark", "imageUrl"); + MissionRecord savedMissionRecord = missionRecordRepository.save(missionRecord); + + // when + MissionRecordFindOneResponse queryMissionRecord = + missionRecordService.findOneMissionRecord(savedMissionRecord.getId()); + + // then + assertEquals(8, queryMissionRecord.sinceDay()); + } } }