Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 약속 나가기, 약속 삭제하기 API 구현 #97

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.kkumulkkum.server.annotation.IsParticipant;
import org.kkumulkkum.server.exception.MemberException;
import org.kkumulkkum.server.exception.code.MemberErrorCode;
import org.kkumulkkum.server.exception.ParticipantException;
import org.kkumulkkum.server.exception.code.ParticipantErrorCode;
import org.kkumulkkum.server.service.participant.ParticipantRetriever;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
Expand All @@ -25,7 +25,7 @@ public void checkUserInMeeting(JoinPoint joinPoint, IsParticipant checkUserInPro
Long userId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

if (!participantRetriever.existsByPromiseIdAndUserId(promiseId, userId)) {
throw new MemberException(MemberErrorCode.NOT_JOINED_MEMBER);
throw new ParticipantException(ParticipantErrorCode.NOT_JOINED_PROMISE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오... 모르고 있던 실수가 있었군요. 좋습니다!

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.annotation.IsMemberByPromiseId;
import org.kkumulkkum.server.annotation.IsParticipant;
import org.kkumulkkum.server.annotation.UserId;
import org.kkumulkkum.server.dto.participant.request.PreparationInfoDto;
import org.kkumulkkum.server.dto.participant.response.LateComersDto;
Expand Down Expand Up @@ -77,4 +78,14 @@ public ResponseEntity<LateComersDto> getLateComers(
) {
return ResponseEntity.ok().body(participantService.getLateComers(promiseId));
}

@IsParticipant(promiseIdParamIndex = 1)
@DeleteMapping("/v1/promises/{promiseId}/leave")
public ResponseEntity<Void> leavePromise(
@UserId final Long userId,
@PathVariable("promiseId") final Long promiseId
) {
participantService.leavePromise(userId, promiseId);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ public ResponseEntity<MainPromisesDto> getUpcomingPromise(
) {
return ResponseEntity.ok().body(promiseService.getUpcomingPromises(userId));
}

@IsParticipant(promiseIdParamIndex = 0)
@DeleteMapping("/v1/promises/{promiseId}")
public ResponseEntity<Void> deletePromise(
@PathVariable("promiseId") final Long promiseId
) {
promiseService.deletePromise(promiseId);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.kkumulkkum.server.service.participant;

import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.domain.Participant;
import org.kkumulkkum.server.repository.ParticipantRepository;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class ParticipantRemover {
Expand All @@ -13,4 +16,12 @@ public class ParticipantRemover {
public void deleteByMemberId(final Long memberId) {
participantRepository.deleteByMemberId(memberId);
}

public void deleteById(final Long id) {
participantRepository.deleteById(id);
}

public void deleteAll(List<Participant> participants) {
participantRepository.deleteAllInBatch(participants);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ParticipantService {
private final ParticipantRetriever participantRetriever;
private final ParticipantEditor participantEditor;
private final PromiseRetriever promiseRetriever;
private final ParticipantRemover participantRemover;
private final FcmService fcmService;

@Transactional
Expand Down Expand Up @@ -138,6 +139,15 @@ public LateComersDto getLateComers(final Long promiseId) {
);
}

@Transactional
public void leavePromise(
final Long userId,
final Long promiseId
) {
Participant participant = participantRetriever.findByPromiseIdAndUserId(promiseId, userId);
participantRemover.deleteById(participant.getId());
}

private boolean validateState(
final Participant participant,
final String status
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.kkumulkkum.server.service.promise;

import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.repository.PromiseRepository;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PromiseRemover {

private final PromiseRepository promiseRepository;

public void deleteById(final Long promiseId) {
promiseRepository.deleteById(promiseId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.kkumulkkum.server.exception.PromiseException;
import org.kkumulkkum.server.exception.code.PromiseErrorCode;
import org.kkumulkkum.server.service.member.MemberRetreiver;
import org.kkumulkkum.server.service.participant.ParticipantRemover;
import org.kkumulkkum.server.service.participant.ParticipantRetriever;
import org.kkumulkkum.server.service.participant.ParticipantSaver;
import org.kkumulkkum.server.service.userInfo.UserInfoRetriever;
Expand All @@ -32,6 +33,8 @@ public class PromiseService {
private final UserInfoRetriever userInfoRetriever;
private final EntityManager entityManager;
private final MemberRetreiver memberRetreiver;
private final PromiseRemover promiseRemover;
private final ParticipantRemover participantRemover;

@Transactional
public PromiseAddDto createPromise(
Expand Down Expand Up @@ -144,6 +147,18 @@ public MainPromisesDto getUpcomingPromises(final Long userId) {
return MainPromisesDto.from(promiseRetriever.findUpcomingPromises(userId, 4));
}

@Transactional
public void deletePromise(final Long promiseId) {
Promise promise = promiseRetriever.findById(promiseId);

List<Participant> participants = participantRetriever.findAllByPromiseId(promise.getId());
// 약속에 속한 모든 참가자 삭제
participantRemover.deleteAll(participants);

// 약속 삭제
promiseRemover.deleteById(promise.getId());
}

private void updateUserInfo(
final Participant participant,
final LocalDateTime promiseTime
Expand Down
Loading