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

๐Ÿš€ ๋ฉ”์ดํŠธ ์ฐธ์—ฌ ์‚ญ์ œ API ๊ตฌํ˜„ #142

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand Down Expand Up @@ -70,4 +71,24 @@ public ResponseEntity<Void> acceptParticipant(
.build();
}

@DeleteMapping("/api/v1/mates/{id}/participants/{participantId}")
@Operation(summary = "๋ฉ”์ดํŠธ ์ฐธ์—ฌ ์š”์ฒญ ๊ฑฐ์ ˆ/์ทจ์†Œ")
@ApiResponse(
responseCode = "204",
description = "๋ฉ”์ดํŠธ ์ฐธ์—ฌ ์š”์ฒญ ๊ฑฐ์ ˆ/์ทจ์†Œ ์„ฑ๊ณต"
)
public ResponseEntity<Void> deleteById(
@AuthenticationPrincipal
Long userId,
@PathVariable
Long id,
@PathVariable
Long participantId
) {
mateService.deleteParticipantById(userId, id, participantId);

return ResponseEntity.noContent()
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package team.silvertown.masil.mate.service;

import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import org.locationtech.jts.geom.Point;
Expand Down Expand Up @@ -130,6 +131,22 @@ public void acceptParticipation(Long authorId, Long id, Long participantId) {
mateParticipant.acceptParticipant();
}

@Transactional
public void deleteParticipantById(Long userId, Long id, Long participantId) {
User user = userRepository.findById(userId)
.orElseThrow(getNotFoundException(MateErrorCode.USER_NOT_FOUND));
Optional<MateParticipant> mateParticipant = mateParticipantRepository.findById(
participantId);

if (mateParticipant.isEmpty()) {
return;
}

MateValidator.validateParticipantDeletion(user, id, mateParticipant.get());

mateParticipantRepository.delete(mateParticipant.get());
}

private Supplier<DataNotFoundException> getNotFoundException(ErrorCode errorCode) {
return () -> new DataNotFoundException(errorCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import team.silvertown.masil.mate.domain.Mate;
import team.silvertown.masil.mate.domain.MateParticipant;
import team.silvertown.masil.mate.exception.MateErrorCode;
import team.silvertown.masil.user.domain.User;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MateValidator extends Validator {
Expand Down Expand Up @@ -59,6 +60,22 @@ public static void validateParticipantAcceptance(
validateParticipantUnderMate(mateId, mate);
}

public static void validateParticipantDeletion(
User user,
Long mateId,
MateParticipant mateParticipant
) {
Mate mate = mateParticipant.getMate();

validateParticipantUnderMate(mateId, mate);

boolean isNotOwner = !user.equals(mateParticipant.getUser());

if (isNotOwner) {
validateAuthForManipulation(user.getId(), mate);
}
}

private static void validateParticipantUnderMate(
Long mateId,
Mate mate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand Down Expand Up @@ -619,4 +620,108 @@ private String getLastLatestCursor(int skipSize) {
.withMessage(MateErrorCode.PARTICIPATING_AROUND_SIMILAR_TIME.getMessage());
}

@Test
void ์‚ฌ์šฉ์ž๊ฐ€_๋ณธ์ธ์˜_๋ฉ”์ดํŠธ_์ฐธ์—ฌ์ž_์‚ญ์ œ๋ฅผ_์„ฑ๊ณตํ•œ๋‹ค() {
// given
Mate mate = mateRepository.save(MateTexture.createDependentMate(author, post));
User user = userRepository.save(UserTexture.createValidUser());
MateParticipant participant = mateParticipantRepository.save(
MateTexture.createMateParticipant(user, mate, ParticipantStatus.REQUESTED));

// when
mateService.deleteParticipantById(user.getId(), mate.getId(), participant.getId());

// then
Optional<MateParticipant> expected = mateParticipantRepository.findById(
participant.getId());

assertThat(expected).isEmpty();
}

@Test
void ๋ฉ”์ดํŠธ_์ž‘์„ฑ์ž๊ฐ€_๋‹ค๋ฅธ_์‚ฌ์šฉ์ž์˜_๋ฉ”์ดํŠธ_์ฐธ์—ฌ_์‚ญ์ œ๋ฅผ_์„ฑ๊ณตํ•œ๋‹ค() {
// given
Mate mate = mateRepository.save(MateTexture.createDependentMate(author, post));
User user = userRepository.save(UserTexture.createValidUser());
MateParticipant participant = mateParticipantRepository.save(
MateTexture.createMateParticipant(user, mate, ParticipantStatus.REQUESTED));

// when
mateService.deleteParticipantById(author.getId(), mate.getId(), participant.getId());

// then
Optional<MateParticipant> expected = mateParticipantRepository.findById(
participant.getId());

assertThat(expected).isEmpty();
}

@Test
void ์กด์žฌํ•˜์ง€_์•Š๋Š”_์ฐธ์—ฌ์ž๋ฉด_์˜ˆ์™ธ๋ฅผ_๋ฐœ์ƒ์‹œํ‚ค์ง€_์•Š๋Š”๋‹ค() {
// given
Mate mate = mateRepository.save(MateTexture.createDependentMate(author, post));
long invalidId = MateTexture.getRandomId();

// when
ThrowingCallable delete = () -> mateService.deleteParticipantById(author.getId(),
mate.getId(), invalidId);

// then
assertThatNoException().isThrownBy(delete);
}

@Test
void ๋กœ๊ทธ์ธ_์‚ฌ์šฉ์ž๊ฐ€_์—†์œผ๋ฉด_๋ฉ”์ดํŠธ_์ฐธ์—ฌ์ž_์‚ญ์ œ๋ฅผ_์‹คํŒจํ•œ๋‹ค() {
// given
Mate mate = mateRepository.save(MateTexture.createDependentMate(author, post));
User user = userRepository.save(UserTexture.createValidUser());
MateParticipant participant = mateParticipantRepository.save(
MateTexture.createMateParticipant(user, mate, ParticipantStatus.REQUESTED));
long invalidId = MateTexture.getRandomId();

// when
ThrowingCallable delete = () -> mateService.deleteParticipantById(invalidId, mate.getId(),
participant.getId());

// then
assertThatExceptionOfType(DataNotFoundException.class).isThrownBy(delete)
.withMessage(MateErrorCode.USER_NOT_FOUND.getMessage());
}

@Test
void ๋ฉ”์ดํŠธ์™€_์ฐธ์—ฌ์ž์˜_๋ฉ”์ดํŠธ๊ฐ€_๋‹ค๋ฅด๋ฉด_๋ฉ”์ดํŠธ_์ฐธ์—ฌ์ž_์‚ญ์ œ๋ฅผ_์‹คํŒจํ•œ๋‹ค() {
// given
Mate mate = mateRepository.save(MateTexture.createDependentMate(author, post));
User user = userRepository.save(UserTexture.createValidUser());
MateParticipant participant = mateParticipantRepository.save(
MateTexture.createMateParticipant(user, mate, ParticipantStatus.REQUESTED));
Mate anotherMate = mateRepository.save(MateTexture.createDependentMate(author, post));

// when
ThrowingCallable delete = () -> mateService.deleteParticipantById(user.getId(),
anotherMate.getId(), participant.getId());

// then
assertThatExceptionOfType(BadRequestException.class).isThrownBy(delete)
.withMessage(MateErrorCode.PARTICIPANT_MATE_NOT_MATCHING.getMessage());
}

@Test
void ๋กœ๊ทธ์ธํ•œ_์‚ฌ์šฉ์ž๊ฐ€_๋ฉ”์ดํŠธ_์ž‘์„ฑ์ž๋„_์•„๋‹ˆ๊ณ _๋ณธ์ธ๋„_์•„๋‹ˆ๋ฉด_์‚ญ์ œ๋ฅผ_์‹คํŒจํ•œ๋‹ค() {
// given
Mate mate = mateRepository.save(MateTexture.createDependentMate(author, post));
User user = userRepository.save(UserTexture.createValidUser());
MateParticipant participant = mateParticipantRepository.save(
MateTexture.createMateParticipant(user, mate, ParticipantStatus.REQUESTED));
User anotherUser = userRepository.save(UserTexture.createValidUser());

// when
ThrowingCallable delete = () -> mateService.deleteParticipantById(anotherUser.getId(),
mate.getId(), participant.getId());

//then
assertThatExceptionOfType(ForbiddenException.class).isThrownBy(delete)
.withMessage(MateErrorCode.USER_NOT_AUTHORIZED_FOR_MATE.getMessage());
}

}
Loading