-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#75): Authorization 헤더를 통한 유저의 그룹 조회
- Loading branch information
1 parent
c9a03fe
commit 9ea2512
Showing
5 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...api/src/main/java/com/heachi/housework/api/controller/group/info/GroupInfoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...ework-api/src/main/java/com/heachi/housework/api/service/group/info/GroupInfoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...m/heachi/housework/api/service/group/info/response/GroupInfoUserGroupServiceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters