Skip to content

Commit

Permalink
🛠️Refactor #27: [동네정보] 게시글 예외처리
Browse files Browse the repository at this point in the history
🛠️Refactor #27: [동네정보] 게시글 예외처리
  • Loading branch information
DDonghyeo authored Jul 28, 2023
2 parents eb8f73e + e949332 commit e4ceddc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;

import javax.validation.constraints.NotNull;
import java.util.List;

public class SharingDto {
Expand All @@ -20,12 +22,17 @@ public class SharingDto {
@AllArgsConstructor
@NoArgsConstructor
public static class Request {

@NotNull(message = "카테고리는 필수입니다.")
private int category;

@NotNull(message = "제목은 필수입니다.")
private String title;

@NotNull(message = "내용은 필수입니다.")
private String content;


private List<String> images;

public SharingBoard toEntity(User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public List<SharingDto.ListResponse> searchByKeyword(String keyword, int categor
if (sharingBoards.isEmpty()) {
throw new CustomException(ErrorCode.NO_CONTENT_FOUND);
}
log.info("board found" + sharingBoards.get(0).getId());
return getListResponses(sharingBoards);
}

Expand Down Expand Up @@ -118,7 +117,7 @@ public SharingDto.Response getBoard(long board_id) {
@Override
public void updateBoard(long board_id, SharingDto.Request req) {
SharingBoard board = sharingBoardRepository.findById(board_id).orElseThrow(
() -> new CustomException(ErrorCode.INVALID_VALUE));
() -> new CustomException(ErrorCode.NO_CONTENT_FOUND));

board.updateBoard(req);
sharingBoardRepository.save(board);
Expand All @@ -132,6 +131,15 @@ public void updateBoard(long board_id, SharingDto.Request req) {
*/
@Override
public void deleteBoard(long board_id) {
Optional<SharingBoard> sharingBoard = sharingBoardRepository.findById(board_id);
if (sharingBoard.isEmpty()) {
throw new CustomException(ErrorCode.NO_CONTENT_FOUND);
}

if (!Objects.equals(sharingBoard.get().getUser().getId(), user.getId())) {
throw new CustomException(ErrorCode.INVALID_AUTH_TOKEN);
}

sharingBoardRepository.deleteById(board_id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.umc.DongnaeFriend.domain.user.entity.User;
import lombok.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -21,12 +23,19 @@ public class DongnaeBoardDto {
@AllArgsConstructor
@NoArgsConstructor
public static class Request {
@NotNull(message = "카테고리는 필수입니다.")
private int category;

@NotNull(message = "제목은 필수입니다.")
private String title;
@NotNull(message = "내응은 필수입니다.")
private String content;
private List<String> images;

@NotNull(message = "장소는 필수입니다.")
private String place;

@NotNull(message = "자세한 장소는 필수입니다.")
private String placeLocation;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.umc.DongnaeFriend.domain.type.YesNo;
import com.umc.DongnaeFriend.domain.user.entity.User;
import com.umc.DongnaeFriend.domain.user.repository.UserRepository;
import com.umc.DongnaeFriend.global.exception.CustomException;
import com.umc.DongnaeFriend.global.exception.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -80,11 +82,15 @@ public List<DongnaeBoardDto.ListResponse> searchByKeyword(String keyword, int ca

List<DongnaeBoard> dongnaeBoardList = dongnaeBoardRepository.findByKeyword(keyword, categoryName, pageable);

if (dongnaeBoardList.isEmpty()) {
throw new CustomException(ErrorCode.NO_CONTENT_FOUND);
}

return getListResponses(dongnaeBoardList);
}

/*
* [동네정보] 게시글 목록 조회
* [동네정보] 게시글 목록 조회 DLETED
* @param sort
*/
@Override
Expand All @@ -106,12 +112,6 @@ public List<DongnaeBoardDto.ListResponse> searchAll(int sort) {
@Override
public void createBoard(DongnaeBoardDto.Request req) {
//TODO : User Mapping UserRepository 필요.

Dongnae dongnae = Dongnae.builder().id(1L).gu("서울구").dong("서울동").city("서울시").townName("무슨마을").build();
User user = User.builder().id(1L).age(Age.AGE10).email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).townCertCnt(10).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build();

dongnaeRepository.save(dongnae);
userRepository.save(user);
dongnaeBoardRepository.save(req.toEntity(user, dongnae));
}

Expand All @@ -126,7 +126,7 @@ public DongnaeBoardDto.Response getBoard(long board_id) {
//TODO : User 식별자 필요.
Optional<DongnaeBoard> board = dongnaeBoardRepository.findById(board_id);
if (board.isEmpty()) {
throw new RuntimeException();
throw new CustomException(ErrorCode.NO_CONTENT_FOUND);
}

//Get Images
Expand Down Expand Up @@ -165,28 +165,28 @@ public DongnaeBoardDto.Response getBoard(long board_id) {
* [동네정보] 게시글 수정
*/
@Override
public void updateBoard(long board_id, DongnaeBoardDto.Request request) throws AuthenticationException {
public void updateBoard(long board_id, DongnaeBoardDto.Request request) {
Optional<DongnaeBoard> board = dongnaeBoardRepository.findById(board_id);
if (board.isPresent()) {
//User Validaiton
if (!Objects.equals(board.get().getUser().getId(), user.getId())) {
throw new AuthenticationException();
throw new CustomException(ErrorCode.INVALID_AUTH_TOKEN);
}
board.get().updateBoard(request);
dongnaeBoardRepository.save(board.get());
} else {
throw new EntityNotFoundException();
throw new CustomException(ErrorCode.INVALID_VALUE);
}
}

@Override
public void deleteBoard(long board_id) throws AuthenticationException {
Optional<DongnaeBoard> board = dongnaeBoardRepository.findById(board_id);

if (board.isEmpty()) throw new EntityNotFoundException();
if (board.isEmpty()) throw new CustomException(ErrorCode.NO_CONTENT_FOUND);
//User Validation
if (!Objects.equals(board.get().getUser().getId(), user.getId())) {
throw new AuthenticationException();
throw new CustomException(ErrorCode.INVALID_AUTH_TOKEN);
} else {
dongnaeBoardRepository.deleteById(board_id);
}
Expand Down

0 comments on commit e4ceddc

Please sign in to comment.