Skip to content
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,9 +1,7 @@
package com.back.catchmate.domain.board.controller;

import com.back.catchmate.domain.board.dto.BoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest.CreateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest.UpdateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardResponse;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardDeleteInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.PagedBoardInfo;
Expand All @@ -30,7 +28,6 @@

import java.time.LocalDate;


@Tag(name = "게시글 관련 API")
@RestController
@RequestMapping("/board")
Expand Down Expand Up @@ -80,6 +77,14 @@ public StateResponse addBookMark(@JwtValidation Long userId,
return bookMarkService.addBookMark(userId, boardId);
}

@GetMapping("/bookmark")
@Operation(summary = "찜한 게시글 조회 API", description = "사용자가 찜한 게시글을 조회하는 API입니다.")
public PagedBoardInfo getBookMarkBoardList(@JwtValidation Long userId,
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC)
@Parameter(hidden = true) Pageable pageable) {
return bookMarkService.getBookMarkBoardList(userId, pageable);
}

@DeleteMapping("/bookmark/{boardId}")
@Operation(summary = "원하는 게시글을의 찜을 삭제하는 API", description = "원하는 게시글을의 찜을 삭제하는 API 입니다.")
public StateResponse removeBookMark(@JwtValidation Long userId,
Expand All @@ -98,7 +103,7 @@ public BoardInfo updateBoard(@JwtValidation Long userId,
@DeleteMapping("/{boardId}")
@Operation(summary = "게시글 삭제 API", description = "게시글을 삭제합니다.")
public BoardDeleteInfo deleteBoard(@JwtValidation Long userId,
@PathVariable Long boardId) {
@PathVariable Long boardId) {
return boardService.deleteBoard(userId, boardId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.back.catchmate.domain.board.dto.BoardRequest.*;
import com.back.catchmate.domain.board.dto.BoardResponse.*;
import com.back.catchmate.domain.board.entity.Board;
import com.back.catchmate.domain.board.entity.BookMark;
import com.back.catchmate.domain.club.entity.Club;
import com.back.catchmate.domain.game.converter.GameConverter;
import com.back.catchmate.domain.game.dto.GameResponse.GameInfo;
import com.back.catchmate.domain.game.entity.Game;
import com.back.catchmate.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
Expand All @@ -33,7 +35,7 @@ public Board toEntity(User user, Game game, Club cheerClub, CreateBoardRequest b
.build();
}

public PagedBoardInfo toPagedBoardInfo(Page<Board> boardList) {
public PagedBoardInfo toPagedBoardInfoFromBoardList(Page<Board> boardList) {
List<BoardInfo> boardInfoList = boardList.stream()
.map(board -> toBoardInfo(board, board.getGame()))
.toList();
Expand Down Expand Up @@ -68,4 +70,13 @@ public BoardDeleteInfo toBoardDeleteInfo(Long boardId) {
.deletedAt(LocalDateTime.now())
.build();
}

public PagedBoardInfo toPagedBoardInfoFromBookMarkList(Page<BookMark> bookMarkList) {
List<Board> boards = bookMarkList.stream()
.map(BookMark::getBoard)
.toList();

Page<Board> boardPage = new PageImpl<>(boards, bookMarkList.getPageable(), bookMarkList.getTotalElements());
return toPagedBoardInfoFromBoardList(boardPage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class Board extends BaseTimeEntity {
private Boolean isCompleted = false;

public boolean isWriterSameAsLoginUser(User user) {
return this.user.equals(user);
return this.user.getId().equals(user.getId());
}

public void updateBoard(Club cheerClub, Game game, UpdateBoardRequest boardRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public interface BoardRepository extends JpaRepository<Board, Long>, BoardReposi
@Query("UPDATE Board b SET b.deletedAt = CURRENT_TIMESTAMP WHERE b.user.id = :userId AND b.id = :boardId")
int softDeleteByUserIdAndBoardId(@Param("userId") Long userId, @Param("boardId") Long boardId);

@Query("SELECT b FROM Board b WHERE b.id = :boardId AND b.deletedAt IS NULL")
@Query("SELECT b FROM Board b WHERE b.id = :boardId AND b.deletedAt IS NULL AND b.isCompleted = true")
Optional<Board> findByIdAndDeletedAtIsNull(Long boardId);

Page<Board> findAllByUserIdAndDeletedAtIsNull(Long userId, Pageable pageable);
Page<Board> findAllByUserIdAndDeletedAtIsNullAndIsCompletedIsTrue(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public Page<Board> findFilteredBoards(LocalDate gameDate, Integer maxPerson, Lon
// 삭제되지 않은 게시글만 조회
builder.and(board.deletedAt.isNull());

// 저장된 게시글만 조회 (임시 저장 X)
builder.and(board.isCompleted.isTrue());

// 최대 인원수 필터
if (maxPerson != null) {
builder.and(board.maxPerson.eq(maxPerson));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.back.catchmate.domain.board.entity.Board;
import com.back.catchmate.domain.board.entity.BookMark;
import com.back.catchmate.domain.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
Expand All @@ -11,4 +13,6 @@ public interface BookMarkRepository extends JpaRepository<BookMark, Long> {
boolean existsByUserAndBoard(User user, Board board);

Optional<BookMark> findByUserIdAndBoardId(Long userId, Long boardId);

Page<BookMark> findAllByUserIdAndDeletedAtIsNull(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.back.catchmate.domain.board.service;

import com.back.catchmate.domain.board.dto.BoardRequest.CreateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest.*;
import com.back.catchmate.domain.board.dto.BoardResponse;
import com.back.catchmate.domain.board.dto.BoardRequest.UpdateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardDeleteInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.PagedBoardInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public PagedBoardInfo getBoardList(Long userId, LocalDate gameStartDate, Integer
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

Page<Board> boardList = boardRepository.findFilteredBoards(gameStartDate, maxPerson, preferredTeamId, pageable);
return boardConverter.toPagedBoardInfo(boardList);
return boardConverter.toPagedBoardInfoFromBoardList(boardList);
}

@Override
Expand All @@ -109,14 +109,14 @@ public PagedBoardInfo getBoardListByUserId(Long loginUserId, Long userId, Pageab
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

Page<Board> boardList = boardRepository.findAllByUserIdAndDeletedAtIsNull(user.getId(), pageable);
return boardConverter.toPagedBoardInfo(boardList);
Page<Board> boardList = boardRepository.findAllByUserIdAndDeletedAtIsNullAndIsCompletedIsTrue(user.getId(), pageable);
return boardConverter.toPagedBoardInfoFromBoardList(boardList);
}

@Override
@Transactional
public BoardInfo updateBoard(Long userId, Long boardId, UpdateBoardRequest request) {
Board board = this.boardRepository.findById(boardId)
Board board = this.boardRepository.findByIdAndDeletedAtIsNull(boardId)
.orElseThrow(() -> new BaseException(ErrorCode.BOARD_NOT_FOUND));

User user = this.userRepository.findById(userId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.back.catchmate.domain.board.service;

import com.back.catchmate.global.dto.StateResponse;
import org.springframework.data.domain.Pageable;

import static com.back.catchmate.domain.board.dto.BoardResponse.*;

public interface BookMarkService {
StateResponse addBookMark(Long userId, Long boardId);

PagedBoardInfo getBookMarkBoardList(Long userId, Pageable pageable);

StateResponse removeBookMark(Long userId, Long boardId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.back.catchmate.domain.board.service;

import com.back.catchmate.domain.board.converter.BoardConverter;
import com.back.catchmate.domain.board.converter.BookMarkConverter;
import com.back.catchmate.domain.board.dto.BoardResponse.PagedBoardInfo;
import com.back.catchmate.domain.board.entity.Board;
import com.back.catchmate.domain.board.entity.BookMark;
import com.back.catchmate.domain.board.repository.BoardRepository;
Expand All @@ -11,6 +13,8 @@
import com.back.catchmate.global.error.ErrorCode;
import com.back.catchmate.global.error.exception.BaseException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -21,6 +25,7 @@ public class BookMarkServiceImpl implements BookMarkService {
private final UserRepository userRepository;
private final BoardRepository boardRepository;
private final BookMarkConverter bookMarkConverter;
private final BoardConverter boardConverter;

@Override
@Transactional
Expand All @@ -31,15 +36,30 @@ public StateResponse addBookMark(Long userId, Long boardId) {
Board board = boardRepository.findById(boardId)
.orElseThrow(() -> new BaseException(ErrorCode.BOARD_NOT_FOUND));

// 본인의 게시글인지 확인
if (user.isDifferentUserFrom(board.getUser())) {
throw new BaseException(ErrorCode.ALREADY_BOOKMARK);
}

if (bookMarkRepository.existsByUserAndBoard(user, board)) {
throw new BaseException(ErrorCode.USER_NOT_FOUND);
throw new BaseException(ErrorCode.ALREADY_BOOKMARK);
}

BookMark bookMark = bookMarkConverter.toEntity(user, board);
bookMarkRepository.save(bookMark);
return new StateResponse(true);
}

@Override
@Transactional(readOnly = true)
public PagedBoardInfo getBookMarkBoardList(Long userId, Pageable pageable) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

Page<BookMark> bookMarkList = bookMarkRepository.findAllByUserIdAndDeletedAtIsNull(user.getId(), pageable);
return boardConverter.toPagedBoardInfoFromBookMarkList(bookMarkList);
}

@Override
@Transactional
public StateResponse removeBookMark(Long userId, Long boardId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public enum ErrorCode {
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 리프레시 토큰입니다."),
ALREADY_BOOKMARK(HttpStatus.BAD_REQUEST, "이미 찜한 게시글입니다."),
BOOKMARK_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 찜입니다."),
BOOKMARK_BAD_REQUEST(HttpStatus.BAD_REQUEST, "본인 게시글은 찜할 수 없습니다."),

// 알림
NOTIFICATION_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 알림입니다."),
Expand Down
Loading