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 new file mode 100644 index 00000000..6b7b46ea --- /dev/null +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java @@ -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())); + } +} 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 new file mode 100644 index 00000000..094c3935 --- /dev/null +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java @@ -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 userGroupInfoList(String email) { + + // 해당 유저가 속한 그룹 가져오기 + List groupInfoList = groupInfoRepository.findGroupInfoUserGroupResponseListByUserEmail(email); + if (groupInfoList.isEmpty()) { + log.warn(">>>> 유저가 가입한 그룹이 존재하지 않습니다."); + + throw new GroupInfoException(ExceptionMessage.GROUP_INFO_NOT_FOUND); + } + + // 유저속한 그룹에서 그룹 아이디만 추출 + List groupInfoIdList = groupInfoList.stream() + .map(GroupInfoUserGroupResponse::getId) + .collect(Collectors.toList()); + + // Map 각 그룹의 HouseworkTodo 가져오기 + Map houseworkTodoCountMap = houseworkTodoRepository.findHouseworkTodoCountByGroupInfoIdList(groupInfoIdList).stream() + .collect(Collectors.toMap(HouseworkTodoCount::getId, hwTodo -> hwTodo)); + + return groupInfoList.stream() + .map(groupInfo -> { + if (houseworkTodoCountMap.containsKey(groupInfo.getId())) { + List 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()); + } +} diff --git a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/response/GroupInfoUserGroupServiceResponse.java b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/response/GroupInfoUserGroupServiceResponse.java new file mode 100644 index 00000000..9fbc9e5e --- /dev/null +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/group/info/response/GroupInfoUserGroupServiceResponse.java @@ -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 groupMembers; // 그룹 멤버 + private int remainTodoListCnt; // 남은 집안일 개수 + private int progressPercent; // 진행률 (소수점 첫째자리에서 반올림해서 정수로 표현) + + @Builder + private GroupInfoUserGroupServiceResponse(Long id, String name, List groupMembers, + int remainTodoListCnt, int progressPercent) { + this.id = id; + this.name = name; + this.groupMembers = groupMembers; + this.remainTodoListCnt = remainTodoListCnt; + this.progressPercent = progressPercent; + } +} diff --git a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/housework/info/HouseworkInfoService.java b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/housework/info/HouseworkInfoService.java index f5d23949..9fbe3168 100644 --- a/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/housework/info/HouseworkInfoService.java +++ b/heachi-core/housework-api/src/main/java/com/heachi/housework/api/service/housework/info/HouseworkInfoService.java @@ -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 생성 diff --git a/heachi-support/common/src/main/java/com/heachi/admin/common/exception/ExceptionMessage.java b/heachi-support/common/src/main/java/com/heachi/admin/common/exception/ExceptionMessage.java index 1a9a22ea..c7ba788b 100644 --- a/heachi-support/common/src/main/java/com/heachi/admin/common/exception/ExceptionMessage.java +++ b/heachi-support/common/src/main/java/com/heachi/admin/common/exception/ExceptionMessage.java @@ -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("그룹 멤버를 찾지 못했습니다."),