Skip to content

Commit

Permalink
Merge branch 'develop' into test/#140/dummyDataApi
Browse files Browse the repository at this point in the history
Signed-off-by: suuumim <83461362+tnals2384@users.noreply.github.com>
  • Loading branch information
tnals2384 authored Sep 10, 2024
2 parents e4b8162 + 723297e commit 64aabbf
Show file tree
Hide file tree
Showing 32 changed files with 251 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ public static class AnswerId {
@Builder
public static class AnswerInfo {
private Long answerId;
private Long writerId;
private String profileImage;
private String nickname;
private String content;
private Boolean isMyAnswer;
private Boolean isMine;
private Boolean isReported;
private Boolean isLiked;
private String writeAt;
}

@Getter
Expand All @@ -44,11 +47,13 @@ public static class AnswerLike {
public static class MemberAnswerInfo {
private Long questionId;
private Long answerId;
private Long writerId;
private String nickname;
private String profileImage;
private String content;
private int heartCount;
private String writeAt;
private Boolean isLiked;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,31 @@ public AnswerList toAnswerList(List<AnswerInfo> answerInfoList) {
.build();
}

public AnswerInfo toAnswerInfo(Answer answer, Long memberId, Boolean isReported) {
public AnswerInfo toAnswerInfo(Answer answer, Long memberId, Boolean isReported, Boolean isLiked, Boolean isMine) {
return AnswerInfo.builder()
.answerId(answer.getId())
.writerId(answer.getMember().getId())
.profileImage(answer.getMember().getProfileImage())
.nickname(answer.getMember().getNickname())
.content(answer.getContent())
.isMyAnswer(answer.getMember().getId() == memberId)
.isMine(isMine)
.isReported(isReported)
.isLiked(isLiked)
.writeAt(answer.getCreatedAt().toString())
.build();
}

public MemberAnswerInfo toMemberAnswerInfo(Answer answer, int heartCount) {
public MemberAnswerInfo toMemberAnswerInfo(Answer answer, int heartCount, Boolean isLiked) {
return MemberAnswerInfo.builder()
.questionId(answer.getQuestion().getId())
.answerId(answer.getId())
.writerId(answer.getMember().getId())
.nickname(answer.getMember().getNickname())
.profileImage(answer.getMember().getProfileImage())
.content(answer.getContent())
.heartCount(heartCount)
.writeAt(answer.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy.MM.dd")))
.writeAt(answer.getCreatedAt().toString())
.isLiked(isLiked)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ public Set<Long> getMemberHeartsAnswer(Long memberId) {
}
return answerIds;
}

public boolean isMemberLikedAnswer(Long memberId, Long answerId) {
String key = ANSWER_HEART_KEY_PREFIX + answerId;
String memberKey = MEMBER_KEY_PREFIX + memberId;
return redisTemplate.opsForSet().isMember(key, memberKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ public AnswerResponse.AnswerId createAnswer(Member loginMember, Long questionId,
Member member = memberService.findMember(loginMember.getId());
Question question = questionService.findQuestion(questionId);

if (answerRepository.existsByQuestionAndMember(question, loginMember)) {
throw new RestApiException(AnswerErrorCode.ANSWER_ALREADY_EXIST);
}

//답변 저장
Answer answer = answerRepository.save(answerMapper.toAnswerEntity(request, member, question));
// answer.getQuestion().increaseCommentCount();

return new AnswerResponse.AnswerId(answer.getId());
}
Expand All @@ -66,6 +71,7 @@ public AnswerResponse.AnswerId deleteAnswer(Member loginMember, Long answerId) {
Answer answer = findAnswer(answerId);

checkPermission(loginMember, answer);
// answer.getQuestion().decreaseCommentCount();

answer.delete();

Expand All @@ -90,14 +96,14 @@ public AnswerList getAnswerList(Long memberId, Long questionId, String keyword,
answerRepository.findByQuestion(questionId, pageable).orElseThrow(()
-> new RestApiException(AnswerErrorCode.ANSWER_NOT_FOUND))
.stream()
.map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer)))
.map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer), answerHeartRedisRepository.isMemberLikedAnswer(memberId, answer.getId()), answer.getMember().getId().equals(memberId)))
.toList());
} else {
return answerMapper.toAnswerList(
answerRepository.findByQuestionAndKeyword(questionId, keyword, pageable).orElseThrow(()
-> new RestApiException(AnswerErrorCode.ANSWER_NOT_FOUND))
.stream()
.map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer)))
.map(answer -> answerMapper.toAnswerInfo(answer, memberId, reportRepository.existsReportByAnswer(answer), answerHeartRedisRepository.isMemberLikedAnswer(memberId, answer.getId()), answer.getMember().getId().equals(memberId)))
.toList());
}

Expand All @@ -109,7 +115,7 @@ public MemberAnswerList getMemberAnswer(Member member) {
List<Answer> answers = answerRepository.findByMember(member).orElse(null);
return answerMapper.toMemberAnswerList(
answers.stream()
.map(answer -> answerMapper.toMemberAnswerInfo(answer, answerHeartRedisRepository.getAnswerHeartsCount(answer.getId())))
.map(answer -> answerMapper.toMemberAnswerInfo(answer, answerHeartRedisRepository.getAnswerHeartsCount(answer.getId()), answerHeartRedisRepository.isMemberLikedAnswer(member.getId(), answer.getId())))
.toList()
);
}
Expand All @@ -120,7 +126,7 @@ public MemberAnswerList getMemberHeartAnswer(Member member) {
return answerMapper.toMemberAnswerList(
answerHeartRedisRepository.getMemberHeartsAnswer(member.getId())
.stream()
.map(answerId -> answerMapper.toMemberAnswerInfo(findAnswer((answerId)), answerHeartRedisRepository.getAnswerHeartsCount(answerId)))
.map(answerId -> answerMapper.toMemberAnswerInfo(findAnswer((answerId)), answerHeartRedisRepository.getAnswerHeartsCount(answerId), answerHeartRedisRepository.isMemberLikedAnswer(member.getId(), answerId)))
.toList()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static class AnswerCommentHeart {
@Builder
public static class AnswerCommentInfo {
private Long answerCommentId;
private String writer;
private Long writerId;
private String content;
private Long heartCount;
private LocalDateTime createdAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public AnswerComment toAnswerCommentEntity(Member member, Answer answer, String
public AnswerCommentInfo toAnswerCommentInfo(AnswerComment comment, Long heartCount) {
return AnswerCommentInfo.builder()
.answerCommentId(comment.getId())
.writer(comment.getMember().getNickname())
.writerId(comment.getMember().getId())
.content(comment.getContent())
.heartCount(heartCount)
.createdAt(comment.getCreatedAt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ private BaseResponse<BoardResponse.BoardCreate> createBoard(
})
@GetMapping()
private BaseResponse<BoardResponse.BoardsGetByBoardType> getBoardsByBoardType(
@AuthMember Member member,
@RequestParam(name = "boardType", required = false) BoardType boardType
// TODO: 페이징 프론트 이슈로 추후 구현
// @PageableDefault(sort = "created_at", direction = Sort.Direction.DESC) @Parameter(hidden = true) Pageable pageable
) {
return BaseResponse.onSuccess(boardService.getBoardsByBoardType(boardType));
return BaseResponse.onSuccess(boardService.getBoardsByBoardType(member, boardType));
}

@Operation(summary = "게시글 삭제 API", description = "게시글을 삭제합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static class BoardCreate {
@AllArgsConstructor
@NoArgsConstructor
public static class BoardsGetByBoardType {
private List<BoardsGetByBoardTypeBoardInfo> boards = new ArrayList<>();
private List<BoardsGetByBoardTypeBoardInfo> boards;
}

@Getter
Expand All @@ -39,6 +39,10 @@ public static class BoardsGetByBoardTypeBoardInfo {
private Integer heartCount;
private Integer commentCount;
private LocalDateTime createAt;
private Boolean liLiked;
private Boolean isMine;
private Boolean isReported;
private Boolean isLiked;
}

@Getter
Expand All @@ -54,7 +58,7 @@ public static class BoardDelete {
@AllArgsConstructor
@NoArgsConstructor
public static class BoardsSearchByKeyword {
private List<BoardsSearchByKeywordBoardInfo> boards = new ArrayList<>();
private List<BoardsSearchByKeywordBoardInfo> boards;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.server.capple.domain.board.entity;

import com.server.capple.domain.answer.dto.AnswerRequest;
import com.server.capple.domain.member.entity.Member;
import com.server.capple.global.common.BaseEntity;
import jakarta.persistence.*;
Expand Down Expand Up @@ -31,4 +32,12 @@ public class Board extends BaseEntity {

@Column(nullable = false)
private Integer commentCount;

public void increaseCommentCount() {
this.commentCount += 1;
}

public void decreaseCommentCount() {
this.commentCount -= 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ public BoardResponse.BoardsGetByBoardType toBoardsGetByBoardType(

public BoardResponse.BoardsGetByBoardTypeBoardInfo toBoardsGetByBoardTypeBoardInfo(
Board board,
Integer boardHeartsCount) {
Integer boardHeartsCount,
Boolean isLiked,
Boolean isMine,
Boolean isReported) {
return BoardResponse.BoardsGetByBoardTypeBoardInfo.builder()
.boardId(board.getId())
.writerId(board.getWriter().getId())
.content(board.getContent())
.heartCount(boardHeartsCount)
// TODO : 댓글 작성 API 나오면 추후 구현
.commentCount(0)
.commentCount(board.getCommentCount())
.createAt(board.getCreatedAt())
.isLiked(isLiked)
.isMine(isMine)
// TODO: BoardReport 관련 테이블 구현 후 수정 요망
.isReported(isReported)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class BoardHeartRedisRepository implements Serializable {
private final RedisTemplate<String, String> redisTemplate;

// 게시판 좋아요 토글
public Boolean toggleBoardHeart(Long boardId, Long memberId) {
public Boolean toggleBoardHeart(Long memberId, Long boardId) {
String key = BOARD_HEART_KEY_PREFIX + boardId.toString();
String member = MEMBER_KEY_PREFIX + memberId.toString();
String createAtKey = key + ":" + member + ":createAt"; // member ID를 포함한 createAtKey
Expand All @@ -45,7 +45,6 @@ public Boolean toggleBoardHeart(Long boardId, Long memberId) {
}
}

//
public String getBoardHeartCreateAt(Long boardId, Long memberId) {
String createAtKey = BOARD_HEART_KEY_PREFIX + boardId.toString() + ":" + MEMBER_KEY_PREFIX + memberId.toString() + ":createAt";
return redisTemplate.opsForValue().get(createAtKey);
Expand Down Expand Up @@ -73,6 +72,11 @@ public Set<Long> getMemberHeartsBoard(Long memberId) {
return boardIds;
}

public boolean isMemberLikedBoard(Long memberId, Long boardId) {
String key = BOARD_HEART_KEY_PREFIX + boardId;
String memberKey = MEMBER_KEY_PREFIX + memberId;
return redisTemplate.opsForSet().isMember(key, memberKey);
}

//더미 데이터 생성용
public void generateDummyBoardLikes(int boardCount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public interface BoardService {
BoardCreate createBoard(Member member, BoardType boardType, String content);

BoardsGetByBoardType getBoardsByBoardType(BoardType boardType);
BoardsGetByBoardType getBoardsByBoardType(Member member, BoardType boardType);

BoardDelete deleteBoard(Member member, Long boardId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.server.capple.domain.board.mapper.BoardMapper;
import com.server.capple.domain.board.repository.BoardHeartRedisRepository;
import com.server.capple.domain.board.repository.BoardRepository;
import com.server.capple.domain.boardComment.repository.BoardCommentHeartRedisRepository;
import com.server.capple.domain.member.entity.Member;
import com.server.capple.global.exception.RestApiException;
import com.server.capple.global.exception.errorCode.BoardErrorCode;
Expand Down Expand Up @@ -37,7 +38,7 @@ public BoardResponse.BoardCreate createBoard(Member member, BoardType boardType,
}

@Override
public BoardResponse.BoardsGetByBoardType getBoardsByBoardType(BoardType boardType) {
public BoardResponse.BoardsGetByBoardType getBoardsByBoardType(Member member, BoardType boardType) {
List<Board> boards = new ArrayList<>();
if (boardType == null) {
boards = boardRepository.findAll();
Expand All @@ -49,7 +50,8 @@ public BoardResponse.BoardsGetByBoardType getBoardsByBoardType(BoardType boardTy
throw new RestApiException(BoardErrorCode.BOARD_BAD_REQUEST);
}
return boardMapper.toBoardsGetByBoardType(boards.stream()
.map(board -> boardMapper.toBoardsGetByBoardTypeBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId())))
// TODO: BoardReport 관련 테이블 구현 후 수정 요망
.map(board -> boardMapper.toBoardsGetByBoardTypeBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId()), boardHeartRedisRepository.isMemberLikedBoard(member.getId(), board.getId()), board.getWriter().getId().equals(member.getId()), false))
.toList()
);
}
Expand All @@ -76,7 +78,7 @@ public BoardResponse.BoardsSearchByKeyword searchBoardsByKeyword(String keyword)
@Override
public BoardResponse.BoardToggleHeart toggleBoardHeart(Member member, Long boardId) {
Board board = findBoard(boardId);
System.out.println(boardHeartRedisRepository.getBoardHeartCreateAt(board.getId(), member.getId()));
// System.out.println(boardHeartRedisRepository.getBoardHeartCreateAt(board.getId(), member.getId()));

Boolean isLiked = boardHeartRedisRepository.toggleBoardHeart(member.getId(), board.getId());
return new BoardResponse.BoardToggleHeart(boardId, isLiked);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class BoardCommentHeart {
@Builder
public static class BoardCommentInfo {
private Long boardCommentId;
private String writer;
private Long writerId;
private String content;
private Long heartCount;
private Boolean isLiked;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public BoardComment toBoardCommentEntity(Member member, Board board, String comm
public BoardCommentInfo toBoardCommentInfo(BoardComment comment, Long heartCount, Boolean isLiked) {
return BoardCommentInfo.builder()
.boardCommentId(comment.getId())
.writer(comment.getMember().getNickname())
.writerId(comment.getMember().getId())
.content(comment.getContent())
.heartCount(heartCount)
.isLiked(isLiked)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public BoardCommentId createBoardComment(Member member, Long boardId, BoardComme

BoardComment boardComment = boardCommentRepository.save(
boardCommentMapper.toBoardCommentEntity(loginMember, board, request.getComment()));
board.increaseCommentCount();

return new BoardCommentId(boardComment.getId());
}
Expand All @@ -59,6 +60,7 @@ public BoardCommentId deleteBoardComment(Member member, Long commentId) {
BoardComment boardComment = findBoardComment(commentId);
checkPermission(member, boardComment);

boardComment.getBoard().decreaseCommentCount();;
boardComment.delete();

return new BoardCommentId(boardComment.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.server.capple.domain.question.controller;

import com.server.capple.config.security.AuthMember;
import com.server.capple.domain.board.dto.BoardResponse;
import com.server.capple.domain.member.entity.Member;
import com.server.capple.domain.question.dto.response.QuestionResponse;
import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionSummary;
import com.server.capple.domain.question.dto.response.QuestionResponse.QuestionInfos;
import com.server.capple.domain.question.service.QuestionService;
Expand All @@ -11,9 +13,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@Tag(name = "질문 API", description = "질문 관련 API")
@RestController
Expand Down Expand Up @@ -41,6 +41,13 @@ private BaseResponse<QuestionInfos> getQuestions(@AuthMember Member member) {
return BaseResponse.onSuccess(questionService.getQuestions(member));
}

@Operation(summary = "질문 좋아요/취소 API", description = " 질문 좋아요/취소 API 입니다." +
"pathvariable 으로 questionId를 주세요.")
@PostMapping("/{questionId}/heart")
public BaseResponse<QuestionResponse.QuestionToggleHeart> toggleBoardHeart(@AuthMember Member member, @PathVariable(value = "questionId") Long questionId) {
return BaseResponse.onSuccess(questionService.toggleQuestionHeart(member, questionId));
}


// @Operation(summary = "최근 지난 질문 조회 API", description = "최근 지난 질문을 조회합니다.")
// @ApiResponses(value = {
Expand Down
Loading

0 comments on commit 64aabbf

Please sign in to comment.