Skip to content

Commit

Permalink
refactor #96: 특정 멤버, 날짜로 집안일 조회 시 쿼리 많은 문제 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
SDB016 committed Jun 26, 2022
1 parent 739b88f commit 9fe7f23
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 76 deletions.
15 changes: 5 additions & 10 deletions src/main/java/com/depromeet/fairer/api/HouseWorkController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.depromeet.fairer.api;

import com.depromeet.fairer.domain.housework.HouseWork;
import com.depromeet.fairer.domain.member.Member;
import com.depromeet.fairer.dto.member.MemberDto;
import com.depromeet.fairer.global.resolver.RequestMemberId;
import com.depromeet.fairer.global.util.DateTimeUtils;
import com.depromeet.fairer.service.housework.HouseWorkService;
Expand All @@ -13,7 +10,7 @@
import com.depromeet.fairer.service.member.MemberService;

import com.depromeet.fairer.vo.houseWork.HouseWorkUpdateVo;
import com.depromeet.fairer.vo.houseWork.PooClass;
import com.depromeet.fairer.vo.houseWork.HouseWorkAndAssigneeResponseDto;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -27,9 +24,7 @@

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
Expand Down Expand Up @@ -75,11 +70,11 @@ public ResponseEntity<?> deleteHouseWork(
@Tag(name = "houseWorks")
@ApiOperation(value = "특정 멤버의 날짜별 집안일 조회", notes = "특정 멤버의 날짜별 집안일 조회")
@GetMapping("")
public ResponseEntity<PooClass> getTheMemberHouseWork(@RequestParam("reqDate") String reqDate,
@RequestParam("memberId") Long memberId,
@ApiIgnore @RequestMemberId Long reqMemberId) {
public ResponseEntity<HouseWorkAndAssigneeResponseDto> getTheMemberHouseWork(@RequestParam("reqDate") String reqDate,
@RequestParam("memberId") Long memberId,
@ApiIgnore @RequestMemberId Long reqMemberId) {
final LocalDate localDate = DateTimeUtils.stringToLocalDate(reqDate);
final PooClass theMemberHouseWorks = houseWorkService.getTheMemberHouseWorks(reqMemberId, memberId, localDate);
final HouseWorkAndAssigneeResponseDto theMemberHouseWorks = houseWorkService.getTheMemberHouseWorks(reqMemberId, memberId, localDate);
return ResponseEntity.ok(theMemberHouseWorks);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ public List<Member> getMemberDtoListByHouseWorkId(Long houseWorkId){

@Override
public List<Member> getMyTeamMembers(Long memberId) {
final QMember teamMember = new QMember("teamMember");
final QTeam aTeam = new QTeam("ATeam");
// return jpaQueryFactory.select(teamMember)
// .leftJoin(teamMember.team, ateam)
// .where(JPAExpressions
// .selectFrom(team)
// .leftJoin(member.team, team)
// .where(member.memberId.eq(memberId))
// ), aTeam).fetch();
return null;
final QMember member1 = QMember.member;
final QMember member2 = QMember.member;
final QTeam team = QTeam.team;
return jpaQueryFactory.select(member1)
.from(team)
.innerJoin(member2).on(member2.team.eq(team))
.where(member2.memberId.eq(memberId))
.fetch();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.depromeet.fairer.vo.houseWork.HouseWorkAndAssigneeVo;
import com.depromeet.fairer.vo.houseWork.HouseWorkDetailVo;
import com.depromeet.fairer.vo.houseWork.HouseWorkUpdateVo;
import com.depromeet.fairer.vo.houseWork.PooClass;
import com.depromeet.fairer.vo.houseWork.HouseWorkAndAssigneeResponseDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -189,16 +189,23 @@ public HouseWork getHouseWorkById(Long houseWorkId) {
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 집안일 입니다."));
}

public PooClass getTheMemberHouseWorks(Long reqMemberId, Long memberId, LocalDate localDate) {
public HouseWorkAndAssigneeResponseDto getTheMemberHouseWorks(Long reqMemberId, Long memberId, LocalDate localDate) {
teamService.validateSameTeam(reqMemberId, memberId);
final List<HouseWorkDetailVo> houseWorkDetailVoList = houseWorkRepository.getHouseWorkAndAssignees(memberId, localDate);

List<HouseWorkAndAssigneeVo> houseWorkAndAssigneeVos = makeHouseWorkAndAssigneeVos(houseWorkDetailVoList);
final long successCount = houseWorkAndAssigneeVos.stream().filter(HouseWorkAndAssigneeVo::getSuccess).count();
return new HouseWorkAndAssigneeResponseDto(houseWorkAndAssigneeVos, successCount, houseWorkAndAssigneeVos.size() - successCount);
}

@NotNull
private List<HouseWorkAndAssigneeVo> makeHouseWorkAndAssigneeVos(List<HouseWorkDetailVo> houseWorkDetailVoList) {
Map<Long, List<HouseWorkAndAssigneeVo.MemberVo>> houseWorkMap = houseWorkDetailVoList.stream()
.collect(Collectors.groupingBy(
HouseWorkDetailVo::getHouseWorkId,
Collectors.mapping(houseWorkDetailVo -> HouseWorkAndAssigneeVo.MemberVo.builder().memberId(houseWorkDetailVo.getMemberId()).memberName(houseWorkDetailVo.getMemberName()).profilePath(houseWorkDetailVo.getProfilePath()).build(), Collectors.toList())));

List<HouseWorkAndAssigneeVo> response = houseWorkDetailVoList.stream().map(houseWorkDetailVo ->
return houseWorkDetailVoList.stream().map(houseWorkDetailVo ->
HouseWorkAndAssigneeVo.builder()
.houseWorkId(houseWorkDetailVo.getHouseWorkId())
.space(houseWorkDetailVo.getSpace())
Expand All @@ -209,43 +216,5 @@ public PooClass getTheMemberHouseWorks(Long reqMemberId, Long memberId, LocalDat
.assignees(houseWorkMap.get(houseWorkDetailVo.getHouseWorkId()))
.build())
.distinct().collect(Collectors.toList());
final long successCount = response.stream().filter(HouseWorkAndAssigneeVo::getSuccess).count();
return new PooClass(response, successCount, response.size() - successCount);

// 이 멤버 아이디로 집안일 조회
// 집안일에 할당된 assignee 리스트 가져오기
/*
final List<HouseWork> houseWorkAndAssignee = houseWorkRepository.getHouseWorkAndAssignee(memberId, localDate);
final List<HouseWorkAndAssigneeVo> response = houseWorkAndAssignee.stream().map(h -> {
final HouseWorkAndAssigneeVo houseWorkAndAssigneeVo = new HouseWorkAndAssigneeVo();
houseWorkAndAssigneeVo.setHouseWorkId(h.getHouseWorkId());
houseWorkAndAssigneeVo.setSpace(h.getSpace());
houseWorkAndAssigneeVo.setScheduledTime(h.getScheduledTime());
houseWorkAndAssigneeVo.setSuccessDateTime(h.getSuccessDateTime());
houseWorkAndAssigneeVo.setSuccess(h.getSuccess());
final List<HouseWorkAndAssigneeVo.MemberVo> memberVos = h.getAssignments().stream()
.map(Assignment::getMember)
.collect(Collectors.toList())
.stream()
.map(this::makeMemberVo).collect(Collectors.toList());
houseWorkAndAssigneeVo.setAssignees(memberVos);
return houseWorkAndAssigneeVo;
}).collect(Collectors.toList());
final long successCount = houseWorkAndAssignee.stream().filter(HouseWork::getSuccess).count();
return new PooClass(response, successCount, houseWorkAndAssignee.size() - successCount);
*/
}

// @NotNull
// private HouseWorkAndAssigneeVo.MemberVo makeMemberVo(Member member) {
// final HouseWorkAndAssigneeVo.MemberVo memberVo = new HouseWorkAndAssigneeVo.MemberVo();
// memberVo.setMemberId(member.getMemberId());
// memberVo.setMemberName(member.getMemberName());
// memberVo.setProfilePath(member.getProfilePath());
// return memberVo;
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.depromeet.fairer.vo.houseWork;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor @AllArgsConstructor
@ApiModel(value = "특정 멤버, 날짜에 할당된 집안일 모두 조회 반환 객체", description = "특정 멤버, 날짜에 할당된 집안일 모두 조회 반환 객체")
public class HouseWorkAndAssigneeResponseDto {

@ApiModelProperty(value = "집안일, 담당자 객체 리스트")
private List<HouseWorkAndAssigneeVo> houseWorkAndAssigneeVos;

@ApiModelProperty(value = "완료한 집안일 갯수")
private int successCount;

@ApiModelProperty(value = "남은 집안일 갯수")
private int leftCount;

public HouseWorkAndAssigneeResponseDto(List<HouseWorkAndAssigneeVo> houseWorkAndAssigneeVos, long successCount, long leftCount) {
this.houseWorkAndAssigneeVos = houseWorkAndAssigneeVos;
this.successCount = (int) successCount;
this.leftCount = (int) leftCount;
}
}
15 changes: 0 additions & 15 deletions src/main/java/com/depromeet/fairer/vo/houseWork/PooClass.java

This file was deleted.

0 comments on commit 9fe7f23

Please sign in to comment.