Skip to content

Commit

Permalink
✨ join시 participantCount 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ashlovesliitea committed Feb 19, 2023
1 parent f8e2459 commit a3a1081
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Query;
import yapp.allround3.member.domain.Member;
import yapp.allround3.participant.domain.Participant;
Expand All @@ -21,8 +20,12 @@ public interface ParticipantRepository extends JpaRepository<Participant, Long>
List<Participant> findParticipantsByProject(Project project);

int countParticipantByProject(Project project);


int countParticipantByProjectId(Long projectId);
Optional<Participant> findParticipantByProjectAndMember(Project project,Member member);


@Query("select f.participant as p from Feedback f where f.task=:task")
List<Participant> findParticipantsGivenFeedback(Task task);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,115 +10,118 @@
import yapp.allround3.member.domain.Member;
import yapp.allround3.member.service.MemberService;
import yapp.allround3.participant.controller.dto.ParticipantFeedbackResponse;
import yapp.allround3.participant.controller.dto.ParticipantResponse;
import yapp.allround3.participant.domain.Participant;
import yapp.allround3.participant.repository.ParticipantRepository;
import yapp.allround3.project.domain.Project;
import yapp.allround3.project.service.ProjectService;
import yapp.allround3.task.domain.Task;
import yapp.allround3.task.repository.TaskRepository;
import yapp.allround3.task.service.TaskService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;


@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ParticipantService {

private final MemberService memberService;
private final ParticipantRepository participantRepository;
private final ProjectService projectService;
private final FeedbackRepository feedbackRepository;
private final TaskRepository taskRepository;
private final MemberService memberService;
private final ParticipantRepository participantRepository;
private final ProjectService projectService;
private final FeedbackRepository feedbackRepository;
private final TaskRepository taskRepository;

public List<Participant> findParticipantsByProject(Project project) {

return participantRepository.findParticipantsByProject(project);
}

public List<Participant> findParticipantsByProject(Project project) {
public List<Participant> findParticipantsByProjectId(Long projectId) {
Project project = projectService.findProjectById(projectId);

return participantRepository.findParticipantsByProject(project);
}
return participantRepository.findParticipantsByProject(project);
}

public List<Participant> findParticipantsByProjectId(Long projectId) {
Project project = projectService.findProjectById(projectId);
public Participant findParticipantById(Long id) {

return participantRepository.findParticipantsByProject(project);
}
return participantRepository.findParticipantById(id)
.orElseThrow(()->new CustomException("해당 task의 참여자가 아닙니다."));
}

public Participant findParticipantById(Long id) {
public int findParticipantCountByProject(Project project) {

return participantRepository.findParticipantById(id)
.orElseThrow(CustomException::new);
}
return participantRepository.countParticipantByProject(project);
}

public int findParticipantCountByProject(Project project) {
public int findParticipantCountByProjectId(Long projectId) {

return participantRepository.countParticipantByProject(project);
}
return participantRepository.countParticipantByProjectId(projectId);
}

public List<Participant> findParticipantsGivenFeedback(Task task) {
public List<Participant> findParticipantsGivenFeedback(Task task) {

return participantRepository.findParticipantsGivenFeedback(task);
}
return participantRepository.findParticipantsGivenFeedback(task);
}

@Transactional
public Participant joinProject(Long projectId, Long memberId) {
Member member = memberService.findMemberById(memberId);
Project project = projectService.findProjectById(projectId);
@Transactional
public Participant joinProject(Long projectId, Long memberId) {
Member member = memberService.findMemberById(memberId);
Project project = projectService.findProjectById(projectId);

Participant participant = Participant.from(project, member);
Participant participant = Participant.from(project, member);

if (isExistedParticipant(participant)) {
throw new CustomException("이미 가입되어 있는 멤버에요.");
}
if (isExistedParticipant(participant)) {
throw new CustomException("이미 가입되어 있는 멤버에요.");
}

return participantRepository.save(participant);
}
Participant savedParticipant = participantRepository.save(participant);
taskRepository.findTasksByProjectId(projectId)
.forEach(task -> taskRepository.updateTaskFeedbackRequiredPersonnel(task.getId()));
return savedParticipant;
}

@Transactional
public void changeLeader(Long memberId, Long participantId) {
Participant former = participantRepository.findById(participantId)
.orElseThrow(() -> new CustomException("해당 참여자가 존재하지 않습니다."));
@Transactional
public void changeLeader(Long memberId, Long participantId) {
Participant former = participantRepository.findById(participantId)
.orElseThrow(() -> new CustomException("해당 참여자가 존재하지 않습니다."));

Member member = memberService.findMemberById(memberId);
Project project = former.getProject();
Participant participant = participantRepository
.findParticipantByProjectAndMember(project, member)
.orElseThrow(() -> new CustomException("같은 프로젝트가 아닙니다."));
Member member = memberService.findMemberById(memberId);
Project project = former.getProject();
Participant participant = participantRepository
.findParticipantByProjectAndMember(project, member)
.orElseThrow(() -> new CustomException("같은 프로젝트가 아닙니다."));

participant.changeLeader(former);
}
participant.changeLeader(former);
}

public ParticipantFeedbackResponse findParticipantGroupByTask(Long taskId) {
Task task = taskRepository.findById(taskId)
.orElseThrow(() -> new CustomException("해당 테스크가 없습니다."));
public ParticipantFeedbackResponse findParticipantGroupByTask(Long taskId) {
Task task = taskRepository.findById(taskId)
.orElseThrow(() -> new CustomException("해당 테스크가 없습니다."));

Participant participant = task.getParticipant();
Project project = participant.getProject();
Participant participant = task.getParticipant();
Project project = participant.getProject();

List<Feedback> feedbacks = feedbackRepository.findByTask(task);
List<Participant> confirmedParticipants = feedbacks.stream()
.map(Feedback::getParticipant)
.toList();
List<Feedback> feedbacks = feedbackRepository.findByTask(task);
List<Participant> confirmedParticipants = feedbacks.stream()
.map(Feedback::getParticipant)
.toList();

List<Participant> unConfirmedParticipants = participantRepository.findParticipantsByProject(project);
unConfirmedParticipants.removeAll(confirmedParticipants);
unConfirmedParticipants.remove(participant);
List<Participant> unConfirmedParticipants = participantRepository.findParticipantsByProject(project);
unConfirmedParticipants.removeAll(confirmedParticipants);
unConfirmedParticipants.remove(participant);

return ParticipantFeedbackResponse.of(confirmedParticipants, unConfirmedParticipants);
}
return ParticipantFeedbackResponse.of(confirmedParticipants, unConfirmedParticipants);
}

public Participant findParticipantByProjectAndMember(Project project, Member member) {
return participantRepository.findParticipantByProjectAndMember(project, member)
.orElseThrow(() -> new CustomException("프로젝트에 참여한 멤버가 아닙니다."));
}
public Participant findParticipantByProjectAndMember(Project project, Member member) {
return participantRepository.findParticipantByProjectAndMember(project, member)
.orElseThrow(() -> new CustomException("프로젝트에 참여한 멤버가 아닙니다."));
}

private boolean isExistedParticipant(Participant participant) {
Member member = participant.getMember();
Project project = participant.getProject();
private boolean isExistedParticipant(Participant participant) {
Member member = participant.getMember();
Project project = participant.getProject();

return participantRepository.findByMemberAndProject(member, project)
.isPresent();
}
return participantRepository.findByMemberAndProject(member, project)
.isPresent();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package yapp.allround3.task.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import yapp.allround3.participant.domain.Participant;
import yapp.allround3.project.domain.Project;
import yapp.allround3.task.domain.Task;

import java.util.List;
Expand All @@ -12,4 +12,11 @@
public interface TaskRepository extends JpaRepository<Task, Long> {
List<Task> findTasksByParticipant(Participant participant);
Optional<Task> findTaskById(Long id);

@Modifying
@Query("update Task t set t.feedbackRequiredPersonnel=t.feedbackRequiredPersonnel+1 where t.id = :taskId")
void updateTaskFeedbackRequiredPersonnel(Long taskId);

@Query("select t from Task t where t.participant.project.id = :projectId")
List<Task> findTasksByProjectId(Long projectId);
}

0 comments on commit a3a1081

Please sign in to comment.