Skip to content

Commit

Permalink
feat: 내가 쓴 동행글 목록 조회 api를 구현한다. (#45)
Browse files Browse the repository at this point in the history
* refactor: 메소드 이름을 변경한다.

* feat: 내가 쓴 동행글 목록 조회 api를 구현한다.

* refactor: 누락된 import를 추가한다.
  • Loading branch information
min429 authored Aug 31, 2024
1 parent e766fb6 commit 8bbbdfa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public ResponseEntity<PageResponse<AccompanyBoardThumbnail>> readAll(
return ResponseEntity.ok(accompanyBoardService.getAllBoards(request, region));
}

@Operation(summary = "내가 쓴 동행글 목록 조회")
@PostMapping("/mine")
public ResponseEntity<PageResponse<AccompanyBoardThumbnail>> readMine(
@RequestBody @Valid PageRequest request,
@AuthenticationPrincipal JwtAuthentication user) {
return ResponseEntity.ok(accompanyBoardService.getMyBoards(request, user.getId()));
}

@Operation(summary = "동행글 상세 조회")
@GetMapping("/{id}")
public ResponseEntity<ReadAccompanyBoardResponse> read(@PathVariable Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.dnd.accompany.domain.accompany.api.dto.PageRequest.*;
import static com.dnd.accompany.domain.accompany.entity.QAccompanyBoard.*;
import static com.dnd.accompany.domain.accompany.entity.QAccompanyImage.*;
import static com.dnd.accompany.domain.accompany.entity.QAccompanyRequest.*;
import static com.dnd.accompany.domain.accompany.entity.QAccompanyTag.*;
import static com.dnd.accompany.domain.accompany.entity.QAccompanyUser.*;
import static com.dnd.accompany.domain.accompany.entity.enums.Region.*;
Expand All @@ -12,12 +13,14 @@

import java.util.List;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.stereotype.Repository;

import com.dnd.accompany.domain.accompany.api.dto.FindBoardThumbnailsResult;
import com.dnd.accompany.domain.accompany.api.dto.FindSlicesResult;
import com.dnd.accompany.domain.accompany.entity.enums.Region;
import com.dnd.accompany.domain.accompany.infrastructure.querydsl.interfaces.AccompanyBoardRepositoryCustom;
import com.querydsl.core.BooleanBuilder;
Expand Down Expand Up @@ -102,7 +105,7 @@ public Slice<FindBoardThumbnailsResult> findBoardThumbnails(String cursor, int s
}

@Override
public Slice<FindBoardThumbnailsResult> findBoardThumbnailsByUserId(String cursor, int size, Long userId) {
public Slice<FindBoardThumbnailsResult> findRecordsByUserId(String cursor, int size, Long userId) {
List<FindBoardThumbnailsResult> content = queryFactory
.select(Projections.constructor(FindBoardThumbnailsResult.class,
accompanyBoard.id,
Expand Down Expand Up @@ -134,6 +137,40 @@ public Slice<FindBoardThumbnailsResult> findBoardThumbnailsByUserId(String curso
return createSlice(size, content);
}

@Override
public Slice<FindBoardThumbnailsResult> findBoardThumbnailsByHostId(String cursor, int size, Long hostId) {
List<FindBoardThumbnailsResult> content = queryFactory
.select(Projections.constructor(FindBoardThumbnailsResult.class,
accompanyBoard.id,
accompanyBoard.title,
accompanyBoard.region,
accompanyBoard.startDate,
accompanyBoard.endDate,
user.nickname,
Expressions.stringTemplate("GROUP_CONCAT(DISTINCT {0})", accompanyImage.imageUrl),
Expressions.stringTemplate(
"CONCAT(DATE_FORMAT({0}, '%Y%m%d%H%i%S'), LPAD(CAST({1} AS STRING), 6, '0'))",
accompanyBoard.updatedAt,
accompanyBoard.id
))
)
.from(accompanyUser)
.join(accompanyUser.accompanyBoard, accompanyBoard)
.join(accompanyUser.user, user)
.leftJoin(accompanyImage).on(accompanyImage.accompanyBoard.id.eq(accompanyBoard.id))
.where(accompanyUser.user.id.eq(hostId))
.where(isHost())
.where(cursorCondition(cursor, accompanyBoard.updatedAt, accompanyBoard.id))
.groupBy(accompanyBoard.id, accompanyBoard.title, accompanyBoard.region,
accompanyBoard.startDate, accompanyBoard.endDate, user.nickname,
accompanyUser.id)
.orderBy(accompanyBoard.updatedAt.desc(), accompanyBoard.id.desc())
.limit(size + 1)
.fetch();

return createSlice(size, content);
}

@Override
public boolean isHostOfBoard(Long userId, Long boardId) {
Integer fetchCount = queryFactory.selectOne()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.dnd.accompany.domain.accompany.infrastructure.querydsl.interfaces;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import com.dnd.accompany.domain.accompany.api.dto.FindBoardThumbnailsResult;
import com.dnd.accompany.domain.accompany.entity.enums.Region;

public interface AccompanyBoardRepositoryCustom {
Slice<FindBoardThumbnailsResult> findBoardThumbnailsByKeyword(String cursor, int size, String keyword);


Slice<FindBoardThumbnailsResult> findBoardThumbnails(String cursor, int size, Region region);

Slice<FindBoardThumbnailsResult> findBoardThumbnailsByUserId(String cursor, int size, Long userId);
Slice<FindBoardThumbnailsResult> findRecordsByUserId(String cursor, int size, Long userId);

Slice<FindBoardThumbnailsResult> findBoardThumbnailsByHostId(String cursor, int size, Long userId);

Slice<FindBoardThumbnailsResult> findBoardThumbnailsByKeyword(String cursor, int size, String keyword);

boolean isHostOfBoard(Long userId, Long boardId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -50,14 +49,23 @@ public AccompanyBoard save(CreateAccompanyBoardRequest request) {
}

@Transactional(readOnly = true)
public PageResponse<AccompanyBoardThumbnail> getMatchedBoards(PageRequest request, String keyword) {
public PageResponse<AccompanyBoardThumbnail> getMyBoards(PageRequest request, Long userId) {
Slice<FindBoardThumbnailsResult> sliceResult = accompanyBoardRepository.findBoardThumbnailsByHostId(request.cursor(), request.size(), userId);

List<AccompanyBoardThumbnail> thumbnails = getBoardThumbnails(sliceResult.getContent());

return new PageResponse<>(sliceResult.hasNext(), thumbnails, getLastCursor(sliceResult.getContent()));
}

public PageResponse<AccompanyBoardThumbnail> getMatchedBoards(PageRequest request, String keyword) {
Slice<FindBoardThumbnailsResult> sliceResult = accompanyBoardRepository.findBoardThumbnailsByKeyword(request.cursor(), request.size(), keyword);

List<AccompanyBoardThumbnail> thumbnails = getBoardThumbnails(sliceResult.getContent());

return new PageResponse<>(sliceResult.hasNext(), thumbnails, getLastCursor(sliceResult.getContent()));
}

@Transactional(readOnly = true)
public PageResponse<AccompanyBoardThumbnail> getAllBoards(PageRequest request, Region region) {
Slice<FindBoardThumbnailsResult> sliceResult = accompanyBoardRepository.findBoardThumbnails(request.cursor(), request.size(), region);

Expand All @@ -68,7 +76,7 @@ public PageResponse<AccompanyBoardThumbnail> getAllBoards(PageRequest request, R

@Transactional(readOnly = true)
public PageResponse<AccompanyBoardThumbnail> getAllRecords(PageRequest request, Long userId) {
Slice<FindBoardThumbnailsResult> sliceResult = accompanyBoardRepository.findBoardThumbnailsByUserId(request.cursor(), request.size(), userId);
Slice<FindBoardThumbnailsResult> sliceResult = accompanyBoardRepository.findRecordsByUserId(request.cursor(), request.size(), userId);

List<AccompanyBoardThumbnail> thumbnails = getBoardThumbnails(sliceResult.getContent());

Expand Down

0 comments on commit 8bbbdfa

Please sign in to comment.