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
23 changes: 23 additions & 0 deletions src/main/java/com/back/catchmate/domain/board/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,27 @@ public void updateBoard(Club cheerClub, Game game, CreateOrUpdateBoardRequest bo
this.club = cheerClub;
this.game = game;
}

public void deleteBoard() {
// Enroll 리스트 삭제
for (Enroll enroll : enrollList) {
enroll.delete();
}
enrollList.clear();

// Notification 리스트 삭제
for (Notification notification : notificationList) {
notification.delete();
}
notificationList.clear();

// BookMark 리스트 삭제
for (BookMark bookMark : bookMarkList) {
bookMark.delete();
}
bookMarkList.clear();

// 삭제 시간 기록
super.delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public BoardInfo createOrUpdateBoard(Long userId, Long boardId, CreateOrUpdateBo

// 작성자가 동일하지 않은 경우 예외 처리
if (user.isDifferentUserFrom(board.getUser())) {
throw new BaseException(ErrorCode.BOARD_BAD_REQUEST);
throw new BaseException(ErrorCode.BOARD_UPDATE_BAD_REQUEST);
}

// Board 업데이트
Expand Down Expand Up @@ -153,12 +153,18 @@ public BoardInfo getTempBoard(Long userId, Long boardId) {
@Override
@Transactional
public BoardDeleteInfo deleteBoard(Long userId, Long boardId) {
int updatedRows = boardRepository.softDeleteByUserIdAndBoardId(userId, boardId);
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

if (updatedRows == 0) {
throw new BaseException(ErrorCode.BOARD_NOT_FOUND);
Board board = boardRepository.findByIdAndDeletedAtIsNullAndIsCompleted(boardId)
.orElseThrow(() -> new BaseException(ErrorCode.BOARD_NOT_FOUND));


if (user.isDifferentUserFrom(board.getUser())) {
throw new BaseException(ErrorCode.BOARD_DELETE_BAD_REQUEST);
}

board.deleteBoard();
return boardConverter.toBoardDeleteInfo(boardId);
}

Expand All @@ -172,14 +178,14 @@ public BoardInfo updateLiftUpDate(Long userId, Long boardId) {
.orElseThrow(() -> new BaseException(ErrorCode.BOARD_NOT_FOUND));

if (user.isDifferentUserFrom(board.getUser())) {
throw new BaseException(ErrorCode.BOARD_BAD_REQUEST);
throw new BaseException(ErrorCode.BOARD_LIFT_UP_BAD_REQUEST);
}

// note: 3일 간격으로 수정할 수 있음.
if (board.getLiftUpDate().plusDays(3).isBefore(LocalDateTime.now())) {
board.setLiftUpDate(LocalDateTime.now());
} else {
throw new BaseException(ErrorCode.BOARD_NOT_ALLOWED_UPDATE_LIFTUPDATE);
throw new BaseException(ErrorCode.BOARD_NOT_ALLOWED_UPDATE_LIFT_UPDATE);
}

return boardConverter.toBoardInfo(board, board.getGame());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ public boolean isDifferentFromLoginUser(User user) {
public void respondToEnroll(AcceptStatus acceptStatus) {
this.acceptStatus = acceptStatus;
}

public void updateIsNew(boolean isNew) {
this.isNew = isNew;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
import java.util.Optional;

public interface EnrollRepository extends JpaRepository<Enroll, Long> {
Optional<Enroll> findByUserIdAndBoardId(Long userId, Long boardId);
Optional<Enroll> findByIdAndDeletedAtIsNull(Long enrollId);

Page<Enroll> findByUserId(Long userId, Pageable pageable);
Optional<Enroll> findByUserIdAndBoardIdAndDeletedAtIsNull(Long userId, Long boardId);

@Query("SELECT e FROM Enroll e WHERE e.board.user.id = :userId")
Page<Enroll> findByUserIdAndDeletedAtIsNull(Long userId, Pageable pageable);

@Query("SELECT e FROM Enroll e WHERE e.board.user.id = :userId AND e.deletedAt IS NULL")
Page<Enroll> findEnrollListByBoardWriter(@Param("userId") Long userId, Pageable pageable);

Page<Enroll> findByBoardId(Long boardId, Pageable pageable);
Page<Enroll> findByBoardIdAndDeletedAtIsNull(Long boardId, Pageable pageable);

@Query("SELECT COUNT(e) FROM Enroll e JOIN e.board b WHERE e.isNew = true AND b.user.id = :userId")
@Query("SELECT COUNT(e) FROM Enroll e JOIN e.board b WHERE e.isNew = true AND b.user.id = :userId AND e.deletedAt IS NULL")
int countNewEnrollListByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

import static com.back.catchmate.domain.notification.message.NotificationMessages.*;

Expand All @@ -46,13 +47,13 @@ public CreateEnrollInfo requestEnroll(CreateEnrollRequest request, Long boardId,
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

// 존재하는 게시글인지, 자신의 게시글인지 확인
Board board = boardRepository.findById(boardId)
Board board = boardRepository.findByIdAndDeletedAtIsNullAndIsCompleted(boardId)
.orElseThrow(() -> new BaseException(ErrorCode.BOARD_NOT_FOUND));
if (board.isWriterSameAsLoginUser(user)) {
throw new BaseException(ErrorCode.ENROLL_BAD_REQUEST);
}

enrollRepository.findByUserIdAndBoardId(user.getId(), board.getId())
enrollRepository.findByUserIdAndBoardIdAndDeletedAtIsNull(user.getId(), board.getId())
.ifPresent(enroll -> {
throw new BaseException(ErrorCode.ENROLL_ALREADY_EXIST);
});
Expand Down Expand Up @@ -80,15 +81,15 @@ public CancelEnrollInfo cancelEnroll(Long enrollId, Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

Enroll enroll = enrollRepository.findById(enrollId)
Enroll enroll = enrollRepository.findByIdAndDeletedAtIsNull(enrollId)
.orElseThrow(() -> new BaseException(ErrorCode.ENROLL_NOT_FOUND));

// 직관 신청한 사용자와 로그인한 사용자가 일치하는지 확인
if (enroll.isDifferentFromLoginUser(user)) {
throw new BaseException(ErrorCode.ENROLL_CANCEL_INVALID);
}

enrollRepository.delete(enroll);
enroll.delete();
return enrollConverter.toCancelEnrollInfo(enroll);
}

Expand All @@ -98,12 +99,12 @@ public PagedEnrollRequestInfo getRequestEnrollList(Long userId, Pageable pageabl
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

Page<Enroll> enrollList = enrollRepository.findByUserId(user.getId(), pageable);
Page<Enroll> enrollList = enrollRepository.findByUserIdAndDeletedAtIsNull(user.getId(), pageable);
return enrollConverter.toPagedEnrollRequestInfo(enrollList);
}

@Override
@Transactional(readOnly = true)
@Transactional
public PagedEnrollReceiveInfo getReceiveEnrollList(Long userId, Pageable pageable) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));
Expand All @@ -113,12 +114,12 @@ public PagedEnrollReceiveInfo getReceiveEnrollList(Long userId, Pageable pageabl
}

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

Board board = boardRepository.findById(boardId)
Board board = boardRepository.findByIdAndDeletedAtIsNullAndIsCompleted(boardId)
.orElseThrow(() -> new BaseException(ErrorCode.BOARD_NOT_FOUND));

// 게시글 작성자가 맞는지 확인
Expand All @@ -127,7 +128,13 @@ public PagedEnrollReceiveInfo getReceiveEnrollListByBoardId(Long userId, Long bo
}

// 게시글에 신청된 목록 조회
Page<Enroll> enrollList = enrollRepository.findByBoardId(boardId, pageable);
Page<Enroll> enrollList = enrollRepository.findByBoardIdAndDeletedAtIsNull(boardId, pageable);

if (enrollList.hasContent()) {
List<Enroll> enrollsToUpdate = enrollList.getContent();
enrollsToUpdate.forEach(enroll -> enroll.updateIsNew(false)); // 읽음 상태로 변경
}

return enrollConverter.toPagedEnrollReceiveInfo(enrollList);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Optional;

public interface NotificationRepository extends JpaRepository<Notification, Long> {
Page<Notification> findByUserId(Long userId, Pageable pageable);
Page<Notification> findByUserIdAndDeletedAtIsNull(Long userId, Pageable pageable);

Optional<Notification> findByIdAndUserId(Long notificationId, Long userId);
Optional<Notification> findByIdAndUserIdAndDeletedAtIsNull(Long notificationId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void createNotification(String title, String body, String senderProfileIm
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

Board board = boardRepository.findById(boardId)
Board board = boardRepository.findByIdAndDeletedAtIsNullAndIsCompleted(boardId)
.orElseThrow(() -> new BaseException(ErrorCode.BOARD_NOT_FOUND));

Notification notification = notificationConverter.toEntity(user, board, senderProfileImageUrl, title, body);
Expand All @@ -45,7 +45,7 @@ public PagedNotificationInfo getNotificationList(Long userId, Pageable pageable)
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

Page<Notification> notificationList = notificationRepository.findByUserId(user.getId(), pageable);
Page<Notification> notificationList = notificationRepository.findByUserIdAndDeletedAtIsNull(user.getId(), pageable);
return notificationConverter.toPagedNotificationInfo(notificationList);
}

Expand All @@ -55,7 +55,7 @@ public NotificationInfo getNotification(Long userId, Long notificationId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

Notification notification = notificationRepository.findByIdAndUserId(notificationId, user.getId())
Notification notification = notificationRepository.findByIdAndUserIdAndDeletedAtIsNull(notificationId, user.getId())
.orElseThrow(() -> new BaseException(ErrorCode.NOTIFICATION_NOT_FOUND));

// 읽지 않은 알림일 경우, 읽음으로 표시
Expand All @@ -72,10 +72,10 @@ public StateResponse deleteNotification(Long userId, Long notificationId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

Notification notification = notificationRepository.findByIdAndUserId(notificationId, user.getId())
Notification notification = notificationRepository.findByIdAndUserIdAndDeletedAtIsNull(notificationId, user.getId())
.orElseThrow(() -> new BaseException(ErrorCode.NOTIFICATION_NOT_FOUND));

notificationRepository.delete(notification);
notification.delete();
return new StateResponse(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Component
public class AuthConverter {

public AuthInfo toLoginInfo(String accessToken, String refreshToken, Boolean isFirstLogin) {
public AuthInfo toAuthInfo(String accessToken, String refreshToken, Boolean isFirstLogin) {
return AuthInfo.builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public AuthInfo login(AuthRequest.LoginRequest loginRequest) {
if (findUserOptional.isEmpty()) {
// 사용자가 없으면 최초 회원가입 여부를 true 반환
isFirstLogin = true;
authInfo = authConverter.toLoginInfo(null, null, isFirstLogin);
authInfo = authConverter.toAuthInfo(null, null, isFirstLogin);
} else {
// 회원가입된 사용자가 있으면 AccessToken과 RefreshToken 반환
User user = findUserOptional.get();
Expand All @@ -60,7 +60,7 @@ public AuthInfo login(AuthRequest.LoginRequest loginRequest) {

// RefreshToken을 Redis에 저장
refreshTokenRepository.save(RefreshToken.of(refreshToken, userId));
authInfo = authConverter.toLoginInfo(accessToken, refreshToken, isFirstLogin);
authInfo = authConverter.toAuthInfo(accessToken, refreshToken, isFirstLogin);
}

return authInfo;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/back/catchmate/global/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public enum ErrorCode {
TEMP_BOARD_NOT_FOUND(HttpStatus.NOT_FOUND, "임시 저장된 글이 존재하지 않습니다."),
TEMP_BOARD_BAD_REQUEST(HttpStatus.NOT_FOUND, "임시 저장된 글을 불러올 권한이 없습니다."),
BOARD_DELETED(HttpStatus.NOT_FOUND, "삭제된 게시글이거나 잘못된 접근입니다."),
BOARD_BAD_REQUEST(HttpStatus.BAD_REQUEST, "자신의 게시글만 수정할 수 있습니다."),
BOARD_NOT_ALLOWED_UPDATE_LIFTUPDATE(HttpStatus.BAD_REQUEST, "마지막으로 끌어올리기 한 지 3일이 지나야 업데이트 가능합니다."),
BOARD_UPDATE_BAD_REQUEST(HttpStatus.BAD_REQUEST, "자신의 게시글만 수정할 수 있습니다."),
BOARD_DELETE_BAD_REQUEST(HttpStatus.BAD_REQUEST, "자신의 게시글만 삭제할 수 있습니다."),
BOARD_LIFT_UP_BAD_REQUEST(HttpStatus.BAD_REQUEST, "자신의 게시글만 삭제할 수 있습니다."),
BOARD_NOT_ALLOWED_UPDATE_LIFT_UPDATE(HttpStatus.BAD_REQUEST, "마지막으로 끌어올리기 한 지 3일이 지나야 업데이트 가능합니다."),
INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 액세스 토큰입니다."),
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 리프레시 토큰입니다."),
ALREADY_BOOKMARK(HttpStatus.BAD_REQUEST, "이미 찜한 게시글입니다."),
Expand Down
Loading