Skip to content

Commit

Permalink
feat: 자신이 주인이 아닌 반려견과 모임에 참여하지 못하도록 검증 추가
Browse files Browse the repository at this point in the history
Co-authored-by: J-I-H-O <jeongjiho0731@gmail.com>
Co-authored-by: jimi567 <repday0609@gmail.com>
  • Loading branch information
J-I-H-O and jimi567 committed Aug 1, 2024
1 parent 05404ce commit 0119032
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.woowacourse.friendogly.club.dto.response.SaveClubMemberResponse;
import com.woowacourse.friendogly.club.dto.response.SaveClubResponse;
import com.woowacourse.friendogly.club.repository.ClubRepository;
import com.woowacourse.friendogly.exception.FriendoglyException;
import com.woowacourse.friendogly.infra.FileStorageManager;
import com.woowacourse.friendogly.member.domain.Member;
import com.woowacourse.friendogly.member.repository.MemberRepository;
Expand Down Expand Up @@ -40,7 +41,7 @@ public ClubCommandService(

public SaveClubResponse save(Long memberId, MultipartFile image, SaveClubRequest request) {
Member member = memberRepository.getById(memberId);
List<Pet> participatingPets = mapToPets(request.participatingPetsId());
List<Pet> participatingPets = mapToPets(request.participatingPetsId(), member);

String imageUrl = "";
if (image != null && !image.isEmpty()) {
Expand All @@ -67,16 +68,23 @@ public SaveClubMemberResponse joinClub(Long clubId, Long memberId, SaveClubMembe
Member member = memberRepository.getById(memberId);

club.addClubMember(member);
club.addClubPet(mapToPets(request.participatingPetsId()));
club.addClubPet(mapToPets(request.participatingPetsId(), member));

//TODO : 채팅방 ID 넘기기
return new SaveClubMemberResponse(1L);
}

private List<Pet> mapToPets(List<Long> participatingPetsId) {
return participatingPetsId.stream()
private List<Pet> mapToPets(List<Long> participatingPetsId, Member member) {
List<Pet> participatingPets = participatingPetsId.stream()
.map(petRepository::getById)
.toList();
boolean isNotOwner = participatingPets.stream()
.anyMatch(pet -> !pet.getMember().getId().equals(member.getId()));

if (isNotOwner) {
throw new FriendoglyException("자신의 반려견만 모임에 데려갈 수 있습니다.");
}
return participatingPets;
}

public void deleteClubMember(Long clubId, Long memberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void save() {
@DisplayName("회원이 모임에 참여한다.")
@Transactional
@Test
void saveClubMember() {
void joinClub() {
Club savedClub = createSavedClub(
savedMember,
savedPet,
Expand All @@ -91,7 +91,7 @@ void saveClubMember() {

@DisplayName("이미 참여한 모임에는 참여할 수 없다.")
@Test
void saveClubMember_FailAlreadyParticipating() {
void joinClub_FailAlreadyParticipating() {
Club savedClub = createSavedClub(
savedMember,
savedPet,
Expand All @@ -107,7 +107,7 @@ void saveClubMember_FailAlreadyParticipating() {

@DisplayName("참여 가능한 강아지가 없다면 참여할 수 없다.")
@Test
void saveClubMember_FailCanNotParticipate() {
void joinClub_FailCanNotParticipate() {
Club savedClub = createSavedClub(
savedMember,
savedPet,
Expand Down Expand Up @@ -140,6 +140,40 @@ void saveClubMember_FailCanNotParticipate() {
.hasMessage("모임에 데려갈 수 없는 강아지가 있습니다.");
}

@DisplayName("자신이 주인이 아닌 반려견을 모임에 참여시키는 경우 예외가 발생한다.")
@Test
void joinClub_FailUnMatchOwner() {
Club savedClub = createSavedClub(
savedMember,
savedPet,
Set.of(Gender.FEMALE, Gender.FEMALE_NEUTERED),
Set.of(SizeType.SMALL)
);
Member newMember = Member.builder()
.name("위브")
.email("wiib@gmail.com")
.tag("tag123")
.build();
Member savedNewMember = memberRepository.save(newMember);
Pet savedNewMemberPet = petRepository.save(
Pet.builder()
.name("땡순이")
.description("귀여워요")
.member(savedMember)
.birthDate(LocalDate.of(2020, 12, 1))
.gender(Gender.FEMALE)
.sizeType(SizeType.SMALL)
.imageUrl("https://image.com")
.build()
);

SaveClubMemberRequest request = new SaveClubMemberRequest(List.of(savedNewMemberPet.getId()));

assertThatThrownBy(() -> clubCommandService.joinClub(savedClub.getId(), savedNewMember.getId(), request))
.isInstanceOf(FriendoglyException.class)
.hasMessage("자신의 반려견만 모임에 데려갈 수 있습니다.");
}

//영속성 컨텍스트를 프로덕션 코드와 통합시키기 위해 트랜잭셔널 추가
@DisplayName("참여 중인 회원을 삭제하고, 방장이면 방장을 위임한다.")
@Transactional
Expand Down

0 comments on commit 0119032

Please sign in to comment.