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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ out/
/eat-ssu.tar

### env file ###
.env
.env

/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
String requestURI = httpRequest.getRequestURI();

if (isWhiteListed(requestURI)) {
String token = resolveToken(httpRequest);
if (token != null && jwtTokenProvider.validateToken(token)) {
Authentication authentication = jwtTokenProvider.getAuthentication(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
Comment on lines +48 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

화이트리스트에 포함된 URL의 경우, 인증이 선택적이어야 합니다. 즉, 유효한 토큰이 있으면 인증된 사용자로 처리하고, 토큰이 없거나 유효하지 않으면 비로그인 사용자로 처리하여 요청을 계속 진행해야 합니다.
현재 코드는 jwtTokenProvider.validateToken(token)true를 반환하더라도 jwtTokenProvider.getAuthentication(token)에서 예외(예: JsonProcessingException)가 발생할 수 있습니다. 이 경우, 예외가 처리되지 않아 서버 오류(500)가 발생하여 비로그인 상태로도 접근 가능해야 할 화이트리스트 URL에 접근하지 못하는 문제가 생길 수 있습니다.
선택적 인증 로직을 try-catch 블록으로 감싸서 예외 발생 시에도 요청이 중단되지 않도록 하는 것이 안전합니다.

            String token = resolveToken(httpRequest);
            if (token != null && jwtTokenProvider.validateToken(token)) {
                try {
                    Authentication authentication = jwtTokenProvider.getAuthentication(token);
                    SecurityContextHolder.getContext().setAuthentication(authentication);
                } catch (Exception e) {
                    log.warn("JWT 토큰 인증 중 오류가 발생했으나, 화이트리스트 경로이므로 비로그인 상태로 계속 진행합니다. URI: {}", requestURI, e);
                }
            }

chain.doFilter(request, response);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,6 @@ public Double getMainRatingAverage(Long mealId) {
.fetchOne();
}

public Double getTasteRatingAverage(Long mealId) {
List<Long> menuIds = mealMenuQueryRepository.getMenuIds(mealId);
return queryFactory
.select(review.ratings.tasteRating.avg())
.from(review)
.join(review.menu, menu)
.where(
menuIdIn(menuIds)
)
.fetchOne();
}

public Double getAmountRatingAverage(Long mealId) {
List<Long> menuIds = mealMenuQueryRepository.getMenuIds(mealId);
return queryFactory
.select(review.ratings.amountRating.avg())
.from(review)
.join(review.menu, menu)
.where(
menuIdIn(menuIds)
)
.fetchOne();
}

private BooleanExpression menuIdIn(List<Long> menuIds) {
return menuIds != null && !menuIds.isEmpty() ? menu.id.in(menuIds) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,7 @@ public Double getMainRatingAverage(Long menuId) {
.fetchOne();
}

public Double getTasteRatingAverage(Long menuId) {
return queryFactory
.select(review.ratings.tasteRating.avg())
.from(review)
.join(review.menu, menu)
.where(
menuIdEq(menuId)
)
.fetchOne();
}

public Double getAmountRatingAverage(Long menuId) {
return queryFactory
.select(review.ratings.amountRating.avg())
.from(review)
.join(review.menu, menu)
.where(
menuIdEq(menuId)
)
.fetchOne();
}

private BooleanExpression menuIdEq(Long menuId) {
return menu.id.eq(menuId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public static PartnershipInfo fromEntity(Partnership partnership,
.description(partnership.getDescription())
.startDate(partnership.getStartDate())
.endDate(partnership.getEndDate())
.collegeName(partnership.getPartnershipCollege() == null ? null : partnership.getPartnershipCollege()
.getName())
.departmentName(partnership.getPartnershipDepartment() == null ? null : partnership.getPartnershipDepartment()
.getName())
.collegeName(partnership.getPartnershipCollege() == null && partnership.getPartnershipDepartment() == null
? "총학"
: (partnership.getPartnershipCollege() != null ? partnership.getPartnershipCollege().getName() : null))
Comment on lines +30 to +32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

collegeName을 결정하는 로직이 중첩된 삼항 연산자로 구현되어 있어 가독성이 떨어집니다. 코드를 더 명확하게 만들기 위해 이 로직을 별도의 private static 메서드로 추출하는 것을 고려해 보세요.

예를 들어, PartnershipInfo 클래스 내에 다음과 같은 헬퍼 메서드를 추가할 수 있습니다.

private static String getCollegeName(Partnership partnership) {
    if (partnership.getPartnershipCollege() == null && partnership.getPartnershipDepartment() == null) {
        return "총학";
    }
    if (partnership.getPartnershipCollege() != null) {
        return partnership.getPartnershipCollege().getName();
    }
    return null;
}

그리고 fromEntity 메서드에서는 다음과 같이 호출합니다.

.collegeName(getCollegeName(partnership))

.departmentName(partnership.getPartnershipDepartment() != null ? partnership.getPartnershipDepartment().getName() : null)
.likeCount(restaurant.getLikes() != null ? restaurant.getLikes().size() : 0)
.isLiked(isLiked)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public enum RestaurantType {
RESTAURANT("음식점"),
CAFE("카페"),
BEER("주점");
PUB("주점");

private final String type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@

public interface PartnershipRepository extends JpaRepository<Partnership, Long> {
@Query("""
select distinct pr
from PartnershipRestaurant pr
join fetch pr.partnerships p
left join fetch p.partnershipCollege pc
left join fetch p.partnershipDepartment pd
where
(pc = :college
or pd = :department
or (pc is not null and pc.name = '총학'))
and p.startDate <= current_date
and (p.endDate is null or p.endDate >= current_date)
""")
select distinct pr
from PartnershipRestaurant pr
join fetch pr.partnerships p
left join fetch p.partnershipCollege pc
left join fetch p.partnershipDepartment pd
where
(
pc = :college
or pd = :department
or (pc is null and pd is null)
)
and p.startDate <= current_date
and (p.endDate is null or p.endDate >= current_date)
""")
List<PartnershipRestaurant> findRestaurantsWithMyPartnerships(
@Param("college") College college,
@Param("department") Department department
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ public RatingAverages mealAverageRatings(Meal meal) {
long totalReviewCount = mealTotalReviewCount(meal);

if (totalReviewCount == 0)
return new RatingAverages(null, null, null);
return new RatingAverages(null);

return RatingAverages.builder()
.mainRating(averageRating(mealTotalMainRating(meal), totalReviewCount))
.amountRating(averageRating(mealTotalAmountRating(meal), totalReviewCount))
.tasteRating(averageRating(mealTotalTasteRating(meal), totalReviewCount))
.build();
}

Expand All @@ -48,13 +46,11 @@ public RatingAverages menuAverageRatings(Menu menu) {
int totalReviewCount = menu.getTotalReviewCount();

if (totalReviewCount == 0) {
return new RatingAverages(null, null, null);
return new RatingAverages(null);
}

return RatingAverages.builder()
.mainRating(averageRating(menuTotalMainRating(menu), totalReviewCount))
.amountRating(averageRating(menuTotalAmountRating(menu), totalReviewCount))
.tasteRating(averageRating(menuTotalTasteRating(menu), totalReviewCount))
.build();
}

Expand Down Expand Up @@ -101,35 +97,11 @@ public Integer mealTotalMainRating(Meal meal) {
.reduce(null, this::sum);
}

// 식단 양 평점 총합
public Integer mealTotalAmountRating(Meal meal) {
return meal.getMealMenus().stream()
.map(MealMenu::getMenu)
.map(menu -> menu.getReviews().getTotalAmountRating())
.reduce(null, this::sum);
}

// 식단 맛 평점 총합
public Integer mealTotalTasteRating(Meal meal) {
return meal.getMealMenus().stream()
.map(MealMenu::getMenu)
.map(menu -> menu.getReviews().getTotalTasteRating())
.reduce(null, this::sum);
}

// 메뉴 메인 평점 총합
public Integer menuTotalMainRating(Menu menu) {
return menu.getReviews().getTotalMainRating();
}

// 메뉴 양 평점 총합
public Integer menuTotalAmountRating(Menu menu) {
return menu.getReviews().getTotalAmountRating();
}

// 메뉴 맛 평점 총합
public Integer menuTotalTasteRating(Menu menu) {
return menu.getReviews().getTotalTasteRating();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@ public RatingAverages mealAverageRatings(Meal meal) {
long totalReviewCount = mealTotalReviewCount(meal);

if (totalReviewCount == 0)
return new RatingAverages(null, null, null);
return new RatingAverages(null);

Collection<RatingsDto> mealRatings = reviewRepository.findByMenu_MealMenus_Meal(meal, RatingsDto.class);

Integer totalMainRating = mealRatings.stream().mapToInt(RatingsDto::getMainRating).sum();
Integer totalTasteRating = mealRatings.stream().mapToInt(RatingsDto::getTasteRating).sum();
Integer totalAmountRating = mealRatings.stream().mapToInt(RatingsDto::getAmountRating).sum();

return RatingAverages.builder()
.mainRating(averageRating(totalMainRating, totalReviewCount))
.tasteRating(averageRating(totalTasteRating, totalReviewCount))
.amountRating(averageRating(totalAmountRating, totalReviewCount))
.build();
}

Expand Down
14 changes: 3 additions & 11 deletions src/main/java/ssu/eatssu/domain/rating/entity/Ratings.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@ public class Ratings {

private Integer mainRating;

private Integer amountRating;

private Integer tasteRating;

private Ratings(Integer mainRating, Integer amountRating, Integer tasteRating) {
private Ratings(Integer mainRating) {
Assert.isTrue(mainRating >= 0 && mainRating <= 5, "mainRating must be between 0 and 5");
Assert.isTrue(amountRating >= 0 && amountRating <= 5, "amountRating must be between 0 and 5");
Assert.isTrue(tasteRating >= 0 && tasteRating <= 5, "tasteRating must be between 0 and 5");
this.mainRating = mainRating;
this.amountRating = amountRating;
this.tasteRating = tasteRating;
}

public static Ratings of(Integer mainRating, Integer amountRating, Integer tasteRating) {
return new Ratings(mainRating, amountRating, tasteRating);
public static Ratings of(Integer mainRating) {
return new Ratings(mainRating);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ public class CreateMenuReviewRequest {
private Long menuId;
@Schema(description = "평점-메인", example = "4")
private Integer mainRating;

@Schema(description = "평점-양", example = "4")
private Integer amountRating;

@Schema(description = "평점-맛", example = "4")
private Integer tasteRating;

@Max(150)
@Schema(description = "한줄평", example = "맛있어용")
private String content;
Expand All @@ -36,19 +29,8 @@ public class CreateMenuReviewRequest {
private String imageUrl;
private MenuLikeRequest menuLike;

public CreateMenuReviewRequest(int mainRating, int amountRating, int tasteRating, String content) {
Assert.isTrue(mainRating >= 1 && mainRating <= 5, "평점은 1에서 5 사이 여야 합니다.");
Assert.isTrue(amountRating >= 1 && amountRating <= 5, "평점은 1에서 5 사이 여야 합니다.");
Assert.isTrue(tasteRating >= 1 && tasteRating <= 5, "평점은 1에서 5 사이 여야 합니다.");
Assert.notNull(content, "리뷰는 null이 될 수 없습니다.");
this.mainRating = mainRating;
this.amountRating = amountRating;
this.tasteRating = tasteRating;
this.content = content;
}

public Review toReviewEntity(User user, Menu menu) {
Ratings ratings = Ratings.of(this.mainRating, this.amountRating, this.tasteRating);
Ratings ratings = Ratings.of(this.mainRating);
return Review.builder()
.user(user)
.content(this.content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public static MealReviewsResponse of(Long totalReviewCount, List<String> menuNam
return MealReviewsResponse.builder()
.menuNames(menuNames)
.mainRating(ratingAverages.mainRating())
.amountRating(ratingAverages.amountRating())
.tasteRating(ratingAverages.tasteRating())
.totalReviewCount(totalReviewCount)
.reviewRatingCount(reviewRatingCount)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
public class MenuLikeRequest {
@Schema(description = "메뉴 식별자", example = "123")
private Long menuId;
@Schema(description = "좋아요 or 싫어요", example = "좋아요 : true or 싫어요 : false")
@Schema(description = "좋아요 선택", example = "좋아요 : true (기본값은 false)")
private Boolean isLike;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public static MenuReviewResponse of(Menu menu,
.menuName(menu.getName())
.totalReviewCount(menu.getTotalReviewCount())
.mainRating(ratingAverages.mainRating())
.amountRating(ratingAverages.amountRating())
.tasteRating(ratingAverages.tasteRating())
.reviewRatingCount(reviewRatingCount)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.Builder;

public record RatingAverages(Double mainRating, Double amountRating, Double tasteRating) {
public record RatingAverages(Double mainRating) {

@Builder
public RatingAverages {
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/ssu/eatssu/domain/review/dto/RatingsDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,5 @@ public Integer getMainRating() {
return ratings.getMainRating();
}

public Integer getTasteRating() {
return ratings.getTasteRating();
}

public Integer getAmountRating() {
return ratings.getAmountRating();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@ public class ReviewCreateRequest {
@Schema(description = "한줄평", example = "맛있어용")
private String content;

public ReviewCreateRequest(int mainRating, int amountRating, int tasteRating, String content) {
public ReviewCreateRequest(int mainRating, String content) {
Assert.isTrue(mainRating >= 1 && mainRating <= 5, "평점은 1에서 5 사이 여야 합니다.");
Assert.isTrue(amountRating >= 1 && amountRating <= 5, "평점은 1에서 5 사이 여야 합니다.");
Assert.isTrue(tasteRating >= 1 && tasteRating <= 5, "평점은 1에서 5 사이 여야 합니다.");
Assert.notNull(content, "리뷰는 null이 될 수 없습니다.");
this.mainRating = mainRating;
this.amountRating = amountRating;
this.tasteRating = tasteRating;
this.content = content;
}

public Review toEntity(User user, Menu menu) {
Ratings ratings = Ratings.of(this.mainRating, this.amountRating, this.tasteRating);
Ratings ratings = Ratings.of(this.mainRating);
return Review.builder()
.user(user)
.content(this.content)
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/ssu/eatssu/domain/review/dto/ReviewDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public static ReviewDetail from(Review review, Long userId) {
ReviewDetailBuilder builder = ReviewDetail.builder()
.reviewId(review.getId())
.mainRating(review.getRatings().getMainRating())
.amountRating(review.getRatings().getAmountRating())
.tasteRating(review.getRatings().getTasteRating())
.writedAt(review.getCreatedDate().toLocalDate())
.content(review.getContent())
.imageUrls(imageUrls)
Expand Down
Loading