From f66a70ee7e23ed48e64fe65383e9ed638f4b9dcc Mon Sep 17 00:00:00 2001 From: hye-on Date: Sat, 19 Aug 2023 23:10:37 +0900 Subject: [PATCH 1/3] =?UTF-8?q?#25=20[REFACTOR]=20:=20custom=20error=20?= =?UTF-8?q?=EC=9D=98=20status=20=EC=BD=94=EB=93=9C=EC=99=80=20response=20?= =?UTF-8?q?=EC=9D=98=20status=20=EC=BD=94=EB=93=9C=20=EC=9D=BC=EC=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../togetUp/exception/GlobalExceptionHandler.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/exception/GlobalExceptionHandler.java b/src/main/java/com/wakeUpTogetUp/togetUp/exception/GlobalExceptionHandler.java index 3c34bae7..6724ce17 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/exception/GlobalExceptionHandler.java @@ -5,6 +5,7 @@ import com.wakeUpTogetUp.togetUp.common.dto.BaseResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingRequestHeaderException; @@ -20,12 +21,13 @@ public class GlobalExceptionHandler { // Catch Custom Exception(BaseException) @ExceptionHandler({ BaseException.class }) - protected BaseResponse handleCustomException(BaseException exception) { - logger.debug("BaseResponse exception occurred: {}", - exception.getMessage(), - exception); + protected ResponseEntity> handleCustomException(BaseException exception) { + logger.debug("BaseResponse exception occurred: {}", exception.getMessage(), exception); - return new BaseResponse<>(exception.getStatus()); + Status status = exception.getStatus(); + ResponseEntity> responseEntity = ResponseEntity.status(status.getHttpStatus()) + .body(new BaseResponse<>(status)); + return responseEntity; } // Catch Bad Request Exception From 116a75c5e88dfe59c0b6edc24e86bc4d5ad903a5 Mon Sep 17 00:00:00 2001 From: hye-on Date: Sun, 20 Aug 2023 12:27:24 +0900 Subject: [PATCH 2/3] =?UTF-8?q?#25=20[FEAT]=20:=20fcmToken=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8,=20=EC=95=8C=EB=A6=BC=EB=8F=99?= =?UTF-8?q?=EC=9D=98=EA=B0=92=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20-group=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EC=9D=B4=EB=A6=84=20room=20=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../togetUp/api/alarm/model/Alarm.java | 2 +- .../togetUp/api/auth/AuthUserResolver.java | 11 +++- .../togetUp/api/auth/service/AuthService.java | 6 +- .../fcmNotification/NotificationService.java | 4 +- .../fcmNotification/entity/Notification.java | 2 +- .../api/mappingRoomUser/GroupUserDto.java | 4 +- .../dto/request/MappingGroupUserReq.java | 4 +- .../togetUp/api/mission/MissionService.java | 4 +- .../togetUp/api/mission/model/MissionLog.java | 2 +- .../api/{group => room}/GroupController.java | 6 +- .../api/{group => room}/GroupRepository.java | 4 +- .../api/{group => room}/GroupService.java | 8 +-- .../{group => room}/dto/request/GroupReq.java | 4 +- .../dto/response/GroupRes.java | 2 +- .../api/{group => room}/model/Room.java | 2 +- .../api/{group => room}/model/RoomUser.java | 3 +- .../togetUp/api/users/UserController.java | 13 +++- .../togetUp/api/users/UserRepository.java | 3 + .../togetUp/api/users/UserService.java | 61 ++++++++++++------- .../togetUp/api/users/fcmToken/FcmToken.java | 18 +++++- .../users/fcmToken/FcmTokenRepository.java | 11 ++++ .../users/fcmToken/fcmTokenRepository.java | 4 -- .../togetUp/api/users/model/User.java | 4 +- .../wakeUpTogetUp/togetUp/common/Status.java | 3 + .../togetUp/utils/JwtService.java | 3 +- .../togetUp/utils/mapper/GroupMapper.java | 4 +- 26 files changed, 125 insertions(+), 67 deletions(-) rename src/main/java/com/wakeUpTogetUp/togetUp/api/{group => room}/GroupController.java (91%) rename src/main/java/com/wakeUpTogetUp/togetUp/api/{group => room}/GroupRepository.java (67%) rename src/main/java/com/wakeUpTogetUp/togetUp/api/{group => room}/GroupService.java (90%) rename src/main/java/com/wakeUpTogetUp/togetUp/api/{group => room}/dto/request/GroupReq.java (79%) rename src/main/java/com/wakeUpTogetUp/togetUp/api/{group => room}/dto/response/GroupRes.java (82%) rename src/main/java/com/wakeUpTogetUp/togetUp/api/{group => room}/model/Room.java (97%) rename src/main/java/com/wakeUpTogetUp/togetUp/api/{group => room}/model/RoomUser.java (96%) create mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenRepository.java delete mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/fcmTokenRepository.java diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/model/Alarm.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/model/Alarm.java index a6195941..37aa77bf 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/model/Alarm.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/model/Alarm.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.alarm.model; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; import com.wakeUpTogetUp.togetUp.api.mission.model.Mission; import com.wakeUpTogetUp.togetUp.api.users.model.User; import lombok.*; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/AuthUserResolver.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/AuthUserResolver.java index 2e252850..86fd8c45 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/AuthUserResolver.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/AuthUserResolver.java @@ -14,6 +14,8 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; + + @Slf4j @Component @RequiredArgsConstructor @@ -29,10 +31,13 @@ public boolean supportsParameter(MethodParameter parameter) { } @Override - public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { - String jwt = webRequest.getHeader("Authorization"); + public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { + if(webRequest.getHeader("Authorization")==null) + throw new BaseException(Status.UNAUTHORIZED); + String jwt = webRequest.getHeader("Authorization").replace("Bearer", "").trim(); if(jwt.isEmpty()) - new BaseException(Status.UNAUTHORIZED); + throw new BaseException(Status.UNAUTHORIZED); + return jwtService.getUserId(jwt); } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/service/AuthService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/service/AuthService.java index 22fc2e8c..2367996d 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/service/AuthService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/service/AuthService.java @@ -34,11 +34,11 @@ public LoginRes socialLogin(SocialLoginReq socialLoginReq) { SocialLoginService loginService = this.getLoginService(socialLoginReq.getLoginType()); //소셜로그인 유저 정보 가져오기 - // SocialUserRes socialUserRes = loginService.getUserInfo(socialLoginReq.getOauthAccessToken()); - //log.info("socialUserResponse {} ", socialUserRes.toString()); + SocialUserRes socialUserRes = loginService.getUserInfo(socialLoginReq.getOauthAccessToken()); + log.info("socialUserResponse {} ", socialUserRes.toString()); //저장된 유저 or 저장한유저의 id 가져오기 - Integer userId= 9;//this.getSignedUserId(socialUserRes,socialLoginReq.getLoginType()); + Integer userId = this.getSignedUserId(socialUserRes,socialLoginReq.getLoginType()); //accessToken 만들기 String accessToken = jwtService.generateAccessToken(userId); diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/fcmNotification/NotificationService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/fcmNotification/NotificationService.java index d34c2be8..850cb702 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/fcmNotification/NotificationService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/fcmNotification/NotificationService.java @@ -2,10 +2,10 @@ import com.wakeUpTogetUp.togetUp.api.fcmNotification.dto.request.PushNotificationReq; import com.wakeUpTogetUp.togetUp.api.fcmNotification.dto.response.PushNotificationRes; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; -import com.wakeUpTogetUp.togetUp.api.group.GroupRepository; +import com.wakeUpTogetUp.togetUp.api.room.GroupRepository; import com.wakeUpTogetUp.togetUp.api.users.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/fcmNotification/entity/Notification.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/fcmNotification/entity/Notification.java index 13cef9dc..6e6d92e5 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/fcmNotification/entity/Notification.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/fcmNotification/entity/Notification.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.fcmNotification.entity; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; import com.wakeUpTogetUp.togetUp.utils.TimestampFormatter; import lombok.Builder; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/mappingRoomUser/GroupUserDto.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/mappingRoomUser/GroupUserDto.java index 1f803433..4bd93e1f 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/mappingRoomUser/GroupUserDto.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/mappingRoomUser/GroupUserDto.java @@ -1,7 +1,7 @@ package com.wakeUpTogetUp.togetUp.api.mappingRoomUser; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; -import com.wakeUpTogetUp.togetUp.api.group.model.RoomUser; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.model.RoomUser; import com.wakeUpTogetUp.togetUp.api.users.model.User; import lombok.*; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/mappingRoomUser/dto/request/MappingGroupUserReq.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/mappingRoomUser/dto/request/MappingGroupUserReq.java index 23d004a8..2fe9ef23 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/mappingRoomUser/dto/request/MappingGroupUserReq.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/mappingRoomUser/dto/request/MappingGroupUserReq.java @@ -1,8 +1,8 @@ package com.wakeUpTogetUp.togetUp.api.mappingRoomUser.dto.request; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; -import com.wakeUpTogetUp.togetUp.api.group.model.RoomUser; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.model.RoomUser; import com.wakeUpTogetUp.togetUp.api.users.model.User; import lombok.*; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/MissionService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/MissionService.java index f308c46d..3c7689e2 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/MissionService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/MissionService.java @@ -1,7 +1,7 @@ package com.wakeUpTogetUp.togetUp.api.mission; -import com.wakeUpTogetUp.togetUp.api.group.GroupRepository; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.GroupRepository; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; import com.wakeUpTogetUp.togetUp.api.mission.model.Mission; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/model/MissionLog.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/model/MissionLog.java index c1c8734e..7918a15f 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/model/MissionLog.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/model/MissionLog.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.mission.model; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; import com.wakeUpTogetUp.togetUp.api.users.model.User; import com.wakeUpTogetUp.togetUp.utils.TimestampFormatter; import lombok.Builder; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/GroupController.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupController.java similarity index 91% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/group/GroupController.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupController.java index 67e73d56..4f9a346b 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/GroupController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupController.java @@ -1,7 +1,7 @@ -package com.wakeUpTogetUp.togetUp.api.group; +package com.wakeUpTogetUp.togetUp.api.room; -import com.wakeUpTogetUp.togetUp.api.group.dto.request.GroupReq; -import com.wakeUpTogetUp.togetUp.api.group.dto.response.GroupRes; +import com.wakeUpTogetUp.togetUp.api.room.dto.request.GroupReq; +import com.wakeUpTogetUp.togetUp.api.room.dto.response.GroupRes; import com.wakeUpTogetUp.togetUp.exception.BaseException; import com.wakeUpTogetUp.togetUp.common.dto.BaseResponse; import com.wakeUpTogetUp.togetUp.common.Status; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/GroupRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupRepository.java similarity index 67% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/group/GroupRepository.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupRepository.java index fa1353be..d0d8b6ad 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/GroupRepository.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupRepository.java @@ -1,7 +1,7 @@ -package com.wakeUpTogetUp.togetUp.api.group; +package com.wakeUpTogetUp.togetUp.api.room; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/GroupService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupService.java similarity index 90% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/group/GroupService.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupService.java index fd3512d3..4f5c8158 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/GroupService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupService.java @@ -1,10 +1,10 @@ -package com.wakeUpTogetUp.togetUp.api.group; +package com.wakeUpTogetUp.togetUp.api.room; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; -import com.wakeUpTogetUp.togetUp.api.group.dto.request.GroupReq; -import com.wakeUpTogetUp.togetUp.api.group.dto.response.GroupRes; +import com.wakeUpTogetUp.togetUp.api.room.dto.request.GroupReq; +import com.wakeUpTogetUp.togetUp.api.room.dto.response.GroupRes; import com.wakeUpTogetUp.togetUp.utils.mapper.GroupMapper; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/dto/request/GroupReq.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/dto/request/GroupReq.java similarity index 79% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/group/dto/request/GroupReq.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/room/dto/request/GroupReq.java index b768b17b..193d1b90 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/dto/request/GroupReq.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/dto/request/GroupReq.java @@ -1,7 +1,7 @@ -package com.wakeUpTogetUp.togetUp.api.group.dto.request; +package com.wakeUpTogetUp.togetUp.api.room.dto.request; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; import lombok.*; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/dto/response/GroupRes.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/dto/response/GroupRes.java similarity index 82% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/group/dto/response/GroupRes.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/room/dto/response/GroupRes.java index f2ae1610..53316b25 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/dto/response/GroupRes.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/dto/response/GroupRes.java @@ -1,4 +1,4 @@ -package com.wakeUpTogetUp.togetUp.api.group.dto.response; +package com.wakeUpTogetUp.togetUp.api.room.dto.response; import lombok.*; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/model/Room.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/Room.java similarity index 97% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/group/model/Room.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/Room.java index fe1533b0..ff363ad8 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/model/Room.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/Room.java @@ -1,4 +1,4 @@ -package com.wakeUpTogetUp.togetUp.api.group.model; +package com.wakeUpTogetUp.togetUp.api.room.model; import lombok.*; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/model/RoomUser.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/RoomUser.java similarity index 96% rename from src/main/java/com/wakeUpTogetUp/togetUp/api/group/model/RoomUser.java rename to src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/RoomUser.java index defe6e53..626eb95d 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/group/model/RoomUser.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/model/RoomUser.java @@ -1,8 +1,7 @@ -package com.wakeUpTogetUp.togetUp.api.group.model; +package com.wakeUpTogetUp.togetUp.api.room.model; import com.wakeUpTogetUp.togetUp.api.users.model.User; import lombok.*; -import org.hibernate.annotations.ColumnDefault; import javax.persistence.*; import java.sql.Timestamp; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java index 43740de6..f0acedf3 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java @@ -4,6 +4,7 @@ import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.common.dto.BaseResponse; import lombok.RequiredArgsConstructor; +import org.springframework.lang.Nullable; import org.springframework.web.bind.annotation.*; @@ -15,7 +16,17 @@ public class UserController { private final UserService userService; private final UserRepository userRepository; - + @PatchMapping("/fcm-token") + public BaseResponse updateFcmToken(@AuthUser Integer userId, @RequestParam( required = false) Integer fcmTokenId, @RequestParam String fcmToken){ + Integer updatedFcmTokenId =userService.updateFcmToken(userId,fcmTokenId,fcmToken); + return new BaseResponse<>(Status.SUCCESS,updatedFcmTokenId); + } + + @PatchMapping("/push") + public BaseResponse updateAgreePush(@AuthUser Integer userId,@RequestParam boolean agreePush){ + userService.updateAgreePush(userId,agreePush); + return new BaseResponse<>(Status.SUCCESS); + } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserRepository.java index 46d091ec..95ded408 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserRepository.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserRepository.java @@ -9,6 +9,9 @@ @Repository public interface UserRepository extends JpaRepository { + + Optional findById(Integer integer); + Optional findBySocialId(String userId); } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java index 00676477..2b596b9e 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserService.java @@ -1,42 +1,57 @@ package com.wakeUpTogetUp.togetUp.api.users; -import com.wakeUpTogetUp.togetUp.api.users.dto.request.LoginReq; -import com.wakeUpTogetUp.togetUp.api.users.dto.request.PatchUserReq; -import com.wakeUpTogetUp.togetUp.api.users.dto.request.SocialLoginReq; -import com.wakeUpTogetUp.togetUp.api.users.dto.request.UserReq; +import com.wakeUpTogetUp.togetUp.api.auth.AuthUser; +import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; +import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenRepository; import com.wakeUpTogetUp.togetUp.api.users.model.User; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; -import com.wakeUpTogetUp.togetUp.api.users.dto.response.UserInfoRes; -import com.wakeUpTogetUp.togetUp.api.users.dto.response.UserRes; -import com.wakeUpTogetUp.togetUp.api.users.dto.response.UserTokenRes; -import com.wakeUpTogetUp.togetUp.utils.JwtService; -import com.wakeUpTogetUp.togetUp.utils.mapper.UserMapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.apache.ibatis.jdbc.Null; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import lombok.RequiredArgsConstructor; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; +import java.util.Objects; @Service -@Transactional(readOnly = true) @RequiredArgsConstructor public class UserService { private final UserRepository userRepository; - private final JwtService jwtService; - - - - - + private final FcmTokenRepository fcmTokenRepository; + + + public Integer updateFcmToken(Integer userId, Integer fcmTokenId, String fcmToken) { + + User user = userRepository.findById(userId).orElse(null); + FcmToken fcmTokenObject; + if (Objects.isNull(user)) + new BaseException(Status.INVALID_USER_ID); + if(fcmTokenId==null) + { + fcmTokenObject = FcmToken.builder() + .value(fcmToken) + .user(user) + .build(); + }else { + fcmTokenObject = fcmTokenRepository.findById(fcmTokenId).orElse( + FcmToken.builder() + .value(fcmToken) + .user(user) + .build()); + fcmTokenObject.updateFcmToken(fcmToken); + } + + return fcmTokenRepository.save(fcmTokenObject).getId(); + + } + + public void updateAgreePush(Integer userId, boolean agreePush){ + User user = userRepository.findById(userId).orElseThrow(() -> new BaseException(Status.USER_NOT_FOUND)); + user.setAgreePush(agreePush); + userRepository.save(user); + } } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmToken.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmToken.java index 77f89a77..c26f8fbd 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmToken.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmToken.java @@ -4,13 +4,16 @@ import lombok.*; import javax.persistence.*; +import java.sql.Timestamp; +import java.time.Instant; @Entity @Table(name = "fcm_token") @Getter @Setter @NoArgsConstructor(force = true) -@Data +@Builder +@AllArgsConstructor public class FcmToken { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -21,4 +24,17 @@ public class FcmToken { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; + + @Column(name = "updated_at") + private Timestamp updatedAt; + @PreUpdate + void updatedAt() { + this.updatedAt = Timestamp.from(Instant.now()); + } + + public void updateFcmToken(String fcmToken){ + setValue(fcmToken); + } + + } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenRepository.java new file mode 100644 index 00000000..77318e32 --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/FcmTokenRepository.java @@ -0,0 +1,11 @@ +package com.wakeUpTogetUp.togetUp.api.users.fcmToken; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + + + +@Repository +public interface FcmTokenRepository extends JpaRepository { + +} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/fcmTokenRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/fcmTokenRepository.java deleted file mode 100644 index ef181476..00000000 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/fcmToken/fcmTokenRepository.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.wakeUpTogetUp.togetUp.api.users.fcmToken; - -public class fcmTokenRepository { -} diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java index 546ccb2a..f6de97dd 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/model/User.java @@ -1,6 +1,6 @@ package com.wakeUpTogetUp.togetUp.api.users.model; -import com.wakeUpTogetUp.togetUp.api.group.model.RoomUser; +import com.wakeUpTogetUp.togetUp.api.room.model.RoomUser; import com.wakeUpTogetUp.togetUp.api.auth.LoginType; import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; import lombok.Builder; @@ -22,7 +22,7 @@ @Getter @Entity @SQLDelete(sql = "UPDATE user SET is_deleted = true WHERE id = ?") -@Where(clause = "deleted = false") +@Where(clause = "is_deleted = false") @Table(name="user") @NoArgsConstructor public class User { diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/common/Status.java b/src/main/java/com/wakeUpTogetUp/togetUp/common/Status.java index 008077bd..702123c3 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/common/Status.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/common/Status.java @@ -40,6 +40,9 @@ public enum Status { INVALID_MISSION_ID(HttpStatus.BAD_REQUEST, "유효하지 않은 미션 id 입니다."), INVALID_ALARM_ID(HttpStatus.BAD_REQUEST, "유효하지 않은 알람 id 입니다."), INVALID_ROUTINE_ID(HttpStatus.BAD_REQUEST, "유효하지 않은 루틴 id 입니다."), + // USER + + USER_NOT_FOUND(HttpStatus.NOT_FOUND,"존재하지 않는 유저입니다."), // Alarm ALARM_NOT_FOUND(HttpStatus.NOT_FOUND, "해당하는 알람을 찾을 수 없습니다."), diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/utils/JwtService.java b/src/main/java/com/wakeUpTogetUp/togetUp/utils/JwtService.java index 2104eb0c..c084a674 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/utils/JwtService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/utils/JwtService.java @@ -49,9 +49,8 @@ public String getJwt(){ return request.getHeader("X-ACCESS-TOKEN"); } - public Integer getUserId(String headerToken) { + public Integer getUserId(String accessToken) { Claims claims; - String accessToken = headerToken.replace("Bearer", "").trim(); try{ claims = Jwts.parserBuilder() .setSigningKey(getSigningKey(key)) diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/utils/mapper/GroupMapper.java b/src/main/java/com/wakeUpTogetUp/togetUp/utils/mapper/GroupMapper.java index 6e591ef2..76622ba4 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/utils/mapper/GroupMapper.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/utils/mapper/GroupMapper.java @@ -1,7 +1,7 @@ package com.wakeUpTogetUp.togetUp.utils.mapper; -import com.wakeUpTogetUp.togetUp.api.group.dto.response.GroupRes; -import com.wakeUpTogetUp.togetUp.api.group.model.Room; +import com.wakeUpTogetUp.togetUp.api.room.dto.response.GroupRes; +import com.wakeUpTogetUp.togetUp.api.room.model.Room; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; From 0e50d822a1d0ce8b468cbc782408594d257dd95d Mon Sep 17 00:00:00 2001 From: hye-on Date: Sun, 20 Aug 2023 15:24:06 +0900 Subject: [PATCH 3/3] =?UTF-8?q?#25=20[CHORE]=20:=20swagger=20=20=EC=84=B8?= =?UTF-8?q?=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 ++ .../togetUp/api/alarm/AlarmController.java | 2 + .../togetUp/api/auth/AuthController.java | 9 ++-- .../api/mission/MissionController.java | 3 +- .../togetUp/api/room/GroupController.java | 4 +- .../togetUp/api/users/UserController.java | 14 +++++-- .../togetUp/common/Constant.java | 7 ++++ .../togetUp/config/OpenApiConfig.java | 41 +++++++++++++++++++ src/main/resources/application.yml | 3 +- 9 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/wakeUpTogetUp/togetUp/config/OpenApiConfig.java diff --git a/build.gradle b/build.gradle index 8b943fdb..e328fca5 100644 --- a/build.gradle +++ b/build.gradle @@ -101,6 +101,9 @@ dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' + // Swagger + implementation 'org.springdoc:springdoc-openapi-ui:1.7.0' + } ext { snippetsDir = file('build/generated-snippets') diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/AlarmController.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/AlarmController.java index f8a01aa9..9528bbc5 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/AlarmController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/AlarmController.java @@ -8,6 +8,7 @@ import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.exception.BaseException; import com.wakeUpTogetUp.togetUp.utils.JwtService; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @@ -15,6 +16,7 @@ import javax.validation.Valid; import java.util.List; +@Tag(name = "알람") @RestController @RequestMapping("/app/alarm") @RequiredArgsConstructor diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/AuthController.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/AuthController.java index 66ad2026..4917c437 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/AuthController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/auth/AuthController.java @@ -5,20 +5,17 @@ import com.wakeUpTogetUp.togetUp.api.auth.dto.response.LoginRes; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.common.dto.BaseResponse; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; -import org.springframework.http.HttpStatus; - - +@Tag(name = "Auth(로그인)",description = "로그인 및 유저의 권한 인증") @RestController @RequiredArgsConstructor @RequestMapping("/app/auth") public class AuthController { private final AuthService authService; - /** - * - */ + @ResponseBody @PostMapping("/login") public BaseResponse join(@RequestBody SocialLoginReq loginReq) { diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/MissionController.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/MissionController.java index 4b960975..8a822ea9 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/MissionController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/mission/MissionController.java @@ -9,13 +9,14 @@ import com.wakeUpTogetUp.togetUp.api.mission.dto.response.MissionLogRes; import com.wakeUpTogetUp.togetUp.api.mission.dto.response.PostObjectRecognitionRes; import com.wakeUpTogetUp.togetUp.utils.JwtService; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.List; - +@Tag(name = "미션") @RestController @RequiredArgsConstructor @RequestMapping("/app/mission") diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupController.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupController.java index 4f9a346b..aa750c2f 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/room/GroupController.java @@ -5,14 +5,14 @@ import com.wakeUpTogetUp.togetUp.exception.BaseException; import com.wakeUpTogetUp.togetUp.common.dto.BaseResponse; import com.wakeUpTogetUp.togetUp.common.Status; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; - - +@Tag(name = "그룹") @RestController @RequiredArgsConstructor @RequestMapping("/app/group") diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java index f0acedf3..41acab76 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/users/UserController.java @@ -3,11 +3,14 @@ import com.wakeUpTogetUp.togetUp.api.auth.AuthUser; import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.common.dto.BaseResponse; +import io.swagger.v3.oas.annotations.Hidden; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; -import org.springframework.lang.Nullable; import org.springframework.web.bind.annotation.*; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; - +@Tag(name = "유저") @RestController @RequiredArgsConstructor @RequestMapping("/app/users") @@ -16,14 +19,17 @@ public class UserController { private final UserService userService; private final UserRepository userRepository; + + @Operation(summary = "fcmToken 업데이트") @PatchMapping("/fcm-token") - public BaseResponse updateFcmToken(@AuthUser Integer userId, @RequestParam( required = false) Integer fcmTokenId, @RequestParam String fcmToken){ + public BaseResponse updateFcmToken(@Parameter(hidden = true) @AuthUser Integer userId, @Parameter(description = "디바이스토큰 id")@RequestParam( required = false) Integer fcmTokenId, @Parameter(description = "토큰값",required = true) @RequestParam String fcmToken){ Integer updatedFcmTokenId =userService.updateFcmToken(userId,fcmTokenId,fcmToken); return new BaseResponse<>(Status.SUCCESS,updatedFcmTokenId); } + @Operation(summary="알림설정 동의 변경") @PatchMapping("/push") - public BaseResponse updateAgreePush(@AuthUser Integer userId,@RequestParam boolean agreePush){ + public BaseResponse updateAgreePush(@Parameter(hidden = true) @AuthUser Integer userId,@RequestParam() boolean agreePush){ userService.updateAgreePush(userId,agreePush); return new BaseResponse<>(Status.SUCCESS); } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/common/Constant.java b/src/main/java/com/wakeUpTogetUp/togetUp/common/Constant.java index d764c0a1..6e85584a 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/common/Constant.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/common/Constant.java @@ -1,4 +1,11 @@ package com.wakeUpTogetUp.togetUp.common; + public class Constant { + public static final String API_NAME = "TOGETUP-API"; + public static final String API_VERSION = "v1"; + public static final String API_DESCRIPTION = "TOGEPUP API DESCRIPTION"; + public static final String BEARER= "jwt"; + public static final String JWT_PREFIX= "Bearer"; + } \ No newline at end of file diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/config/OpenApiConfig.java b/src/main/java/com/wakeUpTogetUp/togetUp/config/OpenApiConfig.java new file mode 100644 index 00000000..688bfde5 --- /dev/null +++ b/src/main/java/com/wakeUpTogetUp/togetUp/config/OpenApiConfig.java @@ -0,0 +1,41 @@ +package com.wakeUpTogetUp.togetUp.config; + +import com.wakeUpTogetUp.togetUp.common.Constant; +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.security.SecurityRequirement; +import io.swagger.v3.oas.models.security.SecurityScheme; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import io.swagger.v3.oas.models.servers.Server; + +@Configuration +class OpenApiConfig { + @Bean + public OpenAPI openApi() { + Info info =new Info() + .title(Constant.API_NAME) + .version(Constant.API_VERSION) + .description(Constant.API_DESCRIPTION); + + return new OpenAPI() + .addServersItem(new Server().url("http://localhost:9010")) + .addServersItem(new Server().url("https://togeptup.shop")) + .info(info) + .addSecurityItem(new SecurityRequirement().addList(Constant.BEARER)) + .components( + new Components() + .addSecuritySchemes( + Constant.BEARER, + new SecurityScheme() + .name(Constant.BEARER) + .type(SecurityScheme.Type.HTTP) + .scheme(Constant.JWT_PREFIX) + .bearerFormat("JWT") + + ) + + ); + } +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a48187ff..4f940c4f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,5 @@ spring: profiles: active: dev - include: oauth \ No newline at end of file + include: oauth +