Skip to content

Commit

Permalink
feat(#118): 그룹 수정 API 구현 & Test
Browse files Browse the repository at this point in the history
  • Loading branch information
jusung-c committed Nov 23, 2023
1 parent f5fbf3f commit dd0a998
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
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 @@ -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());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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.*;
Expand Down Expand Up @@ -440,7 +442,8 @@ void joinRequestStatusIsFalse() {

@Test
@DisplayName("그룹 수정 페이지를 성공적으로 불러온다.")
void GroupInfoUpdatePage() {
void groupInfoUpdatePageTest() {
// given
User user = userRepository.save(generateUser());
GroupInfo group = groupInfoRepository.save(generateGroupInfo(user));

Expand All @@ -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");
}
}
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;
}
}

0 comments on commit dd0a998

Please sign in to comment.