Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -13,8 +13,8 @@
@Repository
public interface MemoRepository extends JpaRepository<Memo, Long> {

// RecordDetail의 모든 Memo들을 찾아서 업데이트 순으로 정렬 후 최상단 반환
Optional<Memo> findTopByRecordDetailOrderByUpdatedAtDesc(RecordDetail recordDetail);
// RecordDetail의 모든 Memo들을 찾아서 생성 순으로 정렬 후 최상단 반환
Optional<Memo> findTopByRecordDetailOrderByCreatedAtDesc(RecordDetail recordDetail);

// RecordDetail의 모든 Memo들을 찾아서 생성 순으로 정렬 후 반환
List<Memo> findAllByRecordDetailOrderByCreatedAtDesc(RecordDetail recordDetail);
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/cmc/mercury/domain/memo/service/MemoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -112,8 +113,9 @@ public MemoResponse updateMemo(User user, Long recordId, Long memoId, MemoUpdate
// db에 updatedAt이 바로 반영되어 응답하도록
memoRepository.flush();

Record record = memo.getRecordDetail().getRecord();
record.updateLastModifiedDateWithDetail(memo.getUpdatedAt());
// 수정은 날짜 바뀌지 않음
// Record record = memo.getRecordDetail().getRecord();
// record.updateLastModifiedDateWithDetail(memo.getUpdatedAt());

return MemoResponse.from(memo, recordId);
}
Expand All @@ -122,13 +124,20 @@ public MemoResponse updateMemo(User user, Long recordId, Long memoId, MemoUpdate
public void deleteMemo(User user, Long recordId, Long memoId) {

Memo memo = validateAndGetMemo(user.getId(), recordId, memoId);
Record record = memo.getRecordDetail().getRecord();
RecordDetail recordDetail = memo.getRecordDetail();

// 사용자 경험치 차감
// user.updateExp(user.getExp() - memo.getAcquiredExp());

memo.getRecordDetail().getMemos().remove(memo);
// 메모 삭제
// memo.getRecordDetail().getMemos().remove(memo);
memoRepository.delete(memo); // Memo 삭제하면 JPA가 자동으로 RecordDetail에서도 삭제

memoRepository.delete(memo);
// 남은 메모 중 최신 메모 찾기
Optional<Memo> latestMemo = memoRepository.findTopByRecordDetailOrderByCreatedAtDesc(recordDetail);
// RecordDetail의 updatedGauge 갱신 (없으면 0)
recordDetail.updateGauge(latestMemo.map(Memo::getGauge).orElse(0));
}

private Memo validateAndGetMemo(Long userId, Long recordId, Long memoId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.cmc.mercury.domain.book.dto.BookResponse;
import com.cmc.mercury.domain.memo.dto.MemoResponse;
import com.cmc.mercury.domain.memo.entity.Memo;
import com.cmc.mercury.domain.record.entity.Record;
import com.cmc.mercury.domain.record.entity.RecordDetail;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -25,13 +26,13 @@ public record RecordDetailResponse(
@Schema(description = "메모 객체 목록")
List<MemoResponse> memos
) {
public static RecordDetailResponse of(Record record, RecordDetail detail, List<MemoResponse> memos) {
public static RecordDetailResponse of(Record record, RecordDetail recordDetail, Memo latestMemo, List<MemoResponse> memos) {
return new RecordDetailResponse(
record.getId(),
detail.getUpdatedGauge(),
recordDetail.getUpdatedGauge(),
BookResponse.from(record.getBook()),
record.getCreatedAt(),
record.getUpdatedAt(),
latestMemo != null ? latestMemo.getCreatedAt() : record.getUpdatedAt(),
memos
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.cmc.mercury.domain.record.dto;

import com.cmc.mercury.domain.book.dto.BookResponse;
import com.cmc.mercury.domain.memo.entity.Memo;
import com.cmc.mercury.domain.record.entity.Record;
import com.cmc.mercury.domain.record.entity.RecordDetail;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;
import java.util.Optional;

@Schema(title = "기록 객체 응답 형식")
public record RecordResponse (
Expand All @@ -25,14 +27,14 @@ public record RecordResponse (
@Schema(description = "얻은 경험치")
int acquiredExp
) {
public static RecordResponse of(Record record, RecordDetail detail, String content) {
public static RecordResponse of(Record record, RecordDetail recordDetail, Memo memo, String content) {

return new RecordResponse(
record.getId(),
detail.getUpdatedGauge(),
recordDetail.getUpdatedGauge(),
content,
record.getCreatedAt(),
record.getUpdatedAt(),
memo != null ? memo.getCreatedAt() : record.getUpdatedAt(),
BookResponse.from(record.getBook()),
record.getAcquiredExp()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -99,7 +100,7 @@ public RecordResponse createRecord(User user, RecordRequest request) {
habitHistoryRepository.save(history);

//* return RecordResponse.of(record, recordDetail, savedMemo.getContent());
return RecordResponse.of(savedRecord, savedRecord.getRecordDetail(), memo.getContent());
return RecordResponse.of(savedRecord, savedRecord.getRecordDetail(), memo, memo.getContent());
}

private int calculateRecordExp(Long userId, LocalDateTime deviceTime) {
Expand Down Expand Up @@ -160,6 +161,7 @@ public void deleteRecord(User user, Long recordId) {

user.updateExp(user.getExp() - reduceExp);*/

// CascadeType.ALL로 인해 recordDetail도 같이 삭제
recordRepository.delete(record);
}

Expand All @@ -181,22 +183,22 @@ public RecordDetailResponse getRecordDetail(User user, Long recordId) {
.map(memo -> MemoResponse.from(memo, record.getId()))
.toList();

return RecordDetailResponse.of(record, recordDetail, memoResponses);
Memo latestMemo = memoRepository.findTopByRecordDetailOrderByCreatedAtDesc(recordDetail).orElse(null);

return RecordDetailResponse.of(record, recordDetail, latestMemo, memoResponses);
}

public List<RecordResponse> toRecordResponses(List<Record> records) {

// RecordResponse 리스트로 변환
return records.stream()
.map(record -> {
RecordDetail detail = record.getRecordDetail();
RecordDetail recordDetail = record.getRecordDetail();
Memo latestMemo = memoRepository.findTopByRecordDetailOrderByCreatedAtDesc(recordDetail).orElse(null);

String latestMemoContent = memoRepository
.findTopByRecordDetailOrderByUpdatedAtDesc(detail)
.map(Memo::getContent)
.orElse("");
String latestMemoContent = (latestMemo != null) ? latestMemo.getContent() : "";

return RecordResponse.of(record, detail, latestMemoContent);
return RecordResponse.of(record, recordDetail, latestMemo, latestMemoContent);
})
.toList();
}
Expand Down