Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WALWAL-145] missionrecord calendarview #52

Merged
merged 14 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
593d703
feature: ๋ฏธ์…˜ ๊ธฐ๋ก ์กฐํšŒ ์บ˜๋Ÿฐ๋”๋ทฐ API ๊ตฌํ˜„
dbscks97 Jul 19, 2024
dd9db91
MissionRecordServiceTest ์ž‘์„ฑ
dbscks97 Jul 22, 2024
2b5f8e2
fix: MissionRecordCalendar ์กฐํšŒ API ์ˆ˜์ •
dbscks97 Jul 23, 2024
63e7e7f
Merge branch 'develop' into feature/#49-missionrecord-calendarview
dbscks97 Jul 24, 2024
b3c7b68
fix: ํ…Œ์ŠคํŠธ์ฝ”๋“œ ์ˆ˜์ • ๋ฐ ์ถ”๊ฐ€, ํŠธ๋žœ์žญ์…˜์ฒ˜๋ฆฌ ์ˆ˜์ •
dbscks97 Jul 24, 2024
fdac3c2
fix: ์บ˜๋ฆฐ๋”์กฐํšŒ API ์ˆ˜์ •
dbscks97 Jul 24, 2024
0b151bc
refactor: QueryDSL๋ฐ˜์˜ ๋ฐ ์บ˜๋ฆฐ๋”์กฐํšŒ API ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ
dbscks97 Jul 25, 2024
abc1c44
fix: ์•ˆ์“ฐ๋Š” ๋ฉ”์„œ๋“œ ์ •๋ฆฌ
dbscks97 Jul 25, 2024
4d3a2ce
fix: DATE_FORMATTER ์ˆ˜์ • ๋ฐ Member final์„ ์–ธ
dbscks97 Jul 25, 2024
a8a0ad8
fix: MissionRepositoryTest์— QuerydslConfig import
dbscks97 Jul 25, 2024
7c516aa
fix: Response ํ˜•์‹ ์ˆ˜์ •, QueryDSL ๋ฉ”์„œ๋“œ ๋ถ„๋ฆฌ ๋ฐ ์ˆ˜์ •
dbscks97 Jul 26, 2024
4957ec4
fix: Test์ฝ”๋“œ ์ˆ˜์ •
dbscks97 Jul 26, 2024
94b521c
Merge branch 'develop' into feature/#49-missionrecord-calendarview
dbscks97 Jul 26, 2024
25815e7
fix: CreatedAtAfter -> CreatedAtFrom ์ˆ˜์ •
dbscks97 Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.depromeet.stonebed.domain.missionRecord.api;

import com.depromeet.stonebed.domain.missionRecord.application.MissionRecordService;
import com.depromeet.stonebed.domain.missionRecord.dto.request.MissionRecordCalendarRequest;
import com.depromeet.stonebed.domain.missionRecord.dto.request.MissionRecordCreateRequest;
import com.depromeet.stonebed.domain.missionRecord.dto.response.MissionRecordCalendarResponse;
import com.depromeet.stonebed.domain.missionRecord.dto.response.MissionRecordCreateResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -34,4 +36,11 @@ public MissionRecordCreateResponse completeMission(
public void deleteMissionRecord(@PathVariable Long recordId) {
missionRecordService.deleteMissionRecord(recordId);
}

@Operation(summary = "์บ˜๋ฆฐ๋” ํ˜•์‹์˜ ๋ฏธ์…˜ ๊ธฐ๋ก ์กฐํšŒ", description = "ํšŒ์›์˜ ๋ฏธ์…˜ ๊ธฐ๋ก์„ ํŽ˜์ด์ง•ํ•˜์—ฌ ์กฐํšŒํ•œ๋‹ค.")
@PostMapping("/calendar")
public MissionRecordCalendarResponse getMissionRecordsForCalendar(
@Valid @RequestBody MissionRecordCalendarRequest request) {
return missionRecordService.getMissionRecordsForCalendar(request.cursor(), request.limit());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
import com.depromeet.stonebed.domain.missionRecord.domain.MissionRecord;
import com.depromeet.stonebed.domain.missionRecord.domain.MissionStatus;
import com.depromeet.stonebed.domain.missionRecord.dto.request.MissionRecordCreateRequest;
import com.depromeet.stonebed.domain.missionRecord.dto.response.MissionRecordCalendarDto;
import com.depromeet.stonebed.domain.missionRecord.dto.response.MissionRecordCalendarResponse;
import com.depromeet.stonebed.domain.missionRecord.dto.response.MissionRecordCreateResponse;
import com.depromeet.stonebed.global.error.ErrorCode;
import com.depromeet.stonebed.global.error.exception.CustomException;
import com.depromeet.stonebed.global.util.MemberUtil;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -24,10 +34,13 @@ public class MissionRecordService {
private final MissionRecordRepository missionRecordRepository;
private final MemberUtil memberUtil;

private static final DateTimeFormatter DATE_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd");

Comment on lines +37 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ •์  ๋ณ€์ˆ˜ ์„ ์–ธ ์ข‹์•„์šฉ ๐Ÿ‘

public MissionRecordCreateResponse completeMission(MissionRecordCreateRequest request) {
Mission mission = findMissionById(request.missionId());

Member member = memberUtil.getCurrentMember();
final Member member = memberUtil.getCurrentMember();

MissionRecord missionRecord =
MissionRecord.builder()
Expand All @@ -50,10 +63,50 @@ public void deleteMissionRecord(Long recordId) {
missionRecordRepository.delete(missionRecord);
}

// ๋‹จ์ผ ๋ฏธ์…˜ ์กฐํšŒ ๋ฉ”์„œ๋“œ
private Mission findMissionById(Long missionId) {
return missionRepository
.findById(missionId)
.orElseThrow(() -> new CustomException(ErrorCode.MISSION_NOT_FOUND));
}

@Transactional(readOnly = true)
public MissionRecordCalendarResponse getMissionRecordsForCalendar(String cursor, int limit) {
final Member member = memberUtil.getCurrentMember();
Pageable pageable = PageRequest.of(0, limit, Sort.by(Sort.Direction.ASC, "createdAt"));

List<MissionRecord> records = getMissionRecords(cursor, member, pageable);

List<MissionRecordCalendarDto> calendarData =
records.stream()
.map(record -> MissionRecordCalendarDto.from(record, DATE_FORMATTER))
.toList();

String nextCursor = getNextCursor(records);

return MissionRecordCalendarResponse.from(calendarData, nextCursor);
}

private List<MissionRecord> getMissionRecords(String cursor, Member member, Pageable pageable) {
if (cursor == null) {
return missionRecordRepository.findByMemberIdWithPagination(member.getId(), pageable);
}

try {
LocalDateTime cursorDate = LocalDate.parse(cursor, DATE_FORMATTER).atStartOfDay();
return missionRecordRepository.findByMemberIdAndCreatedAtFromWithPagination(
member.getId(), cursorDate, pageable);
} catch (DateTimeParseException e) {
throw new CustomException(ErrorCode.INVALID_CURSOR_DATE_FORMAT);
}
}

private String getNextCursor(List<MissionRecord> records) {
if (records.isEmpty()) {
return null;
}

MissionRecord lastRecord = records.get(records.size() - 1);
LocalDate nextCursorDate = lastRecord.getCreatedAt().toLocalDate().plusDays(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(์˜๊ฒฌ) ํ•˜๋‚˜ ์šฐ๋ ค๊ฐ€ ๋˜๋Š” ์ ์€ ์—ฌ๊ธฐ์„œ plus 1์„ ํ•˜๊ฒŒ ๋˜๋ฉด

์˜ˆ๋ฅผ ๋“ค์–ด,

  1. 20240101
  2. 20240102
  3. 20240103
  4. 20240104
  5. 20240105

์ด๋ ‡๊ฒŒ 5๊ฐœ์˜ ๋‚ ์งœ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด

์š”์ฒญ์„ cursor = null, limit = 2 ๋กœ ํ•˜๋ฉด
missions = [
20240101,
20240102,
],
nextCursor = 20240103

์ด๋ ‡๊ฒŒ ๋ ํ…๋ฐ

๊ทธ ๋‹ค์Œ ์š”์ฒญ ๋•Œ 20240103์œผ๋กœ ๋ณด๋‚ด๋ฉด
CreatedAtAfter ๊ฐ€ 20240103 ์ดํ›„๋‹ˆ๊นŒ 20240104, 20240105 ์ด๋Ÿฐ ์‹์œผ๋กœ ์˜ฌ ๊ฒƒ ๊ฐ™์•„์„œ 20240103 ์€ ๋ˆ„๋ฝ๋  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ ...

๋ญ”๊ฐ€ ์ œ๊ฐ€ ์ €๋ฒˆ์— ์„ค๋ช…ํ–ˆ์„ ๋•Œ +1์ผ๋กœ ํ•˜์ž ์ด๋žฌ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ
์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ nextCursorDate๋Š” plus๊ฐ€ ์—†๋Š” ๊ฒŒ ์ข‹์ง€ ์•Š์„๊นŒ ์ƒ๊ฐ์ด ๋“œ๋„ค์šฉ

๊ทผ๋ฐ ์—ญ์‹œ ์ œ๊ฐ€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ•ด์„œ ํ‹€๋ฆด ์ˆ˜๋„ ์žˆ์–ด์„œ ๊ทธ๋ƒฅ ์˜๊ฒฌ ์ •๋„๋กœ๋งŒ ์ƒ๊ฐํ•ด์ฃผ์„ธ์š” ใ…Žใ…Ž

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค ์ข‹์€ ์˜๊ฒฌ ๊ณ ๋งˆ์›Œ CreatedAtAfter๋Œ€์‹  CreatedAtFrom์œผ๋กœ ํฌํ•จํ•ด์„œ ๋ณด๋‚ด์ฃผ๋Š” ๊ฑธ๋กœ ๋ฐ”๊ฟ€๊ฒŒ!!

return nextCursorDate.format(DATE_FORMATTER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
import com.depromeet.stonebed.domain.missionRecord.domain.MissionRecord;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MissionRecordRepository extends JpaRepository<MissionRecord, Long> {}
public interface MissionRecordRepository
extends JpaRepository<MissionRecord, Long>, MissionRecordRepositoryCustom {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.depromeet.stonebed.domain.missionRecord.dao;

import com.depromeet.stonebed.domain.missionRecord.domain.MissionRecord;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.domain.Pageable;

public interface MissionRecordRepositoryCustom {
List<MissionRecord> findByMemberIdWithPagination(Long memberId, Pageable pageable);

List<MissionRecord> findByMemberIdAndCreatedAtFromWithPagination(
Long memberId, LocalDateTime createdAt, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.depromeet.stonebed.domain.missionRecord.dao;

import static com.depromeet.stonebed.domain.missionRecord.domain.QMissionRecord.missionRecord;

import com.depromeet.stonebed.domain.missionRecord.domain.MissionRecord;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class MissionRecordRepositoryImpl implements MissionRecordRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<MissionRecord> findByMemberIdWithPagination(Long memberId, Pageable pageable) {
return queryFactory
.selectFrom(missionRecord)
.where(isMemberId(memberId))
.orderBy(missionRecord.createdAt.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
}

@Override
public List<MissionRecord> findByMemberIdAndCreatedAtFromWithPagination(
Long memberId, LocalDateTime createdAt, Pageable pageable) {
return queryFactory
.selectFrom(missionRecord)
.where(missionRecord.member.id.eq(memberId).and(createdAtFrom(createdAt)))
.orderBy(missionRecord.createdAt.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
}

private BooleanExpression isMemberId(Long memberId) {
return missionRecord.member.id.eq(memberId);
}

private BooleanExpression createdAtFrom(LocalDateTime createdAt) {
return missionRecord.createdAt.goe(createdAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class MissionRecord extends BaseTimeEntity {
@Column(name = "status", nullable = false)
private MissionStatus status;

@Column(name = "booster_value")
private int boosterValue;

@Builder
public MissionRecord(Member member, Mission mission, String imageUrl, MissionStatus status) {
this.member = member;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.depromeet.stonebed.domain.missionRecord.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;

public record MissionRecordCalendarRequest(
@Schema(description = "์ปค์„œ ์œ„์น˜", example = "2024-01-01") String cursor,
@NotNull @Min(1) @Schema(description = "ํŽ˜์ด์ง€ ๋‹น ํ•ญ๋ชฉ ์ˆ˜", example = "30") int limit) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.depromeet.stonebed.domain.missionRecord.dto.response;

import com.depromeet.stonebed.domain.missionRecord.domain.MissionRecord;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.format.DateTimeFormatter;

public record MissionRecordCalendarDto(
@Schema(description = "์ด๋ฏธ์ง€ ID") Long imageId,
@Schema(description = "์ด๋ฏธ์ง€ URL") String imageUrl,
@Schema(description = "๋ฏธ์…˜ ์ˆ˜ํ–‰ ์ผ์ž") String missionDate) {

public static MissionRecordCalendarDto from(
MissionRecord missionRecord, DateTimeFormatter formatter) {
String formattedDate = missionRecord.getCreatedAt().format(formatter);
return new MissionRecordCalendarDto(
missionRecord.getId(), missionRecord.getImageUrl(), formattedDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.depromeet.stonebed.domain.missionRecord.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;

public record MissionRecordCalendarResponse(
@Schema(
description = "๋ฏธ์…˜ ๊ธฐ๋ก ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ",
example =
"[{ 'imageId': 1, 'imageUrl': 'http://example.com/image1.jpg', 'missionDate': '2024-01-01' }]")
List<MissionRecordCalendarDto> list,
@Schema(description = "์ปค์„œ ์œ„์น˜", example = "2024-01-03") String nextCursor) {

public static MissionRecordCalendarResponse from(
List<MissionRecordCalendarDto> list, String nextCursor) {
return new MissionRecordCalendarResponse(list, nextCursor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum ErrorCode {
// mission
MISSION_NOT_FOUND(HttpStatus.NOT_FOUND, "ํ•ด๋‹น ๋ฏธ์…˜์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
MISSION_RECORD_NOT_FOUND(HttpStatus.NOT_FOUND, "ํ•ด๋‹น ๋ฏธ์…˜ ๊ธฐ๋ก์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
INVALID_CURSOR_DATE_FORMAT(HttpStatus.BAD_REQUEST, "์ž˜๋ชป๋œ ์ปค์„œ ๋‚ ์งœ ํ˜•์‹์ž…๋‹ˆ๋‹ค."),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.depromeet.stonebed.domain.mission.dao;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;

import com.depromeet.stonebed.TestQuerydslConfig;
import com.depromeet.stonebed.domain.mission.domain.Mission;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

@DataJpaTest
@Import(TestQuerydslConfig.class)
public class MissionRepositoryTest {

@Autowired private MissionRepository missionRepository;
Expand Down
Loading
Loading