Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9주차 미션 / 서버 3조 장익환 #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Expand Up @@ -3,7 +3,6 @@
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
Expand All @@ -15,7 +14,7 @@
public class JwtAuthHandlerArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
boolean hasAnnotation = parameter.hasParameterAnnotation(PreAuthorize.class);
boolean hasAnnotation = parameter.hasParameterAnnotation(UserPreAuthorize.class);
boolean hasType = long.class.isAssignableFrom(parameter.getParameterType());
log.info("hasAnnotation={}, hasType={}, hasAnnotation && hasType={}", hasAnnotation, hasType, hasAnnotation&&hasType);
return hasAnnotation && hasType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface PreAuthorize {
public @interface UserPreAuthorize {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kuit3.backend.common.exception;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class RestaurantException extends RuntimeException {

private final ResponseStatus exceptionStatus;

public RestaurantException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}

public RestaurantException(ResponseStatus exceptionStatus, String message) {
super(message);
this.exceptionStatus = exceptionStatus;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kuit3.backend.common.exception_handler;

import jakarta.annotation.Priority;

import kuit3.backend.common.exception.RestaurantException;
import kuit3.backend.common.response.BaseErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static kuit3.backend.common.response.status.BaseExceptionResponseStatus.INVALID_RESTAURANT_VALUE;


@Slf4j
@Priority(0)
@RestControllerAdvice
public class RestaurantExceptionControllerAdvice {

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(RestaurantException.class)
public BaseErrorResponse handle_RestaurantException(RestaurantException e) {
log.error("[handle_UserException]", e);
return new BaseErrorResponse(INVALID_RESTAURANT_VALUE, e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
INVALID_USER_VALUE(5000, HttpStatus.BAD_REQUEST.value(), "회원가입 요청에서 잘못된 값이 존재합니다."),
DUPLICATE_EMAIL(5001, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 이메일입니다."),
DUPLICATE_NICKNAME(5002, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 닉네임입니다."),
USER_NOT_FOUND(4003, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 회원입니다."),
PASSWORD_NO_MATCH(4004, HttpStatus.BAD_REQUEST.value(), "비밀번호가 일치하지 않습니다."),
INVALID_USER_STATUS(4005, HttpStatus.BAD_REQUEST.value(), "잘못된 회원 status 값입니다."),
EMAIL_NOT_FOUND(4006, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 이메일입니다.");
USER_NOT_FOUND(5003, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 회원입니다."),
PASSWORD_NO_MATCH(5004, HttpStatus.BAD_REQUEST.value(), "비밀번호가 일치하지 않습니다."),
INVALID_USER_STATUS(5005, HttpStatus.BAD_REQUEST.value(), "잘못된 회원 status 값입니다."),
EMAIL_NOT_FOUND(5006, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 이메일입니다."),


/*
6000 : Restaurant 오류
*/
INVALID_RESTAURANT_VALUE(6000, HttpStatus.BAD_REQUEST.value(), "식당 등록 요청에서 잘못된 값이 존재합니다."),
RESTAURANT_NOT_FOUND(6001, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 식당입니다."),

DUPLICATE_MENU(6002, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 메뉴입니다.");

private final int code;
private final int status;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/kuit3/backend/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class WebConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtAuthenticationInterceptor)
.order(1)
.addPathPatterns("/auth/test");
.addPathPatterns("/auth/test")
.addPathPatterns("/users/*"); // 이게 없으면 인터셉터가 적용이 안되네요...
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/kuit3/backend/controller/AuthController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kuit3.backend.controller;

import kuit3.backend.common.argument_resolver.PreAuthorize;
import kuit3.backend.common.argument_resolver.UserPreAuthorize;
import kuit3.backend.common.exception.UserException;
import kuit3.backend.common.response.BaseResponse;
import kuit3.backend.dto.auth.LoginRequest;
Expand Down Expand Up @@ -38,7 +38,7 @@ public BaseResponse<LoginResponse> login(@Validated @RequestBody LoginRequest au
* 인가(JWT 검증) 테스트
*/
@GetMapping("/test")
public BaseResponse<String> checkAuthorization(@PreAuthorize Long userId) {
public BaseResponse<String> checkAuthorization(@UserPreAuthorize long userId) {
return new BaseResponse<>("userId=" + userId);
}

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/kuit3/backend/controller/MenuController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package kuit3.backend.controller;

import kuit3.backend.common.response.BaseResponse;
import kuit3.backend.dto.menu.GetMenuResponse;
import kuit3.backend.dto.menu.PatchMenuRequest;
import kuit3.backend.service.MenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/menus")
public class MenuController {
private final MenuService menuService;

@GetMapping("")
public BaseResponse<List<GetMenuResponse>> searchByKeyword(@RequestParam("keyword") String keyword){
log.info("menuController.searchByKeyword :: " + keyword);
return new BaseResponse<>(menuService.searchByKeyword(keyword));
}

/*
메뉴 가격 변경
*/
@PatchMapping("/{menuId}")
public BaseResponse<String> modifyPrice(@PathVariable Long menuId,
@RequestBody PatchMenuRequest patchMenuRequest){
log.info("modifyPrice :: request = " + patchMenuRequest);
menuService.modifyPrice(menuId, patchMenuRequest);
return new BaseResponse<>(null);
}
}
103 changes: 103 additions & 0 deletions src/main/java/kuit3/backend/controller/RestaurantController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package kuit3.backend.controller;

import kuit3.backend.common.exception.RestaurantException;
import kuit3.backend.common.response.BaseResponse;
import kuit3.backend.dto.menu.GetMenuResponse;
import kuit3.backend.dto.restaurant.*;
import kuit3.backend.service.RestaurantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static kuit3.backend.common.response.status.BaseExceptionResponseStatus.INVALID_RESTAURANT_VALUE;
import static kuit3.backend.util.BindingResultUtils.getErrorMessages;


@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/restaurants")
public class RestaurantController {
private final RestaurantService restaurantService;

/*
신규 식당 등록
*/
@PostMapping
public BaseResponse<PostRestaurantResponse> register(
@Validated @RequestBody PostRestaurantRequest postRestaurantRequest,
BindingResult bindingResult){
log.info("[RestaurantController.register]");

if(bindingResult.hasErrors()){
throw new RestaurantException(INVALID_RESTAURANT_VALUE, getErrorMessages(bindingResult));
}

PostRestaurantResponse result = restaurantService.createRestaurant(postRestaurantRequest);
return new BaseResponse<PostRestaurantResponse>(result);
}

/*
특정 종류의 식당 조회
*/
@GetMapping("categories/{categoryId}")
public BaseResponse<List<GetRestaurantResponse>> getRestaurants(@PathVariable int categoryId,
@RequestParam Long lastId){
return new BaseResponse<List<GetRestaurantResponse>>(restaurantService.findRestaurantsByCategory(categoryId, lastId));
}

/*
식당에서 메뉴 추가
*/
@PostMapping("/{restaurantId}/menus")
public BaseResponse<Object> addMenu(@PathVariable long restaurantId,
@RequestBody PostRestaurantMenuRequest postRestaurantMenuRequest){
return new BaseResponse<>(restaurantService.addMenu(restaurantId, postRestaurantMenuRequest));
}

/*
식당 메뉴 조회
*/
@GetMapping("/{restaurantId}/menus")
public BaseResponse<List<GetMenuResponse>> getMenus(@PathVariable long restaurantId){
return new BaseResponse<>(restaurantService.getMenus(restaurantId));
}

/*
식당 상태 변경
*/
@PatchMapping("/{restaurantId}/closed")
public BaseResponse<Object> modifyStatusAsClosed(@PathVariable Long restaurantId){
restaurantService.modifyStatusAsClosed(restaurantId);
return new BaseResponse<>(null);
}

/*
식당 영업시간 문구 수정
*/
@PatchMapping("/{restaurantId}/businesshour")
public BaseResponse<Object> modifyBusinnessHour(@PathVariable Long restaurantId,
@RequestBody PatchBusinessHourRequest patchBusinessHourRequest){
log.info("[RestaurantController.modifyBusinnessHour]");
restaurantService.modifyBusinnessHour(restaurantId, patchBusinessHourRequest.getBusiness_hour());
return new BaseResponse<>(null);
}

/*
필터링 조건으로 식당 검색
*/
@GetMapping("")
public BaseResponse<List<GetRestaurantResponse>> searchRestraurants(
@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "min_star", required = false) Integer min_star,
@RequestParam(value = "max_delivery_fee", required = false) String max_delivery_fee,
@RequestParam(required = true) Long lastId
){
return new BaseResponse<>(restaurantService.search(keyword, min_star, max_delivery_fee, lastId));
}

}
23 changes: 17 additions & 6 deletions src/main/java/kuit3/backend/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kuit3.backend.controller;

import kuit3.backend.common.argument_resolver.UserPreAuthorize;
import kuit3.backend.common.exception.UserException;
import kuit3.backend.common.response.BaseResponse;
import kuit3.backend.dto.user.*;
Expand Down Expand Up @@ -38,26 +39,26 @@ public BaseResponse<PostUserResponse> signUp(@Validated @RequestBody PostUserReq
/**
* 회원 휴면
*/
@PatchMapping("/{userId}/dormant")
public BaseResponse<Object> modifyUserStatus_dormant(@PathVariable long userId) {
@PatchMapping("/dormant")
public BaseResponse<Object> modifyUserStatus_dormant(@UserPreAuthorize long userId) {
userService.modifyUserStatus_dormant(userId);
return new BaseResponse<>(null);
}

/**
* 회원 탈퇴
*/
@PatchMapping("/{userId}/deleted")
public BaseResponse<Object> modifyUserStatus_deleted(@PathVariable long userId) {
@PatchMapping("/deleted")
public BaseResponse<Object> modifyUserStatus_deleted(@UserPreAuthorize long userId) {
userService.modifyUserStatus_deleted(userId);
return new BaseResponse<>(null);
}

/**
* 닉네임 변경
*/
@PatchMapping("/{userId}/nickname")
public BaseResponse<String> modifyNickname(@PathVariable long userId,
@PatchMapping("/nickname")
public BaseResponse<String> modifyNickname(@UserPreAuthorize long userId,
@Validated @RequestBody PatchNicknameRequest patchNicknameRequest, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessages(bindingResult));
Expand All @@ -79,4 +80,14 @@ public BaseResponse<List<GetUserResponse>> getUsers(
}
return new BaseResponse<>(userService.getUsers(nickname, email, status));
}

/*
프로필 이미지 변경
*/
@PatchMapping("/profile")
public BaseResponse<Object> modifyProfile(@UserPreAuthorize long userId,
@RequestBody PatchProfileImageRequest patchProfileImageRequest){
userService.modifyProfile_image(userId, patchProfileImageRequest.getProfile_image());
return new BaseResponse<>(null);
}
}
Loading