Skip to content

Commit

Permalink
feat(#75): Authorization 헤더를 통한 유저의 그룹 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
ghdcksgml1 committed Oct 30, 2023
1 parent c9a03fe commit 9ea2512
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.heachi.housework.api.controller.group.info;

import com.heachi.admin.common.response.JsonResult;
import com.heachi.external.clients.auth.response.UserInfoResponse;
import com.heachi.housework.api.service.auth.AuthExternalService;
import com.heachi.housework.api.service.group.info.GroupInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/group/info")
public class GroupInfoController {

private final AuthExternalService authExternalService;
private final GroupInfoService groupInfoService;

// User가 가입한 Group 정보를 리턴한다.
@GetMapping("/list")
public JsonResult<?> userGroupInfoList(@RequestHeader(name = "Authorization") String authorization) {
UserInfoResponse userInfo = authExternalService.userAuthenticate(authorization);

return JsonResult.successOf(groupInfoService.userGroupInfoList(userInfo.getEmail()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.heachi.housework.api.service.group.info;

import com.heachi.admin.common.exception.ExceptionMessage;
import com.heachi.admin.common.exception.group.info.GroupInfoException;
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;
import com.heachi.mysql.define.housework.todo.constant.HouseworkTodoStatus;
import com.heachi.mysql.define.housework.todo.repository.HouseworkTodoRepository;
import com.heachi.mysql.define.housework.todo.repository.response.HouseworkTodoCount;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class GroupInfoService {

private final GroupInfoRepository groupInfoRepository;
private final HouseworkTodoRepository houseworkTodoRepository;

public List<GroupInfoUserGroupServiceResponse> userGroupInfoList(String email) {

// 해당 유저가 속한 그룹 가져오기
List<GroupInfoUserGroupResponse> groupInfoList = groupInfoRepository.findGroupInfoUserGroupResponseListByUserEmail(email);
if (groupInfoList.isEmpty()) {
log.warn(">>>> 유저가 가입한 그룹이 존재하지 않습니다.");

throw new GroupInfoException(ExceptionMessage.GROUP_INFO_NOT_FOUND);
}

// 유저속한 그룹에서 그룹 아이디만 추출
List<Long> groupInfoIdList = groupInfoList.stream()
.map(GroupInfoUserGroupResponse::getId)
.collect(Collectors.toList());

// Map<GROUP_INFO_ID, HouseworkTodoCount> 각 그룹의 HouseworkTodo 가져오기
Map<Long, HouseworkTodoCount> houseworkTodoCountMap = houseworkTodoRepository.findHouseworkTodoCountByGroupInfoIdList(groupInfoIdList).stream()
.collect(Collectors.toMap(HouseworkTodoCount::getId, hwTodo -> hwTodo));

return groupInfoList.stream()
.map(groupInfo -> {
if (houseworkTodoCountMap.containsKey(groupInfo.getId())) {
List<HouseworkTodoStatus> statusList = houseworkTodoCountMap.get(groupInfo.getId()).getStatus();
int remainTodoListCnt = (int) statusList.stream()
.filter(status -> status == HouseworkTodoStatus.HOUSEWORK_TODO_INCOMPLETE)
.count();

return GroupInfoUserGroupServiceResponse.builder()
.id(groupInfo.getId())
.name(groupInfo.getName())
.groupMembers(
groupInfo.getGroupMembers()
)
.remainTodoListCnt(remainTodoListCnt)
.progressPercent(Math.round((float) (100 * (statusList.size() - remainTodoListCnt)) / statusList.size()))
.build();
} else {
return GroupInfoUserGroupServiceResponse.builder()
.id(groupInfo.getId())
.name(groupInfo.getName())
.groupMembers(
groupInfo.getGroupMembers()
)
.remainTodoListCnt(0)
.progressPercent(0)
.build();
}
})
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.heachi.housework.api.service.group.info.response;

import com.heachi.mysql.define.group.info.repository.response.GroupInfoGroupMember;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

@Getter
@ToString
public class GroupInfoUserGroupServiceResponse {
private Long id; // 그룹 아이디
private String name; // 그룹 이름
private List<GroupInfoGroupMember> groupMembers; // 그룹 멤버
private int remainTodoListCnt; // 남은 집안일 개수
private int progressPercent; // 진행률 (소수점 첫째자리에서 반올림해서 정수로 표현)

@Builder
private GroupInfoUserGroupServiceResponse(Long id, String name, List<GroupInfoGroupMember> groupMembers,
int remainTodoListCnt, int progressPercent) {
this.id = id;
this.name = name;
this.groupMembers = groupMembers;
this.remainTodoListCnt = remainTodoListCnt;
this.progressPercent = progressPercent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public void createHouseworkInfo(HouseworkInfoCreateServiceRequest request) {

// GROUP_INFO 조회
GroupInfo groupInfo = groupInfoRepository.findById(request.getGroupId()).orElseThrow(() -> {
log.warn(">>>> GroupInfo Not Found : {}", ExceptionMessage.GROUP_NOT_FOUND.getText());
log.warn(">>>> GroupInfo Not Found : {}", ExceptionMessage.GROUP_INFO_NOT_FOUND.getText());

throw new GroupInfoException(ExceptionMessage.GROUP_NOT_FOUND);
throw new GroupInfoException(ExceptionMessage.GROUP_INFO_NOT_FOUND);
});

// 담당자 지정 - HOUSEWORK_MEMBER 생성
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public enum ExceptionMessage {
AUTH_NOT_FOUND("계정 정보를 찾을 수 없습니다."),
AUTH_DELETE_FAIL("계정 삭제에 실패했습니다."),

// GroupException
GROUP_NOT_FOUND("그룹을 찾지 못했습니다."),
// GroupInfoException
GROUP_INFO_NOT_FOUND("그룹을 찾지 못했습니다."),

// GroupMemberException
GROUP_MEMBER_NOT_FOUND("그룹 멤버를 찾지 못했습니다."),
Expand Down

0 comments on commit 9ea2512

Please sign in to comment.