Skip to content

Commit

Permalink
Merge pull request #34 from kssumin/feat/#164/additional-attend-status
Browse files Browse the repository at this point in the history
[BE/FEAT] 프로그램 참여 관련 구현
  • Loading branch information
kssumin authored Jan 7, 2024
2 parents 6358466 + 34cd956 commit da4c502
Show file tree
Hide file tree
Showing 28 changed files with 358 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@Builder(toBuilder = true)
public class AttendInfoResponse implements AbstractResponseDto {
private Long memberId;
private Long generation;
private String name;
private String attendStatus;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blackcompany.eeos.program.application.dto;
package com.blackcompany.eeos.attend.application.dto;

import com.blackcompany.eeos.common.support.dto.AbstractRequestDto;
import lombok.AllArgsConstructor;
Expand All @@ -11,7 +11,6 @@
@NoArgsConstructor
@Builder(toBuilder = true)
public class ChangeAttendStatusRequest implements AbstractRequestDto {
private Long memberId;
private String beforeAttendStatus;
private String afterAttendStatus;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.blackcompany.eeos.attend.application.dto;

import com.blackcompany.eeos.common.support.dto.AbstractResponseDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
public class ChangeAttendStatusResponse implements AbstractResponseDto {
private String name;
private String attendStatus;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.blackcompany.eeos.attend.application.dto;

import com.blackcompany.eeos.common.support.dto.AbstractResponseDto;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
public class QueryAttendStatusResponse implements AbstractResponseDto {
private List<AttendInfoResponse> members;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.blackcompany.eeos.attend.application.dto.AttendInfoResponse;
import com.blackcompany.eeos.attend.application.model.AttendStatus;
import com.blackcompany.eeos.member.application.model.MemberModel;
import com.blackcompany.eeos.member.persistence.MemberEntity;
import org.springframework.stereotype.Component;

Expand All @@ -16,6 +17,14 @@ public AttendInfoResponse from(final MemberEntity source, final AttendStatus att
.build();
}

public AttendInfoResponse from(final MemberModel source, final AttendStatus attendStatus) {
return AttendInfoResponse.builder()
.memberId(source.getId())
.name(source.getName())
.attendStatus(attendStatus.getStatus())
.build();
}

public AttendInfoResponse from(final MemberEntity source, final String status) {
return AttendInfoResponse.builder()
.memberId(source.getId())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.blackcompany.eeos.attend.application.dto.converter;

import com.blackcompany.eeos.attend.application.dto.ChangeAttendStatusResponse;
import org.springframework.stereotype.Component;

@Component
public class ChangeAttendStatusConverter {
public ChangeAttendStatusResponse from(String name, String attendStatus) {
return ChangeAttendStatusResponse.builder().name(name).attendStatus(attendStatus).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.blackcompany.eeos.attend.application.dto.converter;

import com.blackcompany.eeos.attend.application.dto.AttendInfoResponse;
import com.blackcompany.eeos.attend.application.dto.QueryAttendStatusResponse;
import java.util.List;
import org.springframework.stereotype.Component;

@Component
public class QueryAttendStatusResponseConverter {
public QueryAttendStatusResponse of(List<AttendInfoResponse> response) {
return QueryAttendStatusResponse.builder().members(response).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.blackcompany.eeos.attend.application.exception;

import com.blackcompany.eeos.common.exception.BusinessException;
import org.springframework.http.HttpStatus;

/** 해당 프로그램의 참석 대상자가 아닌데 참석 정보를 입력하려고 할 때 발생하는 예외 */
public class DeniedSaveAttendException extends BusinessException {
private static final String FAIL_CODE = "2005";

public DeniedSaveAttendException() {
super(FAIL_CODE, HttpStatus.NOT_FOUND);
}

@Override
public String getMessage() {
return String.format(" 프로그램의 참석 대상자가 아닙니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.blackcompany.eeos.attend.application.model;

import com.blackcompany.eeos.attend.application.exception.DeniedSaveAttendException;
import com.blackcompany.eeos.attend.application.exception.NotSameBeforeAttendStatusException;
import com.blackcompany.eeos.common.support.AbstractModel;
import lombok.AllArgsConstructor;
Expand All @@ -19,14 +20,26 @@ public class AttendModel implements AbstractModel {
private Long programId;
private AttendStatus status;

public void validateSame(String status) {
public void changeStatus(String beforeStatus, String afterStatus) {
validateChange(beforeStatus);
this.status = AttendStatus.findByAttendStatus(afterStatus);
}

private void validateChange(String beforeStatus) {
validateCanChange(beforeStatus);
validateSame(beforeStatus);
}

private void validateCanChange(String beforeStatus) {
if (AttendStatus.isSame(beforeStatus, AttendStatus.NONRELATED)) {
throw new DeniedSaveAttendException();
}
}

private void validateSame(String status) {
if (AttendStatus.isSame(status, this.status)) {
return;
}
throw new NotSameBeforeAttendStatusException(memberId);
}

public void changeStatus(String status) {
this.status = AttendStatus.findByAttendStatus(status);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.blackcompany.eeos.attend.application.service;

import com.blackcompany.eeos.attend.application.dto.AttendInfoResponse;
import com.blackcompany.eeos.attend.application.dto.ChangeAttendStatusRequest;
import com.blackcompany.eeos.attend.application.dto.ChangeAttendStatusResponse;
import com.blackcompany.eeos.attend.application.dto.QueryAttendStatusResponse;
import com.blackcompany.eeos.attend.application.dto.converter.AttendInfoConverter;
import com.blackcompany.eeos.attend.application.dto.converter.ChangeAttendStatusConverter;
import com.blackcompany.eeos.attend.application.dto.converter.QueryAttendStatusResponseConverter;
import com.blackcompany.eeos.attend.application.exception.NotFoundAttendException;
import com.blackcompany.eeos.attend.application.model.AttendModel;
import com.blackcompany.eeos.attend.application.model.AttendStatus;
import com.blackcompany.eeos.attend.application.model.converter.AttendEntityConverter;
import com.blackcompany.eeos.attend.application.usecase.ChangeAttendStatusUsecase;
import com.blackcompany.eeos.attend.application.usecase.GetAttendStatusUsecase;
import com.blackcompany.eeos.attend.application.usecase.GetAttendantInfoUsecase;
import com.blackcompany.eeos.attend.persistence.AttendEntity;
import com.blackcompany.eeos.attend.persistence.AttendRepository;
import com.blackcompany.eeos.member.application.model.MemberModel;
import com.blackcompany.eeos.member.application.model.converter.MemberEntityConverter;
import com.blackcompany.eeos.member.application.service.QueryMemberService;
import com.blackcompany.eeos.member.persistence.MemberEntity;
import com.blackcompany.eeos.member.persistence.MemberRepository;
import com.blackcompany.eeos.program.application.service.ProgramValidService;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AttendService
implements GetAttendantInfoUsecase, ChangeAttendStatusUsecase, GetAttendStatusUsecase {

private final AttendRepository attendRepository;

private final MemberRepository memberRepository;
private final ProgramValidService programValidService;
private final AttendInfoConverter infoConverter;
private final AttendEntityConverter attendEntityConverter;
private final QueryMemberService queryMemberService;
private final ChangeAttendStatusConverter changeAttendStatusConverter;
private final MemberEntityConverter memberEntityConverter;
private final AttendInfoConverter attendInfoConverter;
private final QueryAttendStatusResponseConverter attendStatusResponseConverter;

@Override
public List<AttendInfoResponse> findAttendInfo(final Long programId) {
programValidService.validate(programId);

return memberRepository.findMembersByProgramId(programId).stream()
.map(member -> infoConverter.from(member, getAttendStatus(member.getId(), programId)))
.collect(Collectors.toList());
}

@Override
public QueryAttendStatusResponse findAttendInfo(final Long programId, final String status) {
programValidService.validate(programId);

List<AttendEntity> attendsByProgram = findAttend(programId, status);
List<MemberModel> members =
findAMembers(programId, status).stream()
.map(memberEntityConverter::from)
.collect(Collectors.toList());

List<AttendInfoResponse> response =
members.stream()
.map(member -> combine(member, attendsByProgram, programId))
.collect(Collectors.toList());

return attendStatusResponseConverter.of(response);
}

@Transactional
@Override
public ChangeAttendStatusResponse changeStatus(
final Long memberId, final ChangeAttendStatusRequest request, final Long programId) {
AttendModel model = attendEntityConverter.from(getAttend(memberId, programId));

model.changeStatus(request.getBeforeAttendStatus(), request.getAfterAttendStatus());
AttendEntity updated = attendRepository.save(attendEntityConverter.toEntity(model));

String name = queryMemberService.getName(memberId);
return changeAttendStatusConverter.from(name, updated.getStatus().getStatus());
}

@Override
public ChangeAttendStatusResponse getStatus(Long memberId, Long programId) {
AttendModel model = attendEntityConverter.from(getAttend(memberId, programId));
String name = queryMemberService.getName(memberId);

return changeAttendStatusConverter.from(name, model.getStatus().getStatus());
}

private AttendEntity getAttend(final Long memberId, final Long programId) {
return attendRepository
.findByProgramIdAndMemberId(programId, memberId)
.orElseThrow(() -> new NotFoundAttendException(programId));
}

private AttendStatus getAttendStatus(final Long memberId, final Long programId) {
return getAttend(memberId, programId).getStatus();
}

private AttendInfoResponse combine(
MemberModel member, List<AttendEntity> attends, Long programId) {
AttendEntity attendEntity =
attends.stream()
.filter(attend -> member.validateSame(attend.getMemberId()))
.findAny()
.orElseThrow(() -> new NotFoundAttendException(programId));

return attendInfoConverter.from(member, attendEntity.getStatus());
}

private List<MemberEntity> findAMembers(final Long programId, final String status) {
List<Long> memberIds =
findAttend(programId, status).stream()
.map(AttendEntity::getMemberId)
.collect(Collectors.toList());

return memberRepository.findUsersByIds(memberIds);
}

private List<AttendEntity> findAttend(final Long programId, final String status) {
AttendStatus attendStatus = AttendStatus.findByAttendStatus(status);

return attendRepository.findAllByProgramIdAndStatus(programId, attendStatus);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.blackcompany.eeos.attend.application.service;

import com.blackcompany.eeos.program.application.dto.ChangeAttendStatusRequest;
import com.blackcompany.eeos.program.application.dto.ChangeAllAttendStatusRequest;
import com.blackcompany.eeos.program.application.dto.ProgramMembers;
import java.util.List;

/** 관련 있는 대상자를 선택한다. */
public interface CandidateService {
void saveCandidate(final Long programId, final List<ProgramMembers> members);

void updateCandidate(final Long programId, final List<ChangeAttendStatusRequest> requests);
void updateCandidate(final Long programId, final List<ChangeAllAttendStatusRequest> requests);
}
Loading

0 comments on commit da4c502

Please sign in to comment.