Skip to content

Commit

Permalink
fixing: getTheMemberHouseWork 수정중
Browse files Browse the repository at this point in the history
  • Loading branch information
SDB016 committed Jun 25, 2022
1 parent e9e1f65 commit 376157d
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 35 deletions.
70 changes: 42 additions & 28 deletions src/main/java/com/depromeet/fairer/api/HouseWorkController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
import com.depromeet.fairer.dto.housework.request.HouseWorksCreateRequestDto;
import com.depromeet.fairer.dto.housework.request.HouseWorkUpdateRequestDto;
Expand All @@ -12,6 +13,7 @@
import com.depromeet.fairer.service.member.MemberService;

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

import javax.validation.Valid;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -49,7 +50,7 @@ public ResponseEntity<HouseWorksCreateResponseDto> createHouseWorks(@ApiIgnore @
}

@Tag(name = "houseWorks")
@ApiOperation(value = "집안일 수정 API ")
@ApiOperation(value = "집안일 수정 API")
@PutMapping("/{houseWorkId}")
public ResponseEntity<HouseWorkResponseDto> editHouseWork(@ApiIgnore @RequestMemberId Long memberId,
@RequestBody @Valid HouseWorkUpdateRequestDto dto,
Expand All @@ -72,34 +73,47 @@ public ResponseEntity<?> deleteHouseWork(
}

@Tag(name = "houseWorks")
@ApiOperation(value = "날짜별 집안일 조회", notes = "본인 포함 팀원들의 집안일까지 모두 조회")
@GetMapping(value = "")
public ResponseEntity<List<HouseWorkDateResponseDto>> getHouseWork(@RequestParam("scheduledDate") String scheduledDate,
@ApiIgnore @RequestMemberId Long memberId) {
LocalDate scheduledDateParse = LocalDate.parse(scheduledDate, DateTimeFormatter.ISO_DATE);

List<Member> members = memberService.getMemberList(memberId);

List<HouseWorkDateResponseDto> houseWorkDateResponseDtos = new ArrayList<>();
for (Member member : members) {
List<HouseWork> houseWorks = houseWorkService.getHouseWorks(scheduledDateParse, member);

List<HouseWorkResponseDto> houseWorkResponseDtoList = houseWorks.stream().map(houseWork -> {
List<MemberDto> memberDtoList = memberService.getMemberListByHouseWorkId(houseWork.getHouseWorkId())
.stream().map(MemberDto::from).collect(Collectors.toList());

return HouseWorkResponseDto.from(houseWork, memberDtoList);
}).collect(Collectors.toList());

long countDone = houseWorkResponseDtoList.stream().filter(HouseWorkResponseDto::getSuccess).count();
long countLeft = houseWorkResponseDtoList.stream().filter(houseWorkResponseDto -> !houseWorkResponseDto.getSuccess()).count();

houseWorkDateResponseDtos.add(HouseWorkDateResponseDto.from(member.getMemberId(), scheduledDateParse, countDone, countLeft, houseWorkResponseDtoList));
}

return ResponseEntity.ok(houseWorkDateResponseDtos);
@ApiOperation(value = "특정 멤버의 날짜별 집안일 조회", notes = "특정 멤버의 날짜별 집안일 조회")
@GetMapping("")
public ResponseEntity<PooClass> 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);
return ResponseEntity.ok(theMemberHouseWorks);
}

// @Tag(name = "houseWorks")
// @ApiOperation(value = "날짜별 집안일 조회", notes = "본인 포함 팀원들의 집안일까지 모두 조회")
// @GetMapping(value = "")
// public ResponseEntity<List<HouseWorkDateResponseDto>> getHouseWork(@RequestParam("reqDate") String reqDate,
// @ApiIgnore @RequestMemberId Long memberId) {
// final LocalDate reqDateParse = DateTimeUtils.stringToLocalDate(reqDate);
//
// houseWorkService.getMyTeamAllHouseWorksByDate(memberId, reqDate);
//
// List<Member> members = memberService.getMyTeamMembers(memberId);
//
// List<HouseWorkDateResponseDto> houseWorkDateResponseDtos = new ArrayList<>();
// for (Member member : members) {
// List<HouseWork> houseWorks = houseWorkService.getHouseWorks(reqDateParse, member);
//
// List<HouseWorkResponseDto> houseWorkResponseDtoList = houseWorks.stream().map(houseWork -> {
// List<MemberDto> memberDtoList = memberService.getMemberListByHouseWorkId(houseWork.getHouseWorkId())
// .stream().map(MemberDto::from).collect(Collectors.toList());
//
// return HouseWorkResponseDto.from(houseWork, memberDtoList);
// }).collect(Collectors.toList());
//
// long countDone = houseWorkResponseDtoList.stream().filter(HouseWorkResponseDto::getSuccess).count();
// long countLeft = houseWorkResponseDtoList.stream().filter(houseWorkResponseDto -> !houseWorkResponseDto.getSuccess()).count();
//
// houseWorkDateResponseDtos.add(HouseWorkDateResponseDto.from(member.getMemberId(), reqDateParse, countDone, countLeft, houseWorkResponseDtoList));
// }
//
// return ResponseEntity.ok(houseWorkDateResponseDtos);
// }

@Tag(name = "houseWorks")
@ApiOperation(value = "개별 집안일 조회", notes = "")
@GetMapping(value = "{houseWorkId}/detail")
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/depromeet/fairer/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
import java.util.ArrayList;
import java.util.List;

@NamedEntityGraph(name = "Member.withMyTeamMembers", attributeNodes = {
@NamedAttributeNode(value = "team", subgraph = "team")
},
subgraphs = @NamedSubgraph(name = "team", attributeNodes = {
@NamedAttributeNode("members")
})
)
@Entity
@Table(name = "member")
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.depromeet.fairer.global.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateTimeUtils {
Expand All @@ -10,4 +12,8 @@ public static LocalDateTime convertToLocalDateTime(Date date) {
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}

public static LocalDate stringToLocalDate(String localDate) {
return LocalDate.parse(localDate, DateTimeFormatter.ISO_DATE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
public interface HouseWorkCustomRepository {
Long getHouseWorkSuccessCount(Long memberId, LocalDate startDate, LocalDate endDate);

List<HouseWork> getHouseWorkAndAssignee(Long memberId, LocalDate localDate);


// List<HouseWork> getHouseWorkList(LocalDate scheduledDate, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ public Long getHouseWorkSuccessCount(Long memberId, LocalDate startDate, LocalDa
.stream()
.count();
}

@Override
public List<HouseWork> getHouseWorkAndAssignee(Long memberId, LocalDate localDate) {
return jpaQueryFactory.selectFrom(houseWork)
.innerJoin(assignment.member, member)
.innerJoin(houseWork.assignments, assignment)
.where(member.memberId.eq(memberId)
.and(houseWork.scheduledDate.eq(localDate)))
.fetch();

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.tomcat.jni.Local;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.depromeet.fairer.repository.member;

import com.depromeet.fairer.domain.member.Member;
import org.springframework.data.jpa.repository.EntityGraph;

import java.util.List;
import java.util.Optional;

public interface MemberCustomRepository {
List<Member> getMemberDtoListByHouseWorkId(Long houseWorkId);

List<Member> getMyTeamMembers(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.depromeet.fairer.repository.member;

import com.depromeet.fairer.domain.member.Member;
import com.depromeet.fairer.domain.member.QMember;
import com.depromeet.fairer.domain.team.QTeam;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

import static com.depromeet.fairer.domain.assignment.QAssignment.assignment;
import static com.depromeet.fairer.domain.member.QMember.member;
import static com.depromeet.fairer.domain.team.QTeam.team;

@Repository
@RequiredArgsConstructor
Expand All @@ -23,4 +28,19 @@ public List<Member> getMemberDtoListByHouseWorkId(Long houseWorkId){
.where(assignment.houseWork.houseWorkId.eq(houseWorkId))
.fetch();
}

@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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.depromeet.fairer.domain.team.Team;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -20,4 +21,5 @@ public interface MemberRepository extends JpaRepository<Member, Long>, MemberCus

@EntityGraph(attributePaths = {"team"})
Optional<Member> findWithTeamByMemberId(Long memberId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
import com.depromeet.fairer.repository.housework.HouseWorkRepository;
import com.depromeet.fairer.repository.member.MemberRepository;
import com.depromeet.fairer.service.member.MemberService;
import com.depromeet.fairer.service.team.TeamService;
import com.depromeet.fairer.vo.houseWork.HouseWorkAndAssigneeVo;
import com.depromeet.fairer.vo.houseWork.HouseWorkUpdateVo;
import com.depromeet.fairer.vo.houseWork.PooClass;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -39,6 +43,7 @@ public class HouseWorkService {
private final MemberRepository memberRepository;
private final AssignmentRepository assignmentRepository;
private final MemberService memberService;
private final TeamService teamService;


public List<HouseWorkResponseDto> createHouseWorks(Long memberId, List<HouseWorkUpdateRequestDto> houseWorksDto) {
Expand Down Expand Up @@ -185,4 +190,41 @@ public HouseWork getHouseWorkById(Long houseWorkId) {
return houseWorkRepository.findById(houseWorkId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 집안일 입니다."));
}

public PooClass getTheMemberHouseWorks(Long reqMemberId, Long memberId, LocalDate localDate) {
teamService.validateSameTeam(reqMemberId, memberId);
// 이 멤버 아이디로 집안일 조회
// 집안일에 할당된 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
@@ -1,6 +1,5 @@
package com.depromeet.fairer.service.member;

import com.depromeet.fairer.domain.team.Team;
import com.depromeet.fairer.domain.member.Member;

import com.depromeet.fairer.global.exception.NoSuchMemberException;
Expand All @@ -11,6 +10,7 @@

import javax.transaction.Transactional;

import java.util.ArrayList;
import java.util.List;


Expand All @@ -29,11 +29,6 @@ public Member findWithTeam(Long memberId) {
return memberRepository.findWithTeamByMemberId(memberId).orElseThrow(()->new NoSuchMemberException("해당하는 멤버를 찾을 수 없습니다."));
}

public List<Member> getMemberList(Long memberId){
Team team = findWithTeam(memberId).getTeam();
return memberRepository.findAllByTeam(team);
}

public List<Member> getMemberListByHouseWorkId(Long houseWorkId) {
return memberRepository.getMemberDtoListByHouseWorkId(houseWorkId);
}
Expand All @@ -49,4 +44,9 @@ public Member updateMember(Long memberId, String memberName, String profilePath,
member.setProfilePath(profilePath);
member.setStatusMessage(statusMessage);
return memberRepository.save(member);
}}
}

public List<Member> getMyTeamMembers(Long memberId) {
return memberRepository.getMyTeamMembers(memberId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.depromeet.fairer.global.exception.BadRequestException;
import com.depromeet.fairer.global.exception.CannotJoinTeamException;
import com.depromeet.fairer.global.exception.MemberTokenNotFoundException;
import com.depromeet.fairer.global.exception.PermissionDeniedException;
import com.depromeet.fairer.repository.member.MemberRepository;
import com.depromeet.fairer.repository.team.TeamRepository;
import com.depromeet.fairer.service.member.MemberService;
Expand Down Expand Up @@ -124,4 +125,10 @@ public Team getTeam(Long memberId) {
.getTeam();

}

public void validateSameTeam(Long reqMemberId, Long memberId) {
if (memberService.getMyTeamMembers(reqMemberId).stream().noneMatch(m -> m.getMemberId().equals(memberId))) {
throw new PermissionDeniedException("해당 멤버의 정보를 가져올 수 없습니다.");
}
}
}
Loading

0 comments on commit 376157d

Please sign in to comment.