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

Feat(#118) group info update #122

Merged
merged 2 commits into from
Nov 25, 2023
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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -74,4 +76,32 @@ 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) {

// 유저 인증 & 그룹장인지 권한 확인
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());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,25 @@ public UserInfoResponse userAuthenticateAndGroupMatch(String authorization, Long

return jsonResult.getResObj();
}

// Auth 서버에 인증 요청을 보낸 후 가져온 정보로 해당 그룹의 그룹장인지 판별한다.
public UserInfoResponse userAuthenticateAndGroupLeaderMatch(String authorization, Long groupId) {
JsonResult<UserInfoResponse> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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;
import com.heachi.mysql.define.group.info.repository.response.GroupInfoUserGroupResponse;
Expand All @@ -14,7 +16,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;
Expand All @@ -31,9 +32,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)
Expand Down Expand Up @@ -200,4 +198,32 @@ 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);

}

@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());
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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;
import com.heachi.housework.TestConfig;
Expand Down Expand Up @@ -37,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.*;
Expand Down Expand Up @@ -437,4 +440,46 @@ void joinRequestStatusIsFalse() {
assertThat(updateMember.getStatus()).isEqualTo(GroupMemberStatus.WITHDRAW);
}

@Test
@DisplayName("그룹 수정 페이지를 성공적으로 불러온다.")
void groupInfoUpdatePageTest() {
// given
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!");
}

@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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GroupMember> findGroupMemberByGroupId(Long groupId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<GroupMember> findGroupMemberByGroupId(Long groupId) {

Expand Down Expand Up @@ -73,7 +88,7 @@ public Optional<GroupMember> findGroupMemberByUserEmailAndTodoId(String email, L
.and(user.email.eq(email)))
.fetchOne());
}

@Override
public Optional<GroupMember> findGroupMemberByGroupMemberIdAndGroupInfoId(Long groupMemberId, Long groupId) {
// select gm from groupMember gm where gm.id= :groupMemberId and gm.groupInfo.id= :groupId
Expand Down