Skip to content

Commit

Permalink
feat: 내 강아지 존재 유무 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ehtjsv2 committed Oct 4, 2024
1 parent 1169ba5 commit 2fc980c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.happy.friendogly.common.ApiResponse;
import com.happy.friendogly.pet.dto.request.SavePetRequest;
import com.happy.friendogly.pet.dto.request.UpdatePetRequest;
import com.happy.friendogly.pet.dto.response.FindExistMyPetResponse;
import com.happy.friendogly.pet.dto.response.FindPetResponse;
import com.happy.friendogly.pet.dto.response.SavePetResponse;
import com.happy.friendogly.pet.service.PetCommandService;
Expand Down Expand Up @@ -72,4 +73,10 @@ public void update(
) {
petCommandService.update(memberId, petId, request, image);
}

@GetMapping("/exists/mine")
public ApiResponse<FindExistMyPetResponse> existMine(@Auth Long memberId) {
FindExistMyPetResponse response = petQueryService.existMine(memberId);
return ApiResponse.ofSuccess(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.happy.friendogly.pet.dto.response;

public record FindExistMyPetResponse(
boolean isExistPet
) {

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

import com.happy.friendogly.exception.FriendoglyException;
import com.happy.friendogly.pet.domain.Pet;
import com.happy.friendogly.pet.dto.response.FindExistMyPetResponse;
import com.happy.friendogly.pet.dto.response.FindPetResponse;
import com.happy.friendogly.pet.repository.PetRepository;
import java.util.List;
Expand Down Expand Up @@ -30,4 +31,8 @@ public List<FindPetResponse> findByMemberId(Long memberId) {
.map(FindPetResponse::new)
.toList();
}

public FindExistMyPetResponse existMine(Long memberId) {
return new FindExistMyPetResponse(petRepository.existsByMemberId(memberId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName;
import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestPartFields;
import static org.springframework.restdocs.request.RequestDocumentation.partWithName;
Expand All @@ -18,6 +19,7 @@
import com.happy.friendogly.pet.domain.SizeType;
import com.happy.friendogly.pet.dto.request.SavePetRequest;
import com.happy.friendogly.pet.dto.request.UpdatePetRequest;
import com.happy.friendogly.pet.dto.response.FindExistMyPetResponse;
import com.happy.friendogly.pet.dto.response.FindPetResponse;
import com.happy.friendogly.pet.dto.response.SavePetResponse;
import com.happy.friendogly.pet.service.PetCommandService;
Expand Down Expand Up @@ -403,6 +405,37 @@ void update_Success() throws Exception {
);
}

@DisplayName("내 반려견 유무 조회 문서화")
@Test
void findExistMine() throws Exception {

FindExistMyPetResponse response = new FindExistMyPetResponse(true);

Mockito.when(petQueryService.existMine(anyLong()))
.thenReturn(response);

mockMvc.perform(RestDocumentationRequestBuilders.get("/pets/exists/mine")
.accept(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getMemberToken()))
.andExpect(status().isOk())
.andDo(MockMvcRestDocumentationWrapper.document("pet-exist-mine",
getDocumentRequest(),
getDocumentResponse(),
resource(ResourceSnippetParameters.builder()
.tag("Pet API")
.summary("내 반려견 유무 조회 API")
.requestHeaders(
headerWithName(HttpHeaders.AUTHORIZATION).description("로그인한 회원의 accessToken")
)
.responseFields(
fieldWithPath("isSuccess").type(JsonFieldType.BOOLEAN).description("요청 성공 여부"),
fieldWithPath("data.isExistPet").description("내 반려견 존재 유무")
)
.responseSchema(Schema.schema("FindExistMyPetResponse"))
.build()))
);
}

@Override
protected Object controller() {
return new PetController(petQueryService, petCommandService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.happy.friendogly.pet.domain.Gender;
import com.happy.friendogly.pet.domain.Pet;
import com.happy.friendogly.pet.domain.SizeType;
import com.happy.friendogly.pet.dto.response.FindExistMyPetResponse;
import com.happy.friendogly.pet.dto.response.FindPetResponse;
import com.happy.friendogly.pet.repository.PetRepository;
import com.happy.friendogly.support.ServiceTest;
Expand Down Expand Up @@ -75,4 +76,45 @@ void findByMemberId() {
() -> assertThat(responses.get(0).id()).isEqualTo(pet.getId())
);
}

@DisplayName("멤버가 가진 펫이 한마리라도 있는 지 알 수 있다 : False")
@Test
void existMineFalse() {
// given
Member member = memberRepository.save(
new Member("김도선", "tag1", "imageUrl")
);

// when
FindExistMyPetResponse response = petQueryService.existMine(member.getId());

// then
assertThat(response.isExistPet()).isFalse();
}

@DisplayName("멤버가 가진 펫이 한마리라도 있는 지 알 수 있다 : False")
@Test
void existMineTrue() {
// given
Member member = memberRepository.save(
new Member("김도선", "tag1", "imageUrl")
);
petRepository.save(
new Pet(
member,
"petName",
"description",
LocalDate.of(2023, 10, 4),
SizeType.LARGE,
Gender.FEMALE,
"imgaeUrl"
)
);

// when
FindExistMyPetResponse response = petQueryService.existMine(member.getId());

// then
assertThat(response.isExistPet()).isTrue();
}
}

0 comments on commit 2fc980c

Please sign in to comment.