diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/CategoryController.java b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/CategoryController.java index a264818..d925eaf 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/CategoryController.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/CategoryController.java @@ -25,9 +25,7 @@ public class CategoryController { private final CategoryService categoryService; private final TokenProvider tokenProvider; - @GetMapping("") - public ApiResponse> readCategoryAll(HttpServletRequest request) - { + public long getUserId(HttpServletRequest request){ String accessToken = tokenProvider.extractAccessToken(request).orElse(null); Long userId = null; @@ -35,12 +33,19 @@ public ApiResponse> readCategoryAll(HttpServletRequest req if (accessToken != null) { userId = tokenProvider.extractId(accessToken).orElse(null); } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); + throw new CustomException(ErrorStatus.NOT_LOGIN_ERROR); } if(userId == null) { throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); } + return userId; + } + + @GetMapping("") + public ApiResponse> readCategoryAll(HttpServletRequest request) + { + long userId = getUserId(request); List categoryList = categoryService.readCategoryAll(userId); return ApiResponse.success(SuccessStatus.OK, categoryList); @@ -50,19 +55,7 @@ public ApiResponse> readCategoryAll(HttpServletRequest req public ApiResponse createCategory(HttpServletRequest request, @RequestBody CategoryRequestDTO categoryRequestDTO) { - String accessToken = tokenProvider.extractAccessToken(request).orElse(null); - - Long userId = null; - - if (accessToken != null) { - userId = tokenProvider.extractId(accessToken).orElse(null); - } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); - - } - if(userId == null) { - throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); - } + long userId = getUserId(request); Category category = categoryService.createCategory(userId, categoryRequestDTO); if (category == null){ @@ -76,19 +69,7 @@ public ApiResponse updateCategory(HttpServletRequest request @PathVariable long categoryId, @RequestBody CategoryRequestDTO categoryRequestDTO) { - String accessToken = tokenProvider.extractAccessToken(request).orElse(null); - - Long userId = null; - - if (accessToken != null) { - userId = tokenProvider.extractId(accessToken).orElse(null); - } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); - - } - if(userId == null) { - throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); - } + long userId = getUserId(request); categoryService.updateCategory(userId, categoryId, categoryRequestDTO); return ApiResponse.success(SuccessStatus.OK, categoryRequestDTO); @@ -98,19 +79,7 @@ public ApiResponse updateCategory(HttpServletRequest request public ApiResponse deleteCategory(HttpServletRequest request, @PathVariable long categoryId) { - String accessToken = tokenProvider.extractAccessToken(request).orElse(null); - - Long userId = null; - - if (accessToken != null) { - userId = tokenProvider.extractId(accessToken).orElse(null); - } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); - - } - if(userId == null) { - throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); - } + long userId = getUserId(request); categoryService.deleteCategory(userId, categoryId); return ApiResponse.success(SuccessStatus.OK); diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/DoController.java b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/DoController.java index 9397e2a..04ce76f 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/DoController.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/DoController.java @@ -23,10 +23,7 @@ public class DoController { private final DoService doService; private final TokenProvider tokenProvider; - @PostMapping("") - public ApiResponse createDo(HttpServletRequest request, - @RequestBody DoRequestDTO doRequestDTO) - { + public long getUserId(HttpServletRequest request){ String accessToken = tokenProvider.extractAccessToken(request).orElse(null); Long userId = null; @@ -34,12 +31,20 @@ public ApiResponse createDo(HttpServletRequest request, if (accessToken != null) { userId = tokenProvider.extractId(accessToken).orElse(null); } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); + throw new CustomException(ErrorStatus.NOT_LOGIN_ERROR); } if(userId == null) { throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); } + return userId; + } + + @PostMapping("") + public ApiResponse createDo(HttpServletRequest request, + @RequestBody DoRequestDTO doRequestDTO) + { + long userId = getUserId(request); Do done = doService.createDo(userId, doRequestDTO); if (done == null){ @@ -53,19 +58,7 @@ public ApiResponse updateDo(HttpServletRequest request, @PathVariable long doId, @RequestBody DoRequestDTO doRequestDTO) { - String accessToken = tokenProvider.extractAccessToken(request).orElse(null); - - Long userId = null; - - if (accessToken != null) { - userId = tokenProvider.extractId(accessToken).orElse(null); - } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); - - } - if(userId == null) { - throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); - } + long userId = getUserId(request); doService.updateDo(userId, doId, doRequestDTO); return ApiResponse.success(SuccessStatus.OK); @@ -75,19 +68,7 @@ public ApiResponse updateDo(HttpServletRequest request, public ApiResponse deleteDo(HttpServletRequest request, @PathVariable long doId) { - String accessToken = tokenProvider.extractAccessToken(request).orElse(null); - - Long userId = null; - - if (accessToken != null) { - userId = tokenProvider.extractId(accessToken).orElse(null); - } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); - - } - if(userId == null) { - throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); - } + long userId = getUserId(request); doService.deleteDo(userId, doId); return ApiResponse.success(SuccessStatus.OK); diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/PlanController.java b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/PlanController.java index 1525602..0e8f6c8 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/PlanController.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/PlanController.java @@ -25,10 +25,7 @@ public class PlanController { private final PlanService planService; private final TokenProvider tokenProvider; - @PostMapping("") - public ApiResponse createPlan(HttpServletRequest request, - @RequestBody PlanRequestDTO planRequestDTO) - { + public long getUserId(HttpServletRequest request){ String accessToken = tokenProvider.extractAccessToken(request).orElse(null); Long userId = null; @@ -36,12 +33,20 @@ public ApiResponse createPlan(HttpServletRequest request, if (accessToken != null) { userId = tokenProvider.extractId(accessToken).orElse(null); } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); + throw new CustomException(ErrorStatus.NOT_LOGIN_ERROR); } if(userId == null) { throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); } + return userId; + } + + @PostMapping("") + public ApiResponse createPlan(HttpServletRequest request, + @RequestBody PlanRequestDTO planRequestDTO) + { + long userId = getUserId(request); Plan plan = planService.createPlan(userId, planRequestDTO); if (plan == null){ @@ -55,19 +60,7 @@ public ApiResponse updatePlan(HttpServletRequest request, @PathVariable long planId, @RequestBody PlanRequestDTO planRequestDTO) { - String accessToken = tokenProvider.extractAccessToken(request).orElse(null); - - Long userId = null; - - if (accessToken != null) { - userId = tokenProvider.extractId(accessToken).orElse(null); - } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); - - } - if(userId == null) { - throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); - } + long userId = getUserId(request); planService.updatePlan(userId, planId, planRequestDTO); return ApiResponse.success(SuccessStatus.OK); @@ -99,19 +92,7 @@ public ApiResponse deletePlan(HttpServletRequest request, public ApiResponse donePlan(HttpServletRequest request, @PathVariable long planId) { - String accessToken = tokenProvider.extractAccessToken(request).orElse(null); - - Long userId = null; - - if (accessToken != null) { - userId = tokenProvider.extractId(accessToken).orElse(null); - } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); - - } - if(userId == null) { - throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); - } + long userId = getUserId(request); planService.donePlan(userId, planId); return ApiResponse.success(SuccessStatus.OK); diff --git a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/SeeController.java b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/SeeController.java index f7a43f5..7ea8b4e 100644 --- a/src/main/java/com/trendithon/timetris/domain/mainpage/controller/SeeController.java +++ b/src/main/java/com/trendithon/timetris/domain/mainpage/controller/SeeController.java @@ -23,10 +23,7 @@ public class SeeController { private final SeeService seeService; private final TokenProvider tokenProvider; - @PostMapping("") - public ApiResponse createSee(HttpServletRequest request, - @RequestBody SeeRequestDTO seeRequestDTO) - { + public long getUserId(HttpServletRequest request){ String accessToken = tokenProvider.extractAccessToken(request).orElse(null); Long userId = null; @@ -34,12 +31,20 @@ public ApiResponse createSee(HttpServletRequest request, if (accessToken != null) { userId = tokenProvider.extractId(accessToken).orElse(null); } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); + throw new CustomException(ErrorStatus.NOT_LOGIN_ERROR); } if(userId == null) { throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); } + return userId; + } + + @PostMapping("") + public ApiResponse createSee(HttpServletRequest request, + @RequestBody SeeRequestDTO seeRequestDTO) + { + long userId = getUserId(request); See see = seeService.createSee(userId, seeRequestDTO); if (see == null){ @@ -53,19 +58,7 @@ public ApiResponse updateSee(HttpServletRequest request, @PathVariable long seeId, @RequestBody SeeRequestDTO seeRequestDTO) { - String accessToken = tokenProvider.extractAccessToken(request).orElse(null); - - Long userId = null; - - if (accessToken != null) { - userId = tokenProvider.extractId(accessToken).orElse(null); - } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); - - } - if(userId == null) { - throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); - } + long userId = getUserId(request); seeService.updateSee(userId, seeId, seeRequestDTO); return ApiResponse.success(SuccessStatus.OK); @@ -75,19 +68,7 @@ public ApiResponse updateSee(HttpServletRequest request, public ApiResponse deleteSee(HttpServletRequest request, @PathVariable long seeId) { - String accessToken = tokenProvider.extractAccessToken(request).orElse(null); - - Long userId = null; - - if (accessToken != null) { - userId = tokenProvider.extractId(accessToken).orElse(null); - } else { - return ApiResponse.failure(ErrorStatus.NOT_LOGIN_ERROR); - - } - if(userId == null) { - throw new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR); - } + long userId = getUserId(request); seeService.deleteSee(userId, seeId); return ApiResponse.success(SuccessStatus.OK);