From f5fbf3f458ea12a37697d3ce65f2f4a8b4bd3c82 Mon Sep 17 00:00:00 2001 From: jusung-c Date: Thu, 23 Nov 2023 21:31:13 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat(#118):=20=EA=B7=B8=EB=A3=B9=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=ED=8E=98=EC=9D=B4=EC=A7=80=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20&=20Test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../group/info/GroupInfoController.java | 11 ++++++ .../api/service/auth/AuthExternalService.java | 21 +++++++++++ .../service/group/info/GroupInfoService.java | 16 ++++++-- .../response/GroupInfoUpdatePageResponse.java | 37 +++++++++++++++++++ .../group/info/GroupInfoServiceTest.java | 14 +++++++ .../GroupMemberRepositoryCustom.java | 3 ++ .../repository/GroupMemberRepositoryImpl.java | 17 ++++++++- 7 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/response/GroupInfoUpdatePageResponse.java diff --git a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java index 7a886577..cd3c36f2 100644 --- a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java @@ -74,4 +74,15 @@ public JsonResult joinGroupRequestHandler(@RequestHeader(name = "Authorizatio return JsonResult.successOf("그룹 가입 요청을 성공적으로 수행했습니다."); } + + @GetMapping("/update/{groupId}") + public JsonResult updateGroupInfo(@RequestHeader(name = "Authorization") String authorization, + @PathVariable(name = "groupId") Long groupId) { + + // Auth 서버에서 사용자 인증 + authExternalService.userAuthenticateAndGroupLeaderMatch(authorization, groupId); + + return JsonResult.successOf(groupInfoService.updateGroupInfoPage(groupId)); + + } } diff --git a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/auth/AuthExternalService.java b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/auth/AuthExternalService.java index cdf485bc..31928807 100644 --- a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/auth/AuthExternalService.java +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/auth/AuthExternalService.java @@ -51,4 +51,25 @@ public UserInfoResponse userAuthenticateAndGroupMatch(String authorization, Long return jsonResult.getResObj(); } + + // Auth 서버에 인증 요청을 보낸 후 가져온 정보로 해당 그룹의 그룹장인지 판별한다. + public UserInfoResponse userAuthenticateAndGroupLeaderMatch(String authorization, Long groupId) { + JsonResult jsonResult = authClients.getUserInfo(authorization).block(); // Mono 객체이므로 Block + + if (jsonResult.getResCode() != 200) { + log.warn(">>>> 유저 인증에 실패했습니다."); + + throw new AuthException(ExceptionMessage.AUTH_SERVER_NOT_RESPOND); + } + + // 그룹장인지 확인 + if (!groupMemberRepository.isLeaderByUserEmailAndGroupInfoId( + jsonResult.getResObj().getEmail(), groupId)) { + log.warn(">>>> 해당 유저[{}]는 해당 그룹[{}]의 그룹장이 아닙니다.", jsonResult.getResObj().getEmail(), groupId); + + throw new GroupMemberException(ExceptionMessage.GROUP_MEMBER_ROLE_NOT_ADMIN); + } + + return jsonResult.getResObj(); + } } diff --git a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java index ea36f62e..34da4000 100644 --- a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java @@ -5,6 +5,7 @@ import com.heachi.admin.common.exception.group.member.GroupMemberException; import com.heachi.housework.api.controller.group.info.request.GroupInfoRegisterRequest; import com.heachi.housework.api.controller.group.info.request.GroupInfoRegisterRequestStatusEnum; +import com.heachi.housework.api.service.group.info.response.GroupInfoUpdatePageResponse; import com.heachi.housework.api.service.group.info.response.GroupInfoUserGroupServiceResponse; import com.heachi.mysql.define.group.info.repository.GroupInfoRepository; import com.heachi.mysql.define.group.info.repository.response.GroupInfoUserGroupResponse; @@ -14,7 +15,6 @@ import com.heachi.admin.common.exception.user.UserException; import com.heachi.housework.api.service.group.info.request.GroupInfoCreateServiceRequest; import com.heachi.mysql.define.group.info.GroupInfo; -import com.heachi.mysql.define.group.info.repository.GroupInfoRepository; import com.heachi.mysql.define.group.member.GroupMember; import com.heachi.mysql.define.group.member.constant.GroupMemberRole; import com.heachi.mysql.define.group.member.constant.GroupMemberStatus; @@ -31,9 +31,6 @@ import java.util.Optional; import java.util.stream.Collectors; -import static com.heachi.mysql.define.group.info.QGroupInfo.groupInfo; -import static com.heachi.mysql.define.user.QUser.user; - @Slf4j @Service @Transactional(readOnly = true) @@ -200,4 +197,15 @@ public void joinRequestHandler(String adminEmail, GroupInfoRegisterRequest reque requestGroupMember.refuseJoin(); } } + + public GroupInfoUpdatePageResponse updateGroupInfoPage(Long groupId) { + GroupInfo group = groupInfoRepository.findById(groupId).orElseThrow(() -> { + log.warn(">>>> 그룹 정보를 찾을 수 없습니다."); + + throw new GroupInfoException(ExceptionMessage.GROUP_INFO_NOT_FOUND); + }); + + return GroupInfoUpdatePageResponse.of(group); + + } } diff --git a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/response/GroupInfoUpdatePageResponse.java b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/response/GroupInfoUpdatePageResponse.java new file mode 100644 index 00000000..73e54c2e --- /dev/null +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/response/GroupInfoUpdatePageResponse.java @@ -0,0 +1,37 @@ +package com.heachi.housework.api.service.group.info.response; + +import com.heachi.mysql.define.group.info.GroupInfo; +import jakarta.validation.constraints.NotEmpty; +import lombok.Builder; +import lombok.Getter; +import org.hibernate.validator.constraints.Length; + +@Getter +public class GroupInfoUpdatePageResponse { + private String bgColor; + private String colorCode; + private String gradient; + private String name; + private String info; + + @Builder + public GroupInfoUpdatePageResponse(String bgColor, String colorCode, String gradient, String name, String info) { + this.bgColor = bgColor; + this.colorCode = colorCode; + this.gradient = gradient; + this.name = name; + this.info = info; + } + + public static GroupInfoUpdatePageResponse of(GroupInfo groupInfo) { + return GroupInfoUpdatePageResponse.builder() + .bgColor(groupInfo.getBgColor()) + .colorCode(groupInfo.getColorCode()) + .gradient(groupInfo.getGradient()) + .name(groupInfo.getName()) + .info(groupInfo.getInfo()) + .build(); + + } + +} diff --git a/heachi-core/housework-api/src/test/java/com/heachi/housework/api/service/group/info/GroupInfoServiceTest.java b/heachi-core/housework-api/src/test/java/com/heachi/housework/api/service/group/info/GroupInfoServiceTest.java index e5241fe1..59b87edb 100644 --- a/heachi-core/housework-api/src/test/java/com/heachi/housework/api/service/group/info/GroupInfoServiceTest.java +++ b/heachi-core/housework-api/src/test/java/com/heachi/housework/api/service/group/info/GroupInfoServiceTest.java @@ -6,6 +6,7 @@ import com.heachi.housework.TestConfig; import com.heachi.housework.api.controller.group.info.request.GroupInfoRegisterRequest; import com.heachi.housework.api.controller.group.info.request.GroupInfoRegisterRequestStatusEnum; +import com.heachi.housework.api.service.group.info.response.GroupInfoUpdatePageResponse; import com.heachi.housework.api.service.group.info.response.GroupInfoUserGroupServiceResponse; import com.heachi.admin.common.exception.user.UserException; import com.heachi.housework.TestConfig; @@ -437,4 +438,17 @@ void joinRequestStatusIsFalse() { assertThat(updateMember.getStatus()).isEqualTo(GroupMemberStatus.WITHDRAW); } + @Test + @DisplayName("그룹 수정 페이지를 성공적으로 불러온다.") + void GroupInfoUpdatePage() { + User user = userRepository.save(generateUser()); + GroupInfo group = groupInfoRepository.save(generateGroupInfo(user)); + + // when + GroupInfoUpdatePageResponse response = groupInfoService.updateGroupInfoPage(group.getId()); + + // then + assertThat(response.getName()).isEqualTo("group"); + assertThat(response.getInfo()).isEqualTo("hello world!"); + } } \ No newline at end of file diff --git a/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/member/repository/GroupMemberRepositoryCustom.java b/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/member/repository/GroupMemberRepositoryCustom.java index d591b571..d82835d7 100644 --- a/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/member/repository/GroupMemberRepositoryCustom.java +++ b/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/member/repository/GroupMemberRepositoryCustom.java @@ -11,6 +11,9 @@ public interface GroupMemberRepositoryCustom { // Email과 GroupId를 통해 사용자가 해당 그룹의 구성원인지 확인하는 쿼리 public boolean existsGroupMemberByUserEmailAndGroupInfoId(String email, Long groupId); + // Email과 GroupId를 통해 사용자가 해당 그룹의 그룹장인지 확인하는 쿼리 + public boolean isLeaderByUserEmailAndGroupInfoId(String email, Long groupId); + // GroupId를 통해 그룹의 구성원을 리턴해주는 쿼리 public List findGroupMemberByGroupId(Long groupId); diff --git a/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/member/repository/GroupMemberRepositoryImpl.java b/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/member/repository/GroupMemberRepositoryImpl.java index 496c9aaa..43708d40 100644 --- a/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/member/repository/GroupMemberRepositoryImpl.java +++ b/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/member/repository/GroupMemberRepositoryImpl.java @@ -2,6 +2,7 @@ import com.heachi.mysql.define.group.info.QGroupInfo; import com.heachi.mysql.define.group.member.GroupMember; +import com.heachi.mysql.define.group.member.constant.GroupMemberRole; import com.heachi.mysql.define.group.member.constant.GroupMemberStatus; import com.heachi.mysql.define.user.User; import com.querydsl.jpa.JPAExpressions; @@ -38,6 +39,20 @@ public boolean existsGroupMemberByUserEmailAndGroupInfoId(String email, Long gro .fetchFirst() != null; } + @Override + public boolean isLeaderByUserEmailAndGroupInfoId(String email, Long groupId) { + + return queryFactory.from(groupMember) + .where(groupMember.groupInfo.id.eq(groupId) + .and(groupMember.user.id.eq( + JPAExpressions + .select(user.id) + .from(user) + .where(user.email.eq(email)))) + .and(groupMember.role.eq(GroupMemberRole.GROUP_ADMIN))) + .fetchFirst() != null; + } + @Override public List findGroupMemberByGroupId(Long groupId) { @@ -73,7 +88,7 @@ public Optional findGroupMemberByUserEmailAndTodoId(String email, L .and(user.email.eq(email))) .fetchOne()); } - + @Override public Optional findGroupMemberByGroupMemberIdAndGroupInfoId(Long groupMemberId, Long groupId) { // select gm from groupMember gm where gm.id= :groupMemberId and gm.groupInfo.id= :groupId From dd0a99824a7142faaf9e341123e0422a2aae2952 Mon Sep 17 00:00:00 2001 From: jusung-c Date: Thu, 23 Nov 2023 22:07:47 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat(#118):=20=EA=B7=B8=EB=A3=B9=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20API=20=EA=B5=AC=ED=98=84=20&=20Test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../group/info/GroupInfoController.java | 21 ++++++++++- .../service/group/info/GroupInfoService.java | 18 ++++++++++ .../GroupInfoUpdateServiceRequest.java | 36 +++++++++++++++++++ .../group/info/GroupInfoServiceTest.java | 33 ++++++++++++++++- .../mysql/define/group/info/GroupInfo.java | 8 +++++ 5 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/request/GroupInfoUpdateServiceRequest.java diff --git a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java index cd3c36f2..3ed45f6d 100644 --- a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java @@ -1,10 +1,12 @@ package com.heachi.housework.api.controller.group.info; +import com.heachi.admin.common.exception.HeachiException; import com.heachi.admin.common.response.JsonResult; import com.heachi.external.clients.auth.response.UserInfoResponse; import com.heachi.housework.api.controller.group.info.request.GroupInfoRegisterRequest; import com.heachi.housework.api.service.auth.AuthExternalService; import com.heachi.housework.api.service.group.info.GroupInfoService; +import com.heachi.housework.api.service.group.info.request.GroupInfoUpdateServiceRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; @@ -79,10 +81,27 @@ public JsonResult joinGroupRequestHandler(@RequestHeader(name = "Authorizatio public JsonResult updateGroupInfo(@RequestHeader(name = "Authorization") String authorization, @PathVariable(name = "groupId") Long groupId) { - // Auth 서버에서 사용자 인증 + // 유저 인증 & 그룹장인지 권한 확인 authExternalService.userAuthenticateAndGroupLeaderMatch(authorization, groupId); return JsonResult.successOf(groupInfoService.updateGroupInfoPage(groupId)); } + + @PostMapping("/update/{groupId}") + public JsonResult updateGroupInfo(@RequestHeader(name = "Authorization") String authorization, + @PathVariable(name = "groupId") Long groupId, + @Valid @RequestBody GroupInfoCreateRequest request) { + + try { + // 유저 인증 & 그룹장인지 권한 확인 + authExternalService.userAuthenticateAndGroupLeaderMatch(authorization, groupId); + groupInfoService.updateGroupInfo(GroupInfoUpdateServiceRequest.of(request, groupId)); + + return JsonResult.successOf("GroupInfo Update Success."); + } catch (HeachiException e) { + return JsonResult.failOf(e.getMessage()); + } + + } } diff --git a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java index 34da4000..dcd79348 100644 --- a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java @@ -5,6 +5,7 @@ import com.heachi.admin.common.exception.group.member.GroupMemberException; import com.heachi.housework.api.controller.group.info.request.GroupInfoRegisterRequest; import com.heachi.housework.api.controller.group.info.request.GroupInfoRegisterRequestStatusEnum; +import com.heachi.housework.api.service.group.info.request.GroupInfoUpdateServiceRequest; import com.heachi.housework.api.service.group.info.response.GroupInfoUpdatePageResponse; import com.heachi.housework.api.service.group.info.response.GroupInfoUserGroupServiceResponse; import com.heachi.mysql.define.group.info.repository.GroupInfoRepository; @@ -208,4 +209,21 @@ public GroupInfoUpdatePageResponse updateGroupInfoPage(Long groupId) { return GroupInfoUpdatePageResponse.of(group); } + + @Transactional + public void updateGroupInfo(GroupInfoUpdateServiceRequest request) { + Long groupId = request.getGroupId(); + + GroupInfo group = groupInfoRepository.findById(groupId).orElseThrow(() -> { + log.warn(">>>> 그룹 정보를 찾을 수 없습니다."); + + throw new GroupInfoException(ExceptionMessage.GROUP_INFO_NOT_FOUND); + }); + + group.updateGroupInfo(request.getBgColor(), + request.getColorCode(), + request.getGradient(), + request.getName(), + request.getInfo()); + } } diff --git a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/request/GroupInfoUpdateServiceRequest.java b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/request/GroupInfoUpdateServiceRequest.java new file mode 100644 index 00000000..b0323d67 --- /dev/null +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/request/GroupInfoUpdateServiceRequest.java @@ -0,0 +1,36 @@ +package com.heachi.housework.api.service.group.info.request; + +import com.heachi.housework.api.controller.group.info.request.GroupInfoCreateRequest; +import lombok.Builder; +import lombok.Getter; + +@Getter +public class GroupInfoUpdateServiceRequest { + private Long groupId; + private String bgColor; + private String colorCode; + private String gradient; + private String name; + private String info; + + @Builder + public GroupInfoUpdateServiceRequest(Long groupId, String bgColor, String colorCode, String gradient, String name, String info) { + this.groupId = groupId; + this.bgColor = bgColor; + this.colorCode = colorCode; + this.gradient = gradient; + this.name = name; + this.info = info; + } + + public static GroupInfoUpdateServiceRequest of(GroupInfoCreateRequest request, Long groupId) { + return GroupInfoUpdateServiceRequest.builder() + .groupId(groupId) + .bgColor(request.getBgColor()) + .colorCode(request.getColorCode()) + .gradient(request.getGradient()) + .name(request.getName()) + .info(request.getInfo()) + .build(); + } +} diff --git a/heachi-core/housework-api/src/test/java/com/heachi/housework/api/service/group/info/GroupInfoServiceTest.java b/heachi-core/housework-api/src/test/java/com/heachi/housework/api/service/group/info/GroupInfoServiceTest.java index 59b87edb..b0382668 100644 --- a/heachi-core/housework-api/src/test/java/com/heachi/housework/api/service/group/info/GroupInfoServiceTest.java +++ b/heachi-core/housework-api/src/test/java/com/heachi/housework/api/service/group/info/GroupInfoServiceTest.java @@ -6,6 +6,7 @@ import com.heachi.housework.TestConfig; import com.heachi.housework.api.controller.group.info.request.GroupInfoRegisterRequest; import com.heachi.housework.api.controller.group.info.request.GroupInfoRegisterRequestStatusEnum; +import com.heachi.housework.api.service.group.info.request.GroupInfoUpdateServiceRequest; import com.heachi.housework.api.service.group.info.response.GroupInfoUpdatePageResponse; import com.heachi.housework.api.service.group.info.response.GroupInfoUserGroupServiceResponse; import com.heachi.admin.common.exception.user.UserException; @@ -38,6 +39,7 @@ import java.time.LocalDate; import java.util.List; +import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; @@ -440,7 +442,8 @@ void joinRequestStatusIsFalse() { @Test @DisplayName("그룹 수정 페이지를 성공적으로 불러온다.") - void GroupInfoUpdatePage() { + void groupInfoUpdatePageTest() { + // given User user = userRepository.save(generateUser()); GroupInfo group = groupInfoRepository.save(generateGroupInfo(user)); @@ -451,4 +454,32 @@ void GroupInfoUpdatePage() { assertThat(response.getName()).isEqualTo("group"); assertThat(response.getInfo()).isEqualTo("hello world!"); } + + @Test + @DisplayName("그룹 정보 수정을 요청했을 경우 성공적으로 수정된다.") + void groupInfoUpdateTest() { + // given + User user = userRepository.save(generateUser()); + GroupInfo group = groupInfoRepository.save(generateGroupInfo(user)); + + GroupInfoUpdateServiceRequest request = GroupInfoUpdateServiceRequest.builder() + .groupId(group.getId()) + .bgColor("updateBG") + .colorCode("updateColor") + .gradient("updateGradient") + .name("updateName") + .info("updateInfo") + .build(); + + // when + groupInfoService.updateGroupInfo(request); + + // then + GroupInfo findGroup = groupInfoRepository.findById(group.getId()).get(); + assertThat(findGroup.getBgColor()).isEqualTo("updateBG"); + assertThat(findGroup.getColorCode()).isEqualTo("updateColor"); + assertThat(findGroup.getGradient()).isEqualTo("updateGradient"); + assertThat(findGroup.getName()).isEqualTo("updateName"); + assertThat(findGroup.getInfo()).isEqualTo("updateInfo"); + } } \ No newline at end of file diff --git a/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/info/GroupInfo.java b/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/info/GroupInfo.java index e9335312..bbc58e4a 100644 --- a/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/info/GroupInfo.java +++ b/heachi-domain-mysql/src/main/java/com/heachi/mysql/define/group/info/GroupInfo.java @@ -68,4 +68,12 @@ public String rotateJoinCode() { return this.joinCode; } + + public void updateGroupInfo(String bgColor, String colorCode, String gradient, String name, String info) { + this.bgColor = bgColor; + this.colorCode = colorCode; + this.gradient = gradient; + this.name = name; + this.info = info; + } }