Skip to content

Commit 94bdb87

Browse files
authored
Merge pull request #91 from dnd-side-project/feature/55-order-saved-notes
feat: ์ €์žฅ๋œ ์ชฝ์ง€์— ์ •๋ ฌ๊ธฐ์ค€ ์ถ”๊ฐ€
2 parents 645053b + f3d681f commit 94bdb87

File tree

7 files changed

+69
-16
lines changed

7 files changed

+69
-16
lines changed

โ€Žsrc/main/java/com/example/wini/domain/note/controller/NoteController.javaโ€Ž

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

33
import com.example.wini.domain.note.dto.request.NoteCreateRequest;
44
import com.example.wini.domain.note.dto.response.NoteResponse;
5+
import com.example.wini.domain.note.dto.response.SimpleNoteResponse;
56
import com.example.wini.domain.note.service.NoteService;
67
import io.swagger.v3.oas.annotations.Operation;
78
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -26,15 +27,17 @@ public NoteResponse getNote(@PathVariable Long noteId) {
2627
}
2728

2829
@GetMapping("/latest")
29-
@Operation(summary = "์ตœ๊ทผ ๋ฐ›์€ ์ชฝ์ง€ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ", description = "24์‹œ๊ฐ„ ๋‚ด ๋ฐ›์€ ์ชฝ์ง€ ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.")
30-
public List<NoteResponse> getLatestNotes() {
31-
return noteService.findLatestNotes();
30+
@Operation(summary = "์ตœ๊ทผ ๋ฐ›์€ ์ชฝ์ง€ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ", description = "24์‹œ๊ฐ„ ๋‚ด ๋ฐ›์€ ์ชฝ์ง€ ๋ชฉ๋ก์„ ์ตœ์‹ ์ˆœ ์ •๋ ฌํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.")
31+
public List<SimpleNoteResponse> getLatestNotes() {
32+
return noteService.findLatestNotesSorted();
3233
}
3334

3435
@GetMapping("/saved")
35-
@Operation(summary = "๋ณด๊ด€๋œ ์ชฝ์ง€ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ", description = "์‚ฌ์šฉ์ž๊ฐ€ ์ €์žฅํ•œ ์ชฝ์ง€ ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.")
36-
public List<NoteResponse> getSavedNotes() {
37-
return noteService.findSavedNotes();
36+
@Operation(summary = "๋ณด๊ด€๋œ ์ชฝ์ง€ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ", description = "์‚ฌ์šฉ์ž๊ฐ€ ์ €์žฅํ•œ ์ชฝ์ง€ ๋ชฉ๋ก์„ ์ƒ์„ฑ์ผ์‹œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.")
37+
public List<SimpleNoteResponse> getSavedNotes(
38+
@RequestParam(value = "sort", required = false, defaultValue = "latest") String sort) {
39+
// TODO : ํŽ˜์ด์ง• ์ถ”๊ฐ€ ์‹œ Pageable๋กœ ์ˆ˜์ •
40+
return noteService.findSavedNotesSorted(sort);
3841
}
3942

4043
@PostMapping
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.wini.domain.note.domain;
2+
3+
import static com.example.wini.global.error.exception.ErrorCode.SORT_ORDER_NOT_FOUND;
4+
5+
import com.example.wini.global.error.exception.CustomException;
6+
import java.util.Arrays;
7+
import lombok.AllArgsConstructor;
8+
import lombok.Getter;
9+
10+
@Getter
11+
@AllArgsConstructor
12+
public enum SortOrder {
13+
ASC("oldest"),
14+
DESC("latest"),
15+
;
16+
17+
private final String value;
18+
19+
public static SortOrder from(String value) {
20+
return Arrays.stream(SortOrder.values())
21+
.filter(sortOrder -> sortOrder.value.equals(value))
22+
.findFirst()
23+
.orElseThrow(() -> new CustomException(SORT_ORDER_NOT_FOUND));
24+
}
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.wini.domain.note.dto.response;
2+
3+
import com.example.wini.domain.note.domain.Note;
4+
import com.example.wini.domain.template.dto.response.*;
5+
import java.time.LocalDateTime;
6+
7+
public record SimpleNoteResponse(Long id, EmotionResponse emotion, boolean isRead, LocalDateTime createdAt) {
8+
public static SimpleNoteResponse from(Note note) {
9+
return new SimpleNoteResponse(
10+
note.getId(), EmotionResponse.from(note.getEmotion()), note.isRead(), note.getCreatedAt());
11+
}
12+
}

โ€Žsrc/main/java/com/example/wini/domain/note/repository/NoteCustomRepository.javaโ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.example.wini.domain.log.dto.response.ActionChange;
44
import com.example.wini.domain.log.dto.response.WeeklyNoteCount;
55
import com.example.wini.domain.note.domain.Note;
6+
import com.example.wini.domain.note.domain.SortOrder;
67
import com.example.wini.domain.template.domain.ActionCategory;
78
import com.example.wini.domain.template.domain.EmotionType;
89
import java.util.List;
@@ -11,9 +12,9 @@
1112
public interface NoteCustomRepository {
1213
Optional<Note> findFullNote(Long noteId);
1314

14-
List<Note> findLatestNotes(Long memberId, Long roomId);
15+
List<Note> findLatestNotesSortedByCreatedAtDesc(Long memberId, Long roomId);
1516

16-
List<Note> findSavedNotes(Long memberId, Long roomId);
17+
List<Note> findSavedNotesSortedByCreatedAt(Long memberId, Long roomId, SortOrder sortOrder);
1718

1819
ActionChange findMostIncreasedPositiveActionChange(Long memberId, Long roomId);
1920

โ€Žsrc/main/java/com/example/wini/domain/note/repository/NoteCustomRepositoryImpl.javaโ€Ž

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import com.example.wini.domain.log.dto.response.ActionChange;
88
import com.example.wini.domain.log.dto.response.WeeklyNoteCount;
99
import com.example.wini.domain.note.domain.Note;
10+
import com.example.wini.domain.note.domain.SortOrder;
1011
import com.example.wini.domain.template.domain.ActionCategory;
1112
import com.example.wini.domain.template.domain.EmotionType;
13+
import com.querydsl.core.types.Order;
14+
import com.querydsl.core.types.OrderSpecifier;
1215
import com.querydsl.core.types.Projections;
1316
import com.querydsl.core.types.dsl.BooleanExpression;
1417
import com.querydsl.core.types.dsl.CaseBuilder;
@@ -47,18 +50,23 @@ public Optional<Note> findFullNote(Long noteId) {
4750
}
4851

4952
@Override
50-
public List<Note> findLatestNotes(Long memberId, Long roomId) {
53+
public List<Note> findLatestNotesSortedByCreatedAtDesc(Long memberId, Long roomId) {
5154
return queryFactory
5255
.selectFrom(note)
5356
.where(isThisRoom(roomId).and(isReceiver(memberId)).and(isCreatedLatest()))
57+
.orderBy(note.createdAt.desc())
5458
.fetch();
5559
}
5660

5761
@Override
58-
public List<Note> findSavedNotes(Long memberId, Long roomId) {
62+
public List<Note> findSavedNotesSortedByCreatedAt(Long memberId, Long roomId, SortOrder sortOrder) {
63+
Order order = sortOrder == SortOrder.ASC ? Order.ASC : Order.DESC;
64+
OrderSpecifier<?> orderSpecifier = new OrderSpecifier<>(order, note.createdAt);
65+
5966
return queryFactory
6067
.selectFrom(note)
6168
.where(isThisRoom(roomId).and(isReceiver(memberId)).and(isSaved()))
69+
.orderBy(orderSpecifier)
6270
.fetch();
6371
}
6472

โ€Žsrc/main/java/com/example/wini/domain/note/service/NoteService.javaโ€Ž

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import com.example.wini.domain.member.domain.Member;
77
import com.example.wini.domain.member.repository.MemberRepository;
88
import com.example.wini.domain.note.domain.Note;
9+
import com.example.wini.domain.note.domain.SortOrder;
910
import com.example.wini.domain.note.dto.request.NoteCreateRequest;
1011
import com.example.wini.domain.note.dto.response.NoteResponse;
12+
import com.example.wini.domain.note.dto.response.SimpleNoteResponse;
1113
import com.example.wini.domain.note.repository.NoteRepository;
1214
import com.example.wini.domain.notification.domain.NotificationType;
1315
import com.example.wini.domain.notification.event.NotificationEvent;
@@ -54,23 +56,24 @@ public NoteResponse findNoteById(Long noteId) {
5456
}
5557

5658
@Transactional(readOnly = true)
57-
public List<NoteResponse> findLatestNotes() {
59+
public List<SimpleNoteResponse> findLatestNotesSorted() {
5860
Member me = memberUtil.getCurrentMember();
5961
Room room = roomRepository
6062
.findOpenRoomByMemberId(me.getId())
6163
.orElseThrow(() -> new CustomException(ROOM_NOT_FOUND));
62-
List<Note> notes = noteRepository.findLatestNotes(me.getId(), room.getId());
63-
return notes.stream().map(NoteResponse::from).toList();
64+
List<Note> notes = noteRepository.findLatestNotesSortedByCreatedAtDesc(me.getId(), room.getId());
65+
return notes.stream().map(SimpleNoteResponse::from).toList();
6466
}
6567

6668
@Transactional(readOnly = true)
67-
public List<NoteResponse> findSavedNotes() {
69+
public List<SimpleNoteResponse> findSavedNotesSorted(String sort) {
6870
Member me = memberUtil.getCurrentMember();
6971
Room room = roomRepository
7072
.findOpenRoomByMemberId(me.getId())
7173
.orElseThrow(() -> new CustomException(ROOM_NOT_FOUND));
72-
List<Note> notes = noteRepository.findSavedNotes(me.getId(), room.getId());
73-
return notes.stream().map(NoteResponse::from).toList();
74+
SortOrder sortOrder = SortOrder.from(sort);
75+
List<Note> notes = noteRepository.findSavedNotesSortedByCreatedAt(me.getId(), room.getId(), sortOrder);
76+
return notes.stream().map(SimpleNoteResponse::from).toList();
7477
}
7578

7679
@Transactional(readOnly = false)

โ€Žsrc/main/java/com/example/wini/global/error/exception/ErrorCode.javaโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public enum ErrorCode {
4747
// Note
4848
NOTE_NOT_FOUND(HttpStatus.NOT_FOUND, "์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋งˆ์Œ์ชฝ์ง€์ž…๋‹ˆ๋‹ค."),
4949
NOTE_RECEIVER_MISMATCH(HttpStatus.BAD_REQUEST, "๋งˆ์Œ์ชฝ์ง€์˜ ์ˆ˜์‹ ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค."),
50+
SORT_ORDER_NOT_FOUND(HttpStatus.BAD_REQUEST, "์ž˜๋ชป๋œ ์ •๋ ฌ ์ˆœ์„œ์ž…๋‹ˆ๋‹ค."),
5051

5152
// Template
5253
EMOTION_TYPE_NOT_FOUND(HttpStatus.BAD_REQUEST, "์ž˜๋ชป๋œ ๊ฐ์ • ๋ถ„๋ฅ˜์ž…๋‹ˆ๋‹ค."),

0 commit comments

Comments
ย (0)