Skip to content

Commit 75e099d

Browse files
authored
Merge pull request #48 from rhkr8521/feat/#add-return-memo-with-comment,like
[FEAT] 내가 좋아요 누른 메모, 댓글 작성한 메모 리스트 반환 구현
2 parents 6f72fe0 + 5258dd2 commit 75e099d

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed

src/main/java/com/rhkr8521/mapping/api/comment/repository/CommentRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.rhkr8521.mapping.api.comment.entity.Comment;
44
import com.rhkr8521.mapping.api.memo.entity.Memo;
55
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
68

79
import java.util.List;
810

@@ -11,4 +13,7 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
1113

1214
List<Comment> findByMemoId(Long memoId); // 특정 메모의 댓글 찾기
1315
void deleteAllByMemoId(Long memoId);
16+
17+
@Query("SELECT DISTINCT c.memo FROM Comment c WHERE c.member.id = :userId")
18+
List<Memo> findDistinctMemoByMemberId(@Param("userId") Long userId);
1419
}

src/main/java/com/rhkr8521/mapping/api/memo/controller/MemoController.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,36 @@ public ResponseEntity<ApiResponse<Void>> toggleHate(
264264
return ApiResponse.success_only(SuccessStatus.TOGGLE_HATE_SUCCESS);
265265
}
266266

267+
@Operation(
268+
summary = "내가 댓글 작성한 메모 목록 조회 API",
269+
description = "내가 댓글 작성한 메모 목록을 조회합니다."
270+
)
271+
@ApiResponses({
272+
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "전체 메모 조회 성공"),
273+
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 메모를 찾을 수 없습니다."),
274+
})
275+
@GetMapping("/commented")
276+
public ResponseEntity<ApiResponse<List<MemoListResponseDTO>>> getMemosWithMyComments(@AuthenticationPrincipal UserDetails userDetails) {
277+
278+
List<MemoListResponseDTO> memos = memoService.getMemosWithMyComments(userDetails);
279+
return ApiResponse.success(SuccessStatus.SEND_TOTAL_MEMO_SUCCESS, memos);
280+
}
281+
282+
@Operation(
283+
summary = "내가 좋아요 누른 메모 목록 조회 API",
284+
description = "내가 댓글 작성한 메모 목록을 조회합니다."
285+
)
286+
@ApiResponses({
287+
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "전체 메모 조회 성공"),
288+
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "해당 메모를 찾을 수 없습니다."),
289+
})
290+
@GetMapping("/liked")
291+
public ResponseEntity<ApiResponse<List<MemoListResponseDTO>>> getMemosILiked(@AuthenticationPrincipal UserDetails userDetails) {
292+
293+
List<MemoListResponseDTO> memos =memoService.getMemosILiked(userDetails);
294+
return ApiResponse.success(SuccessStatus.SEND_TOTAL_MEMO_SUCCESS, memos);
295+
}
296+
267297
private boolean isImageFile(MultipartFile file) {
268298
// 허용되는 이미지 MIME 타입
269299
String contentType = file.getContentType();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.rhkr8521.mapping.api.memo.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
7+
import java.util.List;
8+
9+
@Data
10+
@Builder
11+
@AllArgsConstructor
12+
public class MemoListResponseDTO {
13+
private Long id; // 메모 ID
14+
private String title; // 제목
15+
private String content; // 내용
16+
private String category; // 카테고리
17+
private long likeCnt; // 좋아요 개수
18+
private long hateCnt; // 싫어요 개수
19+
private List<String> images; // 이미지 목록
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.rhkr8521.mapping.api.memo.repository;
22

3+
import com.rhkr8521.mapping.api.memo.entity.Memo;
34
import com.rhkr8521.mapping.api.memo.entity.MemoLike;
45
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
58
import org.springframework.stereotype.Repository;
69

10+
import java.util.List;
711
import java.util.Optional;
812

913
@Repository
1014
public interface MemoLikeRepository extends JpaRepository<MemoLike, Long> {
1115
Optional<MemoLike> findByMemoIdAndMemberId(Long memoId, Long memberId);
1216
void deleteAllByMemoId(Long memoId);
17+
18+
@Query("SELECT ml.memo FROM MemoLike ml WHERE ml.member.id = :userId")
19+
List<Memo> findMemosByMemberId(@Param("userId") Long userId);
1320
}

src/main/java/com/rhkr8521/mapping/api/memo/repository/MemoRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ List<Memo> findMemosWithinRadius(@Param("lat") double lat,
2222
@Param("km") double km);
2323

2424
List<Memo> findMemosByMemberId(Long memberId);
25+
2526
}

src/main/java/com/rhkr8521/mapping/api/memo/service/MemoService.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,49 @@ public void toggleHate(Long memoId, Long userId) {
382382
memoRepository.save(memo);
383383
}
384384

385+
// 내가 댓글 작성한 메모 목록 조회
386+
@Transactional(readOnly = true)
387+
public List<MemoListResponseDTO> getMemosWithMyComments(UserDetails userDetails) {
388+
Long userId = memberRepository.findByEmail(userDetails.getUsername())
389+
.orElseThrow(() -> new NotFoundException(ErrorStatus.USER_NOTFOUND_EXCEPTION.getMessage()))
390+
.getId();
391+
392+
List<Memo> memos = commentRepository.findDistinctMemoByMemberId(userId);
393+
394+
return memos.stream()
395+
.map(this::convertToDTO)
396+
.collect(Collectors.toList());
397+
}
398+
399+
// 내가 좋아요 누른 메모 목록 조회
400+
@Transactional(readOnly = true)
401+
public List<MemoListResponseDTO> getMemosILiked(UserDetails userDetails) {
402+
Long userId = memberRepository.findByEmail(userDetails.getUsername())
403+
.orElseThrow(() -> new NotFoundException(ErrorStatus.USER_NOTFOUND_EXCEPTION.getMessage()))
404+
.getId();
405+
406+
List<Memo> likedMemos = memoLikeRepository.findMemosByMemberId(userId);
407+
408+
return likedMemos.stream()
409+
.map(this::convertToDTO)
410+
.collect(Collectors.toList());
411+
}
412+
413+
// Memo -> MemoListResponseDTO 변환
414+
private MemoListResponseDTO convertToDTO(Memo memo) {
415+
List<String> imageUrls = memo.getImages().stream()
416+
.map(MemoImage::getImageUrl)
417+
.collect(Collectors.toList());
418+
419+
return MemoListResponseDTO.builder()
420+
.id(memo.getId())
421+
.title(memo.getTitle())
422+
.content(memo.getContent())
423+
.category(memo.getCategory())
424+
.likeCnt(memo.getLikeCnt())
425+
.hateCnt(memo.getHateCnt())
426+
.images(imageUrls)
427+
.build();
428+
}
429+
385430
}

0 commit comments

Comments
 (0)