Skip to content

Commit

Permalink
#67 #9 feat: findAllBoards, findAllBoardsByCategory API 반환 GetAllBoar…
Browse files Browse the repository at this point in the history
…dRes 클래스 정의 및 적용
  • Loading branch information
xhaktmchl committed Nov 27, 2022
1 parent dc63b88 commit 907a202
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageReq;
import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageRes;
import com.yogit.server.board.dto.response.BoardRes;
import com.yogit.server.board.dto.response.GetAllBoardRes;
import com.yogit.server.board.service.BoardService;
import com.yogit.server.global.dto.ApplicationResponse;
import io.swagger.annotations.*;
Expand Down Expand Up @@ -119,7 +120,7 @@ public ApplicationResponse<BoardRes> deleteBoard(@RequestBody @Validated DeleteB
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@PostMapping("/get")
public ApplicationResponse<List<List<BoardRes>>> findAllBoards(@RequestBody @Validated GetAllBoardsReq getAllBoardsReq){
public ApplicationResponse<List<List<GetAllBoardRes>>> findAllBoards(@RequestBody @Validated GetAllBoardsReq getAllBoardsReq){
return boardService.findAllBoards(getAllBoardsReq);
}

Expand All @@ -135,7 +136,7 @@ public ApplicationResponse<List<List<BoardRes>>> findAllBoards(@RequestBody @Val
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@PostMapping("/get/category")
public ApplicationResponse<List<BoardRes>> findAllBoardsByCategory(@RequestBody @Validated GetAllBoardsByCategoryReq getAllBoardsByCategoryReq){
public ApplicationResponse<List<GetAllBoardRes>> findAllBoardsByCategory(@RequestBody @Validated GetAllBoardsByCategoryReq getAllBoardsByCategoryReq){
return boardService.findAllBoardsByCategory(getAllBoardsByCategoryReq);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.yogit.server.board.dto.response;

import com.yogit.server.board.entity.Board;
import com.yogit.server.config.domain.BaseStatus;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class GetAllBoardRes {

@ApiModelProperty(example = "1")
@ApiParam(value = "생성된 Board ID")
private Long boardId;

@ApiModelProperty(example = "1")
@ApiParam(value = "도시 ID")
private Long cityId;

@ApiModelProperty(example = "추가 예정")
@ApiParam(value = "유저 프로필 이미지 url")
private String profileImgUrl;

@ApiModelProperty(example = "경복궁 탐사입니다.")
@ApiParam(value = "게시글 제목")
private String title;

@ApiModelProperty(example = "2022-07-13 16:29:30")
@ApiParam(value = "사용자 ID")
private LocalDateTime date; // 모임 시각

@ApiModelProperty(example = "2")
@ApiParam(value = "현재 참여 인원수")
private int currentMember;

@ApiModelProperty(example = "5")
@ApiParam(value = "총 인원수")
private int totalMember;

@ApiModelProperty(example = "2")
@ApiParam(value = "모임 카테고리 ID")
private Long categoryId;

@ApiModelProperty(example = "['이미지 url','이미지2 url']")
@ApiParam(value = "게시글 이미지 url 리스트")
private List<String> imageUrls;

@ApiModelProperty(example = "[1,2,3]")
@ApiParam(value = "게시글 이미지 ID 리스트")
private List<Long> imageIds;

@ApiModelProperty(example = "ACTIVE")
@ApiParam(value = "객체 상태")
private BaseStatus status;

public static GetAllBoardRes toDto(Board board, List<String> imageUrls, String profileImgUrl){
return GetAllBoardRes.builder()
.boardId(board.getId())
.cityId(board.getCity().getId())
.profileImgUrl(profileImgUrl)
.title(board.getTitle())
.date(board.getDate())
.currentMember(board.getCurrentMember())
.totalMember(board.getTotalMember())
.categoryId(board.getCategory().getId())
.imageUrls(imageUrls)
.imageIds(board.getBoardImages().stream().map(image -> image.getId()).collect(Collectors.toList()))
.status(board.getStatus())
.build();
}

@Builder
public GetAllBoardRes(Long boardId, Long cityId, String profileImgUrl, String title, LocalDateTime date, int currentMember, int totalMember, Long categoryId, List<String> imageUrls, List<Long> imageIds, BaseStatus status) {
this.boardId = boardId;
this.cityId = cityId;
this.profileImgUrl = profileImgUrl;
this.title = title;
this.date = date;
this.currentMember = currentMember;
this.totalMember = totalMember;
this.categoryId = categoryId;
this.imageUrls = imageUrls;
this.imageIds = imageIds;
this.status = status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageReq;
import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageRes;
import com.yogit.server.board.dto.response.BoardRes;
import com.yogit.server.board.dto.response.GetAllBoardRes;
import com.yogit.server.global.dto.ApplicationResponse;

import java.util.List;
Expand All @@ -16,9 +17,9 @@ public interface BoardService {

ApplicationResponse<BoardRes> deleteBoard(DeleteBoardReq deleteBoardReq);

ApplicationResponse<List<List<BoardRes>>> findAllBoards(GetAllBoardsReq getAllBoardsReq);
ApplicationResponse<List<List<GetAllBoardRes>>> findAllBoards(GetAllBoardsReq getAllBoardsReq);

ApplicationResponse<List<BoardRes>> findAllBoardsByCategory(GetAllBoardsByCategoryReq getAllBoardsByCategoryReq);
ApplicationResponse<List<GetAllBoardRes>> findAllBoardsByCategory(GetAllBoardsByCategoryReq getAllBoardsByCategoryReq);

ApplicationResponse<BoardRes> findBoard(GetBoardReq getBoardReq);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageReq;
import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageRes;
import com.yogit.server.board.dto.response.BoardRes;
import com.yogit.server.board.dto.response.GetAllBoardRes;
import com.yogit.server.board.entity.Board;
import com.yogit.server.board.entity.BoardImage;
import com.yogit.server.board.entity.BoardUser;
Expand Down Expand Up @@ -150,7 +151,7 @@ public ApplicationResponse<BoardRes> deleteBoard(DeleteBoardReq dto){

@Transactional(readOnly = true)
@Override
public ApplicationResponse<List<List<BoardRes>>> findAllBoards(GetAllBoardsReq dto){
public ApplicationResponse<List<List<GetAllBoardRes>>> findAllBoards(GetAllBoardsReq dto){
int cursor = dto.getCursor();

User user = userRepository.findById(dto.getUserId())
Expand All @@ -166,12 +167,12 @@ public ApplicationResponse<List<List<BoardRes>>> findAllBoards(GetAllBoardsReq d
PageRequest pageRequest = PageRequest.of(cursor, 10, sort); // 페이징 요청 객체

// 사이즈 만큼 반복하면서 각 board category 별 보드 리스트 조회 (10개씩)
List<List<BoardRes>> res = new ArrayList<>();
List<List<GetAllBoardRes>> res = new ArrayList<>();
for(int i=0;i< categoryList.size();i++){
Slice<Board> boards = boardRepository.findAllBoardsByCategory(pageRequest, categoryList.get(i).getId());
// 보드 res에 이미지uuid -> aws s3 url로 변환
List<BoardRes> boardsRes = boards.stream()
.map(board -> BoardRes.toDto(board, awsS3Service.makeUrlsOfFilenames(board.getBoardImagesUUids()), awsS3Service.makeUrlOfFilename(user.getProfileImg())))
List<GetAllBoardRes> boardsRes = boards.stream()
.map(board -> GetAllBoardRes.toDto(board, awsS3Service.makeUrlsOfFilenames(board.getBoardImagesUUids()), awsS3Service.makeUrlOfFilename(user.getProfileImg())))
.collect(Collectors.toList());
// 전체 리스트에 카테고리 별 리스트 추가
res.add(boardsRes);
Expand All @@ -183,7 +184,7 @@ public ApplicationResponse<List<List<BoardRes>>> findAllBoards(GetAllBoardsReq d

@Transactional(readOnly = true)
@Override
public ApplicationResponse<List<BoardRes>> findAllBoardsByCategory(GetAllBoardsByCategoryReq dto){
public ApplicationResponse<List<GetAllBoardRes>> findAllBoardsByCategory(GetAllBoardsByCategoryReq dto){
int cursor = dto.getCursor();

User user = userRepository.findById(dto.getUserId())
Expand All @@ -198,8 +199,8 @@ public ApplicationResponse<List<BoardRes>> findAllBoardsByCategory(GetAllBoardsB

Slice<Board> boards = boardRepository.findAllBoardsByCategory(pageRequest, dto.getCategoryId());
// 보드 res에 이미지uuid -> aws s3 url로 변환
List<BoardRes> boardsRes = boards.stream()
.map(board -> BoardRes.toDto(board, awsS3Service.makeUrlsOfFilenames(board.getBoardImagesUUids()), awsS3Service.makeUrlOfFilename(user.getProfileImg())))
List<GetAllBoardRes> boardsRes = boards.stream()
.map(board -> GetAllBoardRes.toDto(board, awsS3Service.makeUrlsOfFilenames(board.getBoardImagesUUids()), awsS3Service.makeUrlOfFilename(user.getProfileImg())))
.collect(Collectors.toList());
return ApplicationResponse.ok(boardsRes);
}
Expand Down

0 comments on commit 907a202

Please sign in to comment.