Skip to content

Commit

Permalink
feat: BusinessException 예외처리 등록
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-so committed Mar 27, 2024
1 parent c91d6c6 commit 62a301b
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/bookmark")
public class BookMarkController {
public class BookmarkController {

private final BookmarkService bookmarkService;

Expand Down
55 changes: 0 additions & 55 deletions src/main/java/com/eodya/api/bookmark/domain/BookMark.java

This file was deleted.

16 changes: 8 additions & 8 deletions src/main/java/com/eodya/api/bookmark/domain/Bookmark.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.eodya.api.bookmark.domain;

import com.eodya.api.bookmark.util.BookMarkStatusConverter;
import com.eodya.api.bookmark.util.BookmarkStatusConverter;
import com.eodya.api.common.entity.BaseEntity;
import com.eodya.api.place.domain.Place;
import com.eodya.api.users.domain.User;
Expand All @@ -11,23 +11,23 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import static com.eodya.api.bookmark.domain.BookMarkStatus.*;
import static com.eodya.api.bookmark.domain.BookmarkStatus.*;

@Getter
@Entity
@Table(name = "bookmark")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BookMark extends BaseEntity {
public class Bookmark extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "bookmark_id")
private Long id;

@NotNull
@Convert(converter = BookMarkStatusConverter.class)
@Convert(converter = BookmarkStatusConverter.class)
@Column(length = 20)
private BookMarkStatus status = FALSE;
private BookmarkStatus status = FALSE;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
Expand All @@ -39,12 +39,12 @@ public class BookMark extends BaseEntity {

public void setUser(User user) {
this.user = user;
this.user.getBookMarks().add(this);
this.user.getBookmarks().add(this);
}

@Builder
private BookMark(
BookMarkStatus status,
private Bookmark(
BookmarkStatus status,
User user,
Place place
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.eodya.api.bookmark.domain;

import com.eodya.api.bookmark.exception.BookMarkException;
import com.eodya.api.bookmark.exception.BookmarkException;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.eodya.api.bookmark.exception.BookMarkExceptionCode.*;
import static com.eodya.api.bookmark.exception.BookmarkExceptionCode.*;

@Getter
@RequiredArgsConstructor
public enum BookMarkStatus {
public enum BookmarkStatus {

TRUE("생성"),
FALSE("삭제"),
Expand All @@ -26,14 +26,14 @@ public enum BookMarkStatus {
private final String description;

@JsonCreator
public static BookMarkStatus from(String description) throws BookMarkException {
public static BookmarkStatus from(String description) throws BookmarkException {
if (statusMap.containsKey(description)) {
return statusMap.get(description);
}
throw new BookMarkException(BOOKMARK_STATUS_NOT_FOUND, description);
throw new BookmarkException(BOOKMARK_STATUS_NOT_FOUND, description);
}

private static final Map<String, BookMarkStatus> statusMap =
Collections.unmodifiableMap(Arrays.stream(values())
.collect(Collectors.toMap(BookMarkStatus::getDescription, Function.identity())));
private static final Map<String, BookmarkStatus> statusMap =
Collections.unmodifiableMap(Stream.of(values())
.collect(Collectors.toMap(BookmarkStatus::getDescription, Function.identity())));
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.eodya.api.bookmark.dto.request;

import com.eodya.api.bookmark.domain.BookMarkStatus;
import com.eodya.api.bookmark.domain.BookmarkStatus;
import lombok.*;

@Getter
Expand All @@ -9,5 +9,5 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class BookMarkChangeStatusRequest {

private BookMarkStatus status;
private BookmarkStatus status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.eodya.api.common.exception.BusinessException;
import com.eodya.api.common.exception.ExceptionCode;

public class BookMarkException extends BusinessException {
public class BookmarkException extends BusinessException {

public BookMarkException(ExceptionCode exceptionCode, Object... rejectedValues) {
public BookmarkException(ExceptionCode exceptionCode, Object... rejectedValues) {
super(exceptionCode, rejectedValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@Getter
@RequiredArgsConstructor
public enum BookMarkExceptionCode implements ExceptionCode {
public enum BookmarkExceptionCode implements ExceptionCode {

BOOKMARK_STATUS_NOT_FOUND(HttpStatus.NOT_FOUND, "BOO-001", "북마크 상태를 찾을 수 없음"),
;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.eodya.api.bookmark.repository;

import com.eodya.api.bookmark.domain.Bookmark;
import com.eodya.api.bookmark.exception.BookmarkException;
import org.springframework.data.jpa.repository.JpaRepository;

import static com.eodya.api.bookmark.exception.BookmarkExceptionCode.*;

public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {

default Bookmark getBookMarkById(Long bookMarkId) {
return findById(bookMarkId).orElseThrow(() -> new BookmarkException(BOOKMARK_STATUS_NOT_FOUND, bookMarkId));
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.eodya.api.bookmark.service;

import com.eodya.api.bookmark.domain.BookMark;
import com.eodya.api.bookmark.domain.Bookmark;
import com.eodya.api.bookmark.dto.request.BookMarkChangeStatusRequest;
import com.eodya.api.bookmark.repository.BookMarkRepository;
import com.eodya.api.bookmark.repository.BookmarkRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class BookmarkService {

private final BookMarkRepository bookmarkRepository;
private final BookmarkRepository bookmarkRepository;

public void updateBookmarkStatus(
Long placeId,
BookMarkChangeStatusRequest changeStatusRequest
) {
BookMark bookMark = bookmarkRepository.getBookMarkById(placeId);
Bookmark bookMark = bookmarkRepository.getBookMarkById(placeId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.eodya.api.bookmark.util;

import com.eodya.api.bookmark.domain.BookmarkStatus;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

@Converter
public class BookmarkStatusConverter implements AttributeConverter<BookmarkStatus, String> {

@Override
public String convertToDatabaseColumn(BookmarkStatus bookMarkStatus) {
return bookMarkStatus.getDescription();
}

@Override
public BookmarkStatus convertToEntityAttribute(String status) {
return BookmarkStatus.from(status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Getter;

@Getter
public class BusinessException extends RuntimeException {
public abstract class BusinessException extends RuntimeException {

private final ExceptionCode exceptionCode;
private final Object[] rejectedValues;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eodya.api.common.exception;

import com.eodya.api.common.dto.ExceptionResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand Down Expand Up @@ -33,7 +34,16 @@ public ResponseEntity<ExceptionResponse> handleHttpRequestMethodNotSupportedExce

@ExceptionHandler(Exception.class)
public ResponseEntity<ExceptionResponse> handleException(Exception e) {

return ResponseEntity.status(COMMON_INTERNAL_SERVER_ERROR.getStatus())
.body(ExceptionResponse.from(COMMON_INTERNAL_SERVER_ERROR.getCode()));
}

@ExceptionHandler(BusinessException.class)
public ResponseEntity<ExceptionResponse> handleBusinessException(BusinessException e) {
ExceptionCode exceptionCode = e.getExceptionCode();

return ResponseEntity.status(exceptionCode.getStatus())
.body(ExceptionResponse.from(exceptionCode.getCode()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.eodya.api.recommendation.exception.RecommendationExceptionCode.*;

Expand All @@ -34,6 +35,6 @@ public static RecommendationStatus from(String description) throws Recommendatio
}

private static final Map<String, RecommendationStatus> statusMap =
Collections.unmodifiableMap(Arrays.stream(values())
Collections.unmodifiableMap(Stream.of(values())
.collect(Collectors.toMap(RecommendationStatus::getDescription, Function.identity())));
}
4 changes: 2 additions & 2 deletions src/main/java/com/eodya/api/users/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.eodya.api.users.domain;

import com.eodya.api.bookmark.domain.BookMark;
import com.eodya.api.bookmark.domain.Bookmark;
import com.eodya.api.common.entity.BaseEntity;
import com.eodya.api.recommendation.domain.Recommendation;
import com.eodya.api.review.domain.Review;
Expand Down Expand Up @@ -55,7 +55,7 @@ public class User extends BaseEntity {
private List<Review> reviews = new ArrayList<>();

@OneToMany(mappedBy = "user")
private List<BookMark> bookMarks = new ArrayList<>();
private List<Bookmark> bookmarks = new ArrayList<>();

@OneToMany(mappedBy = "user")
private List<Recommendation> recommendations = new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/eodya/api/domain/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void createUser_bookmark_review_recommend_size_test() {
User user = UserFixture.userBuild(userName, OAuthId, OAuthProvider);

//when & then
assertEquals(user.getBookMarks().size(), 0);
assertEquals(user.getBookmarks().size(), 0);
assertEquals(user.getRecommendations().size(), 0);
assertEquals(user.getRecommendations().size(), 0);
}
Expand Down

0 comments on commit 62a301b

Please sign in to comment.