Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 일지 단건 조회 ResponseDTO 개선 #34

Merged
merged 4 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.unit.daybook.domain.board.controller;

import com.unit.daybook.domain.board.dto.request.AddBoardRequestDto;
import com.unit.daybook.domain.board.dto.response.AddBoardResponseDto;
import com.unit.daybook.domain.board.dto.response.BoardResponseDto;
import com.unit.daybook.domain.board.dto.response.BoardTmpResponse;
import com.unit.daybook.domain.board.entity.ReadBoard;
import com.unit.daybook.domain.board.dto.response.FindBoardListResponse;
import com.unit.daybook.domain.board.dto.response.FindOneBoardResponse;
import com.unit.daybook.domain.board.service.BoardService;
import com.unit.daybook.domain.common.annotation.LoginUsers;
import com.unit.daybook.domain.member.domain.Member;
import com.unit.daybook.global.config.security.CustomUserDetails;
import lombok.RequiredArgsConstructor;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RequestMapping("/board")
Expand All @@ -22,15 +23,17 @@ public class BoardController {
private final BoardService boardService;

@GetMapping("/{boardId}")
public AddBoardResponseDto getBoard(@PathVariable("boardId") Long boardId) {
public FindOneBoardResponse getBoard(
@PathVariable("boardId") Long boardId
) {
return boardService.getBoard(boardId);
}

/**
* 사용자가 작성한 일지 목록 조회
*/
@GetMapping("/boards")
public List<AddBoardResponseDto> getMyBoards(@LoginUsers CustomUserDetails userDetails) {
public FindBoardListResponse getMyBoards(@LoginUsers CustomUserDetails userDetails) {
return boardService.getMyBoards(userDetails.getMemberId());
}

Expand All @@ -40,26 +43,29 @@ public List<AddBoardResponseDto> getMyBoards(@LoginUsers CustomUserDetails userD
* 만약 배치 후 가입한 사용자라면, 자기가 쓰지 않은 최신글
*/
@GetMapping("/random")
public List<AddBoardResponseDto> getRandomBoards(@LoginUsers CustomUserDetails userDetails) {
List<AddBoardResponseDto> result = boardService.getRandomBoards(userDetails.getMemberId());
public List<BoardResponseDto> getRandomBoards(@LoginUsers CustomUserDetails userDetails) {

return result;
return boardService.getRandomBoards(userDetails.getMemberId());
}

@PostMapping
public BoardTmpResponse addBoard(@RequestBody AddBoardRequestDto addBoardRequestDto, @LoginUsers CustomUserDetails userDetails) {
AddBoardResponseDto board = boardService.addBoard(addBoardRequestDto, userDetails.getMemberId());
public BoardTmpResponse addBoard(
@RequestBody AddBoardRequestDto addBoardRequestDto,
@LoginUsers CustomUserDetails userDetails)
{
BoardResponseDto board = boardService.addBoard(addBoardRequestDto, userDetails.getMemberId());
return boardService.getBoardWithHashTag(board.boardId());
}

@PostMapping("/{boardId}")
public BoardTmpResponse modifyBoard(@PathVariable("boardId") Long boardId, @RequestBody AddBoardRequestDto addBoardRequestDto) {
return boardService.modifyBoard(boardId, addBoardRequestDto);
}
// @PostMapping("/{boardId}")
// public BoardTmpResponse modifyBoard(@PathVariable("boardId") Long boardId, @RequestBody AddBoardRequestDto addBoardRequestDto) {
// return boardService.modifyBoard(boardId, addBoardRequestDto);
// }

@DeleteMapping("/{boardId}")
public Map<String, Long> deleteBoard(@PathVariable("boardId") Long boardId) {
return boardService.deleteBoard(boardId);
public ResponseEntity<Void> deleteBoard(@PathVariable("boardId") Long boardId) {
boardService.deleteBoard(boardId);
return ResponseEntity.ok().build();
}

@GetMapping("/test")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.unit.daybook.domain.board.dto.response;

import com.unit.daybook.domain.board.entity.Board;
import com.unit.daybook.domain.board.entity.Hashtag;

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

public record BoardResponseDto(
Long boardId,
String content,
Long respectBoardId,
Long authorId,
String category,
Long hearts,
List<String> hashtags,
String paperType,
LocalDateTime createdAt
) {

public static BoardResponseDto from(Board board) {
List<String> hashContents = board.getHashtags().stream().map(Hashtag::getContent).toList();
return new BoardResponseDto(
board.getBoardId(),
board.getContent(),
board.getRespectBoardId(),
board.getMemeber().getId(),
board.getCategory(),
board.getHearts(),
hashContents,
board.getPaperType(),
board.getCreatedAt()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class BoardTmpResponse {
private String category;
private Long hearts;
private String papaerType;
private List<String> hashtags;
private List<String> hashtags;
private LocalDateTime createdAt;

@Builder
public BoardTmpResponse(AddBoardResponseDto dto, List<String> hashtags) {
public BoardTmpResponse(FindOneBoardResponse dto, List<String> hashtags) {
this.boardId = dto.boardId();
this.content = dto.content();
this.respectBoardId = dto.respectBoardId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.unit.daybook.domain.board.dto.response;

import java.util.List;

import com.unit.daybook.domain.board.entity.Board;

public record FindBoardListResponse (
List<FindBoardResponse> boards
) {
public static FindBoardListResponse from(List<Board> board) {
return new FindBoardListResponse(board.stream().map(FindBoardResponse::from).toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.unit.daybook.domain.board.dto.response;

import java.time.LocalDateTime;

import com.unit.daybook.domain.board.entity.Board;

public record FindBoardResponse(
Long boardId,
String content,
Long respectBoardId,
Long authorId,
String category,
Long hearts,
String paperType,
LocalDateTime createdAt
) {
public static FindBoardResponse from(Board board) {
return new FindBoardResponse(
board.getBoardId(),
board.getContent(),
board.getRespectBoardId(),
board.getMemeber().getId(),
board.getCategory(),
board.getHearts(),
board.getPaperType(),
board.getCreatedAt()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.unit.daybook.domain.board.dto.response;

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

import com.unit.daybook.domain.board.entity.Board;
import com.unit.daybook.domain.board.entity.Hashtag;
import com.unit.daybook.domain.reaction.dto.response.ReactionTypeAndCount;
import com.unit.daybook.domain.reaction.entity.Reaction;

public record FindOneBoardResponse(
Long boardId,
String content,
Long respectBoardId,
Long authorId,

String category,
Long hearts,
List<String> hashtags,
String paperType,
List<ReactionTypeAndCount> reactions,
LocalDateTime createdAt
) {
public static FindOneBoardResponse of(Board board, List<ReactionTypeAndCount> reactions) {
List<String> hashContents = board.getHashtags().stream().map(Hashtag::getContent).toList();
Long heartsFromReactions = reactions.stream().mapToLong(ReactionTypeAndCount::count).sum();
return new FindOneBoardResponse(
board.getBoardId(),
board.getContent(),
board.getRespectBoardId(),
board.getMemeber().getId(),
board.getCategory(),
heartsFromReactions,
hashContents,
board.getPaperType(),
reactions,
board.getCreatedAt()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
import org.springframework.stereotype.Repository;

@Repository
public interface BoardRepository extends JpaRepository<Board, Long> {
public interface BoardRepository extends JpaRepository<Board, Long>, BoardRepositoryCustom {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.unit.daybook.domain.board.repository;

import java.util.List;

import com.unit.daybook.domain.board.entity.Board;

public interface BoardRepositoryCustom {
List<Board> findBoardsByMemberId(Long memberId);
List<Board> findBoardInBoardIds(List<Long> boardIds);

List<Board> findNotReadBoardsByMemberId(Long memberId, List<Board> alreadyReadBoards);

List<Board> findCurrentBoards(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ public List<Board> findBoardsByMemberId(Long memberId) {

}

public List<Board> findNotReadBoardsByMemberId(Long memberId, List<Long> aleadyReadBoardIds) {
public List<Board> findNotReadBoardsByMemberId(Long memberId, List<Board> alreadyReadBoardIds) {
return queryFactory
.select(board)
.from(board)
.join(board.memeber, member).fetchJoin()
.where(
// member.id.eq(memberId)
//.and(
board.boardId.notIn(aleadyReadBoardIds)
board.in(alreadyReadBoardIds)
//)
)
.fetch();

}

public List<Board> findBoardInBoardIds(List<Long> todayBoards) {
public List<Board> findBoardInBoardIds(List<Long> todayBoardIds) {
return queryFactory
.select(board)
.from(board)
.where(
board.boardId.in(todayBoards)
board.boardId.in(todayBoardIds)
)
.fetch();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import org.springframework.stereotype.Repository;

@Repository
public interface HashtagRepository extends JpaRepository<Hashtag, Long> {
public interface HashtagRepository extends JpaRepository<Hashtag, Long>, HashtagRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package com.unit.daybook.domain.board.repository;

import java.util.List;

public interface HashtagRepositoryCustom {
List<String> findAllByBoardId(Long boardId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
import org.springframework.stereotype.Repository;

@Repository
public interface ReadBoardRepository extends JpaRepository<ReadBoard, Long> {
public interface ReadBoardRepository extends JpaRepository<ReadBoard, Long>, ReadBoardRepositoryCustom {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package com.unit.daybook.domain.board.repository;

import java.util.List;

import com.unit.daybook.domain.board.entity.Board;

public interface ReadBoardRepositoryCustom {
List<Board> findBoardsByMemberId(Long memberId);

List<Long> findTodayBoardsByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.unit.daybook.domain.board.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.unit.daybook.domain.board.entity.Board;
import com.unit.daybook.domain.board.entity.ReadBoard;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

Expand All @@ -14,21 +17,19 @@

@Repository
@RequiredArgsConstructor
public class ReadBoardRepositoryImpl implements BoardRepositoryCustom {
public class ReadBoardRepositoryImpl implements ReadBoardRepositoryCustom {
private final JPAQueryFactory queryFactory;

public List<Long> findBoardsByMemberId(Long memberId) {
public List<Board> findBoardsByMemberId(Long memberId) {

List<Long> boards = queryFactory
.select(readBoard.board.boardId)
return queryFactory
.select(readBoard.board)
.from(readBoard)
.innerJoin(readBoard.member, member)
.where(
member.id.eq(memberId)
)
.fetch();

return boards;
}

public List<Long> findTodayBoardsByMemberId(Long memberId) {
Expand All @@ -38,10 +39,14 @@ public List<Long> findTodayBoardsByMemberId(Long memberId) {
.select(readBoard.board.boardId)
.from(readBoard)
.innerJoin(readBoard.board, board)
.on(readBoard.board.boardId.eq(board.boardId))
.innerJoin(readBoard.member, member)
.on(readBoard.member.id.eq(member.id))
.where(
member.id.eq(memberId)
.and(readBoard.createdAt.between(currentDate.atStartOfDay(), currentDate.atStartOfDay().plusDays(1).minusNanos(1))
.and(readBoard.createdAt.between(
currentDate.atStartOfDay(),
currentDate.atStartOfDay().plusDays(1).minusNanos(1))
))
.fetch();
}
Expand Down
Loading
Loading