Skip to content

Commit f57aade

Browse files
authored
🔀 상태와 회독 분리 Merge /feature/#83
🔀 Feature/#83
2 parents 95574da + 1d78b05 commit f57aade

File tree

9 files changed

+75
-62
lines changed

9 files changed

+75
-62
lines changed

src/main/java/com/boggle_boggle/bbegok/dto/ReadDateAndIdDto.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.boggle_boggle.bbegok.dto;
22

33
import com.boggle_boggle.bbegok.entity.ReadDate;
4+
import com.boggle_boggle.bbegok.enums.ReadStatus;
45
import lombok.AllArgsConstructor;
56
import lombok.Getter;
67
import lombok.NoArgsConstructor;
@@ -15,11 +16,13 @@ public class ReadDateAndIdDto {
1516
private Long readDateId;
1617
private LocalDateTime startReadDate;
1718
private LocalDateTime endReadDate;
19+
private ReadStatus status;
1820

1921
public ReadDateAndIdDto(ReadDate readDate) {
2022
this.readDateId = readDate.getReadDateSeq();
2123
this.startReadDate = readDate.getStartReadDate();
2224
this.endReadDate = readDate.getEndReadDate();
25+
this.status = readDate.getStatus();
2326
}
2427

2528

src/main/java/com/boggle_boggle/bbegok/dto/RecordData.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
@Builder
1616
@AllArgsConstructor
1717
public class RecordData {
18-
private ReadStatus status;
1918
private Double rating;
2019
private List<ReadDateAndIdDto> readDateList;
2120
private List<LibraryListDto> libraries;
2221
private Boolean isBookVisible;
2322

2423
public static RecordData fromEntity(ReadingRecord readingRecord){
2524
return RecordData.builder()
26-
.status(readingRecord.getStatus())
2725
.rating(readingRecord.getRating())
2826
.isBookVisible(readingRecord.getIsBooksVisible())
2927
.readDateList(readingRecord.getReadDateList().stream()
28+
.filter(readDate -> !readDate.getStatus().equals(ReadStatus.pending))
3029
.map(ReadDateAndIdDto::new).toList())
3130
.libraries(readingRecord.getMappingList().stream()
3231
.map(library -> new LibraryListDto(library.getLibrary().getLibrarySeq(),library.getLibrary().getLibraryName())).toList())

src/main/java/com/boggle_boggle/bbegok/dto/request/UpdateReadingRecordRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
@NoArgsConstructor
1717
@ToString
1818
public class UpdateReadingRecordRequest {
19-
private JsonNullable<ReadStatus> readStatus = JsonNullable.undefined();
20-
private JsonNullable<List<ReadDateAndIdDto>> readDateAndIdList = JsonNullable.undefined();
19+
private JsonNullable<List<ReadDateAndIdDto>> readDateList = JsonNullable.undefined();
2120
private JsonNullable<List<Long>> libraryIdList = JsonNullable.undefined();
2221
private JsonNullable<Double> rating = JsonNullable.undefined();
2322
private JsonNullable<Boolean> isVisible = JsonNullable.undefined();

src/main/java/com/boggle_boggle/bbegok/dto/response/LibraryResponse.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,34 @@
66
import com.boggle_boggle.bbegok.enums.ReadStatus;
77
import lombok.AllArgsConstructor;
88
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
910

11+
import java.util.ArrayList;
1012
import java.util.List;
1113

1214
@Getter
13-
@AllArgsConstructor
15+
@NoArgsConstructor
1416
public class LibraryResponse {
1517
private List<LibrariesDto> libraryList;
1618
private List<RecordByStatusDto> statusList;
1719

1820
public static LibraryResponse ofDtos(List<LibrariesDto> librariesDtos, List<RecordByStatusDto> readingRecords) {
19-
LibraryResponse resp = new LibraryResponse(librariesDtos, readingRecords);
20-
21+
LibraryResponse resp = new LibraryResponse();
22+
resp.libraryList = librariesDtos;
23+
resp.statusList = new ArrayList<>();
2124
Long cnt = 0L;
22-
for(RecordByStatusDto dto : resp.getStatusList()) cnt+=dto.getBookCount();
25+
boolean pendingFlag=false, readingFlag=false, completedFlag=false;
26+
for(RecordByStatusDto dto : readingRecords) {
27+
cnt+=dto.getBookCount();
28+
if(dto.getStatus() == LibraryByStatus.completed) completedFlag = true;
29+
else if(dto.getStatus() == LibraryByStatus.pending) pendingFlag = true;
30+
else if(dto.getStatus() == LibraryByStatus.reading) readingFlag = true;
31+
resp.getStatusList().add(dto);
32+
}
33+
if(!pendingFlag) resp.getStatusList().add(new RecordByStatusDto(LibraryByStatus.pending, 0L));
34+
if(!readingFlag) resp.getStatusList().add(new RecordByStatusDto(LibraryByStatus.reading, 0L));
35+
if(!completedFlag) resp.getStatusList().add(new RecordByStatusDto(LibraryByStatus.completed, 0L));
36+
2337
resp.getStatusList().add(0, new RecordByStatusDto(LibraryByStatus.all, cnt));
2438
return resp;
2539
}

src/main/java/com/boggle_boggle/bbegok/entity/ReadDate.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.boggle_boggle.bbegok.entity;
22

3+
import com.boggle_boggle.bbegok.enums.ReadStatus;
34
import jakarta.persistence.*;
45
import lombok.Getter;
56
import lombok.ToString;
@@ -28,18 +29,37 @@ public class ReadDate {
2829
@OneToMany(mappedBy = "readDate", cascade = CascadeType.PERSIST)
2930
private List<Note> notes = new ArrayList<>();
3031

32+
@Enumerated(EnumType.STRING) // ENUM을 스트링으로 저장
33+
@Column(name = "status")
34+
private ReadStatus status;
35+
3136
protected ReadDate(){}
3237

33-
public ReadDate(ReadingRecord record, LocalDateTime startReadDate, LocalDateTime endReadDate) {
38+
public ReadDate(ReadingRecord record, LocalDateTime startReadDate, LocalDateTime endReadDate, ReadStatus status) {
3439
this.readingRecord = record;
3540
this.startReadDate = startReadDate;
3641
this.endReadDate = endReadDate;
42+
this.status = status;
43+
}
44+
45+
public static ReadDate createReadDate(ReadingRecord record, LocalDateTime startReadDate, LocalDateTime endReadDate, ReadStatus status){
46+
return new ReadDate(record, startReadDate, endReadDate, status);
47+
}
48+
49+
public void update(LocalDateTime startReadDate, LocalDateTime endReadDate, ReadStatus status) {
50+
this.startReadDate = startReadDate;
51+
this.endReadDate = endReadDate;
52+
this.status = status;
3753
}
3854

39-
public static ReadDate createReadDate(ReadingRecord record, LocalDateTime startReadDate, LocalDateTime endReadDate){
40-
return new ReadDate(record, startReadDate, endReadDate);
55+
public void removeNoteAssociation() {
56+
for (Note note : this.notes) {
57+
note.updateReadDate(null); // 외래 키를 null로 설정
58+
}
59+
this.notes.clear(); // 로컬 리스트 비우기
4160
}
4261

62+
4363
@Override
4464
public boolean equals(Object o) {
4565
if (this == o) return true;
@@ -54,16 +74,4 @@ public int hashCode() {
5474
return readDateSeq != null ? readDateSeq.hashCode() : 0;
5575
}
5676

57-
public void update(LocalDateTime startReadDate, LocalDateTime endReadDate) {
58-
this.startReadDate = startReadDate;
59-
this.endReadDate = endReadDate;
60-
}
61-
62-
public void removeNoteAssociation() {
63-
for (Note note : this.notes) {
64-
note.updateReadDate(null); // 외래 키를 null로 설정
65-
}
66-
this.notes.clear(); // 로컬 리스트 비우기
67-
}
68-
6977
}

src/main/java/com/boggle_boggle/bbegok/entity/ReadingRecord.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,31 @@ public class ReadingRecord {
4747
@Column(name = "rating")
4848
private Double rating;
4949

50-
@Enumerated(EnumType.STRING) // ENUM을 스트링으로 저장
51-
@Column(name = "status", nullable = false)
52-
private ReadStatus status;
53-
5450
protected ReadingRecord(){}
5551

5652
private ReadingRecord(User user, Book book, LocalDateTime readStartDate, LocalDateTime readEndDate,
57-
List<Library> libraries, Double rating, Boolean visible, ReadStatus readStatus) {
53+
List<Library> libraries, Double rating, Boolean visible, ReadStatus status) {
5854
this.user = user;
5955
this.book = book;
6056
this.rating = rating;
6157
this.isBooksVisible = visible;
62-
this.status = readStatus;
63-
addReadDateList(readStartDate, readEndDate);
58+
addReadDateList(readStartDate, readEndDate, status);
6459
addLibraries(libraries);
6560
}
6661

6762
public static ReadingRecord createReadingRecord(User user, Book book, LocalDateTime readStartDate, LocalDateTime readEndDate,
68-
List<Library> libraries, Double rating, Boolean visible, ReadStatus readStatus) {
69-
return new ReadingRecord(user, book, readStartDate, readEndDate, libraries, rating, visible, readStatus);
63+
List<Library> libraries, Double rating, Boolean visible, ReadStatus status) {
64+
return new ReadingRecord(user, book, readStartDate, readEndDate, libraries, rating, visible, status);
7065
}
7166

7267
//==수정
7368
public void update(ReadStatus readStatus, Double rating, List<ReadDateDto> readDateList,
74-
Boolean visible, List<Library> libraries) {
75-
if(readStatus != null) this.status = readStatus;
69+
Boolean visible, List<Library> libraries, ReadStatus status) {
7670
if(rating != null) this.rating = rating;
7771
if(visible != null) this.isBooksVisible = visible;
7872

7973
if(readDateList != null) {
80-
for(ReadDateDto dto : readDateList) addReadDateList(dto.getStartReadDate(), dto.getEndReadDate());
74+
for(ReadDateDto dto : readDateList) addReadDateList(dto.getStartReadDate(), dto.getEndReadDate(), status);
8175
}
8276

8377
addLibraries(libraries);
@@ -95,19 +89,15 @@ public void addLibrary(Library library) {
9589
ReadingRecordLibraryMapping mapping = ReadingRecordLibraryMapping.createReadingRecordLibraryMapping(this, library);
9690
this.mappingList.add(mapping);
9791
}
98-
public void addReadDateList(LocalDateTime readStartDate, LocalDateTime readEndDate) {
99-
ReadDate readDate = ReadDate.createReadDate(this, readStartDate, readEndDate);
100-
this.readDateList.add(readDate);
92+
public void addReadDateList(LocalDateTime readStartDate, LocalDateTime readEndDate, ReadStatus status) {
93+
ReadDate readDate = ReadDate.createReadDate(this, readStartDate, readEndDate, status);
94+
updateReadDateList(readDate);
10195
}
10296

10397
public void updateReadDateList(ReadDate readDate) {
10498
this.readDateList.add(readDate);
10599
}
106100

107-
public void updateReadStatus(ReadStatus status) {
108-
this.status = status;
109-
}
110-
111101
public void updateRating(Double rating) {
112102
this.rating = rating;
113103
}
@@ -117,8 +107,6 @@ public void updateIsVisible(Boolean visible) {
117107
}
118108

119109
public void removeReadDate(ReadDate readDate) {
120-
System.out.println("삭제 할게 : "+readDate.getReadDateSeq());
121110
boolean b = this.readDateList.remove(readDate);
122-
System.out.println("삭제됨? : "+b);
123111
}
124112
}

src/main/java/com/boggle_boggle/bbegok/repository/ReadingRecordRepository.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public interface ReadingRecordRepository extends JpaRepository<ReadingRecord, Long> {
2020
ReadingRecord findByUserAndBook(User user, Book book);
2121

22-
Optional<ReadingRecord> findByreadingRecordSeqAndUser(Long id, User user);
22+
Optional<ReadingRecord> findByreadingRecordSeqAndUserOrderByReadingRecordSeq(Long id, User user);
2323

2424
@Query(value = """
2525
SELECT DISTINCT r
@@ -40,7 +40,8 @@ List<ReadingRecord> findBooksByUserAndReadDate(
4040
@Query("""
4141
SELECT r
4242
FROM ReadingRecord r
43-
WHERE r.status = :status
43+
JOIN r.readDateList rd
44+
WHERE rd.status = :status
4445
AND r.user = :user
4546
""")
4647
Page<ReadingRecord> findBooksByUserAndStatus(
@@ -50,18 +51,20 @@ Page<ReadingRecord> findBooksByUserAndStatus(
5051
);
5152

5253
@Query("""
53-
SELECT new com.boggle_boggle.bbegok.dto.RecordByStatusDto(r.status, COUNT(r))
54+
SELECT DISTINCT new com.boggle_boggle.bbegok.dto.RecordByStatusDto(rd.status, COUNT(r))
5455
FROM ReadingRecord r
56+
JOIN r.readDateList rd
5557
WHERE r.user = :user
56-
GROUP BY r.status
58+
GROUP BY rd.status
5759
""")
5860
List<RecordByStatusDto> countReadingRecordsByStatus(@Param("user") User user);
5961

6062

6163
@Query("""
6264
SELECT r
6365
FROM ReadingRecord r
64-
WHERE r.status = :status
66+
JOIN r.readDateList rd
67+
WHERE rd.status = :status
6568
AND r.user = :user
6669
AND LOWER(r.book.title) LIKE LOWER(CONCAT('%', :keyword, '%'))
6770
""")

src/main/java/com/boggle_boggle/bbegok/service/NoteService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Map<ReadDateAndIdDto, List<NoteDto>> groupNotesByReadDate(List<Note> note
6565
));
6666

6767
// 3. null 키가 없으면 추가
68-
ReadDateAndIdDto nullKey = new ReadDateAndIdDto(null, null, null);
68+
ReadDateAndIdDto nullKey = new ReadDateAndIdDto(null, null, null, null);
6969
groupedNotes.putIfAbsent(nullKey , new ArrayList<>());
7070

7171
// 4. Note를 NoteDto로 변환하고 ReadDateAndIdDto 기준으로 그룹화
@@ -111,7 +111,7 @@ public User getUser(String userId) {
111111

112112
private ReadingRecord findReadingRecord(Long id, String userId){
113113
User user = getUser(userId);
114-
return readingRecordRepository.findByreadingRecordSeqAndUser(id, user)
114+
return readingRecordRepository.findByreadingRecordSeqAndUserOrderByReadingRecordSeq(id, user)
115115
.orElseThrow(() -> new GeneralException(Code.READING_RECORD_NOT_FOUND));
116116
}
117117

src/main/java/com/boggle_boggle/bbegok/service/ReadingRecordService.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private ReadingRecord findReadingRecord(String isbn, String userId){
122122

123123
private ReadingRecord findReadingRecord(Long id, String userId){
124124
User user = getUser(userId);
125-
return readingRecordRepository.findByreadingRecordSeqAndUser(id, user)
125+
return readingRecordRepository.findByreadingRecordSeqAndUserOrderByReadingRecordSeq(id, user)
126126
.orElseThrow(() -> new GeneralException(Code.READING_RECORD_NOT_FOUND));
127127
}
128128

@@ -133,25 +133,24 @@ private Library findLibrary(String userId, Long libraryId){
133133
}
134134

135135
private void updateReadingRecord(UpdateReadingRecordRequest request, User user, ReadingRecord readingRecord) {
136-
if(request.getReadStatus().isPresent()) {
137-
if(request.getReadStatus().get() == null) throw new GeneralException(Code.BAD_REQUEST, "status can't null");
138-
readingRecord.updateReadStatus(request.getReadStatus().get());
139-
}
140-
if(request.getReadDateAndIdList().isPresent()) {
141-
if(request.getReadDateAndIdList().get() == null) throw new GeneralException(Code.BAD_REQUEST, "readDateIdList can't null");
142-
List<ReadDateAndIdDto> readDateAndIdDtoList = request.getReadDateAndIdList().get();
136+
if(request.getReadDateList().isPresent()) {
137+
if(request.getReadDateList().get() == null) throw new GeneralException(Code.BAD_REQUEST, "readDateIdList can't null");
138+
List<ReadDateAndIdDto> readDateAndIdDtoList = request.getReadDateList().get();
143139

144140
//기존 ReadDate중에서 요청readDate에 없는경우 해당 readDate를 삭제해야하는데,
145141
//1. id가 있으면 업데이트처리 -> 2. id가 없으면 새로운 회독정보 추가 -> 3. 원래 DB와 비교했을때 1에 해당하지 않은 정보들은 삭제
146142
List<ReadDate> readDateList = readingRecord.getReadDateList();
147-
Set<Long> set = new HashSet();
143+
Set<Long> set = new HashSet<>();
148144
for(ReadDateAndIdDto dto : readDateAndIdDtoList) {
149145
if(dto.getReadDateId() != null) {
150146
set.add(dto.getReadDateId());
151147
ReadDate readDate = readDateRepository.findById(dto.getReadDateId())
152148
.orElseThrow(()-> new GeneralException(Code.READ_DATE_NOT_FOUND));
153-
readDate.update(dto.getStartReadDate(), dto.getEndReadDate());
154-
} else readDateRepository.save(ReadDate.createReadDate(readingRecord, dto.getStartReadDate(), dto.getEndReadDate()));
149+
readDate.update(dto.getStartReadDate(), dto.getEndReadDate(), dto.getStatus());
150+
} else {
151+
ReadDate readDate = readDateRepository.save(ReadDate.createReadDate(readingRecord, dto.getStartReadDate(), dto.getEndReadDate(),dto.getStatus()));
152+
set.add(readDate.getReadDateSeq());
153+
}
155154
}
156155
Iterator<ReadDate> iterator = readDateList.iterator();
157156
while (iterator.hasNext()) {

0 commit comments

Comments
 (0)