Skip to content

Commit

Permalink
[Feature] 멤버가 팀에서 제거될 때 관련 데이터 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
songhyeon99 committed May 6, 2024
1 parent 4af4b54 commit 1ee3df5
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ResponseEntity<SuccessResponse<Object>> deleteTeamMember(
@Positive(message = "삭제할 회원 식별자는 양수만 가능합니다.")
Long memberId
) {
teamUseCase.deleteMember(authResponse, memberId);
teamUseCase.deleteMember(memberId);
return ResponseEntity.ok().body(SuccessResponse.from(TEAM_MEMBER_DELETED));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package idorm.idormServer.calendar.adapter.out.persistence;

import java.util.List;

import org.springframework.stereotype.Component;

import idorm.idormServer.calendar.application.port.out.DeleteSleepoverCalendarPort;
import idorm.idormServer.calendar.application.port.out.DeleteTeamMemberPort;
import idorm.idormServer.calendar.application.port.out.DeleteTeamPort;
import idorm.idormServer.calendar.application.port.out.LoadTeamCalendarPort;
import idorm.idormServer.calendar.application.port.out.LoadTeamPort;
import idorm.idormServer.calendar.entity.Team;
import idorm.idormServer.calendar.entity.TeamCalendar;
import idorm.idormServer.member.application.port.out.LoadMemberPort;
import idorm.idormServer.member.entity.Member;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class DeleteTeamMemberAdapter implements DeleteTeamMemberPort {

private final LoadMemberPort loadMemberPort;
private final LoadTeamPort loadTeamPort;
private final LoadTeamCalendarPort loadTeamCalendarPort;
private final DeleteSleepoverCalendarPort deleteSleepoverCalendarPort;
private final DeleteTeamPort deleteTeamPort;

@Override
public void deleteTeamMember(Long memberId) {
final Member member = loadMemberPort.loadMember(memberId);
final Team team = loadTeamPort.findByMemberId(member.getId());
final List<TeamCalendar> teamCalendars = loadTeamCalendarPort.findByMemberId(member.getId());

deleteSleepoverCalendarPort.deleteByMemberId(memberId);
teamCalendars
.forEach(teamCalendar -> teamCalendar.deletePariticipant(member.getId()));
team.deleteMember(member);
if (team.getMembers().isEmpty()) {
deleteTeamPort.delete(team);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package idorm.idormServer.calendar.adapter.out.persistence;

import java.util.Optional;

import org.springframework.stereotype.Component;

import idorm.idormServer.calendar.adapter.out.exception.NotFoundTeamException;
import idorm.idormServer.calendar.application.port.out.LoadTeamPort;
import idorm.idormServer.calendar.entity.Team;

import java.util.Optional;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class LoadTeamAdapter implements LoadTeamPort {
Expand All @@ -24,7 +23,7 @@ public Optional<Team> findByMemberIdWithOptional(Long memberId) {

@Override
public Team findByMemberId(Long memberId) {
Team response = findByMemberId(memberId);
Team response = teamRepository.findByMemberId(memberId);
if (response == null) {
throw new NotFoundTeamException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package idorm.idormServer.calendar.adapter.out.persistence;

import java.time.YearMonth;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Component;

import idorm.idormServer.calendar.adapter.out.exception.NotFoundTeamCalendarException;
import idorm.idormServer.calendar.application.port.out.LoadTeamCalendarPort;
import idorm.idormServer.calendar.entity.Team;
import idorm.idormServer.calendar.entity.TeamCalendar;
import idorm.idormServer.notification.adapter.out.api.NotificationRequest;

import java.time.YearMonth;
import java.util.Collections;
import java.util.List;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class LoadTeamCalendarAdapter implements LoadTeamCalendarPort {
Expand All @@ -39,7 +38,7 @@ public TeamCalendar findByIdAndMemberId(Long teamCalendarId, Long memberId) {

@Override
public List<TeamCalendar> findByMemberId(Long memberId) {
List<TeamCalendar> responses = findByMemberId(memberId);
List<TeamCalendar> responses = teamCalendarRepository.findByMemberId(memberId);
return responses != null ? responses : Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public List<TeamCalendar> findByMemberId(Long memberId) {
return queryFactory
.select(teamCalendar)
.from(teamCalendar)
.join(teamCalendar.team, team)
.where(teamCalendar.participants.participants.any().memberId.eq(memberId))
.join(teamCalendar.participants.participants, participant)
.where(participant.memberId.eq(memberId))
.fetch();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public TeamCalendarResponse save(final AuthResponse authResponse, final SaveTeam
@Transactional
@Override
public TeamCalendarResponse update(final AuthResponse authResponse, final UpdateTeamCalendarRequest request) {
final TeamCalendar teamCalendar = loadTeamCalendarPort.findByIdAndMemberId(request.teamCalendarId(),
authResponse.getId());
final Team team = loadTeamPort.findByMemberId(authResponse.getId());
final TeamCalendar teamCalendar = loadTeamCalendarPort.findByIdAndTeamId(request.teamCalendarId(),
team.getId());

teamCalendar.update(team, request.title(), request.content(), request.getPeriod(), request.startTime(),
request.endTime(), request.targets());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package idorm.idormServer.calendar.application;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import idorm.idormServer.auth.application.port.in.dto.AuthResponse;
import idorm.idormServer.calendar.application.port.in.TeamUseCase;
import idorm.idormServer.calendar.application.port.in.dto.TeamParticipantResponse;
import idorm.idormServer.calendar.application.port.in.dto.TeamResponse;
import idorm.idormServer.calendar.application.port.out.DeleteTeamMemberPort;
import idorm.idormServer.calendar.application.port.out.DeleteTeamPort;
import idorm.idormServer.calendar.application.port.out.LoadSleepoverCalendarPort;
import idorm.idormServer.calendar.application.port.out.LoadTeamPort;
Expand All @@ -12,17 +21,8 @@
import idorm.idormServer.calendar.entity.Team;
import idorm.idormServer.member.application.port.out.LoadMemberPort;
import idorm.idormServer.member.entity.Member;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand All @@ -36,6 +36,7 @@ public class TeamService implements TeamUseCase {
private final LoadSleepoverCalendarPort loadSleepoverCalendarPort;

private final DeleteTeamPort deleteTeamPort;
private final DeleteTeamMemberPort deleteTeamMemberPort;

@Override
@Transactional
Expand All @@ -60,12 +61,9 @@ public void addTeamMember(final AuthResponse authResponse, final Long registerMe

@Override
@Transactional
public void deleteMember(final AuthResponse authResponse, final Long targetId) {
final Member loginMember = loadMemberPort.loadMember(authResponse.getId());
final Member deleteMember = loadMemberPort.loadMember(targetId);
final Team team = loadTeamPort.findByMemberId(loginMember.getId());
public void deleteMember(final Long targetId) {

team.deleteMember(deleteMember);
deleteTeamMemberPort.deleteTeamMember(targetId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface TeamUseCase {

void addTeamMember(AuthResponse authResponse, Long registerMemberId);

void deleteMember(AuthResponse authResponse, Long memberId);
void deleteMember(Long memberId);

void explodeTeam(AuthResponse authResponse);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package idorm.idormServer.calendar.application.port.out;

public interface DeleteTeamMemberPort {
void deleteTeamMember(final Long memberId);
}
2 changes: 2 additions & 0 deletions src/main/java/idorm/idormServer/calendar/entity/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void deleteMember(Member member) {
throw new NotFoundTeamMemberException();
}
member.removeTeam();
this.members.remove(member);
if (this.members.size() == 1) {
this.teamStatus = TeamStatus.ALONE;
}
Expand All @@ -74,6 +75,7 @@ public void delete(Member member) {
if (teamStatus.isNotAlone())
throw new CannotExplodeTeamException();
member.removeTeam();
this.members.remove(member);
}

private void validateParticipationInTeam(Member member) {
Expand Down

0 comments on commit 1ee3df5

Please sign in to comment.