Skip to content

Commit

Permalink
refactor: 보호 동물 목록 조회 쿼리를 개선한다. (#440)
Browse files Browse the repository at this point in the history
* refactor: 보호 동물 목록 조회에 페치 조인을 추가한다.

* refactor: 보호 동물 목록 조회 쿼리를 개선한다.

* refactor: 보호 동물 목록 조회 서비스 로직이 호출하는 쿼리를 변경한다.

* refactor: 보호 동물 등록 서비스 메서드 파라미터를 개선한다.

* refactor: 중복된 클래스를 제거한다.

* refactor: 잘못된 변수명을 수정한다.
  • Loading branch information
hseong3243 authored Dec 17, 2023
1 parent 934766d commit 76ae039
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ public class AnimalController {
public ResponseEntity<RegisterAnimalResponse> registerAnimal(
@LoginUser Long volunteerId,
@RequestBody @Valid RegisterAnimalRequest registerAnimalRequest) {
RegisterAnimalResponse registerAnimalResponse = animalService.registerAnimal(volunteerId,
registerAnimalRequest);
RegisterAnimalResponse registerAnimalResponse = animalService.registerAnimal(
volunteerId,
registerAnimalRequest.name(),
registerAnimalRequest.birthDate(),
registerAnimalRequest.type(),
registerAnimalRequest.breed(),
registerAnimalRequest.gender(),
registerAnimalRequest.isNeutered(),
registerAnimalRequest.active(),
registerAnimalRequest.weight(),
registerAnimalRequest.information(),
registerAnimalRequest.imageUrls());
URI location = URI.create("/api/shelters/animals/" + registerAnimalResponse.animalId());
return ResponseEntity.created(location).body(registerAnimalResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.clova.anifriends.domain.animal.repository.response.FindAnimalsResult;
import com.clova.anifriends.domain.common.PageInfo;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;

public record FindAnimalsResponse(
Expand Down Expand Up @@ -42,14 +41,6 @@ public static FindAnimalResponse from(FindAnimalsResult animal) {

}

public static FindAnimalsResponse from(Page<Animal> pagination) {
PageInfo pageInfo = PageInfo.of(pagination.getTotalElements(), pagination.hasNext());
List<FindAnimalResponse> findAnimalByVolunteerResponses = pagination.get()
.map(FindAnimalResponse::from).toList();

return new FindAnimalsResponse(pageInfo, findAnimalByVolunteerResponses);
}

public static FindAnimalsResponse fromV2(Slice<FindAnimalsResult> animalsWithPagination,
Long count) {
PageInfo pageInfo = PageInfo.of(count, animalsWithPagination.hasNext());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Page<Animal> findAnimalsByShelter(
Pageable pageable
);

Page<Animal> findAnimals(
Page<FindAnimalsResult> findAnimals(
AnimalType type,
AnimalActive active,
AnimalNeuteredFilter neuteredFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Page<Animal> findAnimalsByShelter(
}

@Override
public Page<Animal> findAnimals(
public Page<FindAnimalsResult> findAnimals(
AnimalType type,
AnimalActive active,
AnimalNeuteredFilter neuteredFilter,
Expand All @@ -90,8 +90,22 @@ public Page<Animal> findAnimals(
AnimalSize size,
Pageable pageable
) {
List<Animal> animals = query.selectFrom(animal)
List<FindAnimalsResult> animals = query
.select(new QFindAnimalsResult(
animal.animalId,
animal.name.name,
animal.createdAt,
animal.shelter.name.name,
animal.shelter.addressInfo.address,
ExpressionUtils.as(
select(animalImage.imageUrl.max())
.from(animalImage)
.where(animalImage.animal.eq(animal))
, "animageImageUrl")
))
.from(animal)
.join(animal.shelter)
.leftJoin(animal.shelter.image)
.where(
animalTypeContains(type),
animalActiveContains(active),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.clova.anifriends.domain.animal.service;

import com.clova.anifriends.domain.animal.dto.response.FindAnimalsResponse;
import com.clova.anifriends.domain.animal.dto.response.FindAnimalsResponse.FindAnimalResponse;
import com.clova.anifriends.domain.animal.repository.response.FindAnimalsResult;
import com.clova.anifriends.domain.common.PageInfo;
import java.util.List;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
public final class AnimalMapper {

public static FindAnimalsResponse resultToResponse(Page<FindAnimalsResult> animalPage) {
List<FindAnimalResponse> content = animalPage
.map(result -> new FindAnimalResponse(
result.getAnimalId(),
result.getAnimalName(),
result.getShelterName(),
result.getShelterAddress(),
result.getAnimalImageUrl()
)).toList();
PageInfo pageInfo = PageInfo.of(animalPage.getTotalElements(), animalPage.hasNext());
return new FindAnimalsResponse(pageInfo, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import com.clova.anifriends.domain.animal.Animal;
import com.clova.anifriends.domain.animal.AnimalAge;
import com.clova.anifriends.domain.animal.AnimalSize;
import com.clova.anifriends.domain.animal.dto.request.RegisterAnimalRequest;
import com.clova.anifriends.domain.animal.dto.response.FindAnimalDetail;
import com.clova.anifriends.domain.animal.dto.response.FindAnimalsByShelterResponse;
import com.clova.anifriends.domain.animal.dto.response.FindAnimalsResponse;
import com.clova.anifriends.domain.animal.dto.response.RegisterAnimalResponse;
import com.clova.anifriends.domain.animal.exception.AnimalNotFoundException;
import com.clova.anifriends.domain.animal.mapper.AnimalMapper;
import com.clova.anifriends.domain.animal.repository.AnimalCacheRepository;
import com.clova.anifriends.domain.animal.repository.AnimalRepository;
import com.clova.anifriends.domain.animal.repository.response.FindAnimalsResult;
Expand Down Expand Up @@ -44,9 +42,30 @@ public class AnimalService {

@Transactional
public RegisterAnimalResponse registerAnimal(
Long shelterId, RegisterAnimalRequest registerAnimalRequest) {
Long shelterId,
String name,
LocalDate birthDate,
String type,
String breed,
String gender,
Boolean isNeutered,
String active,
Double weight,
String information,
List<String> imageUrls) {
Shelter shelter = getShelterById(shelterId);
Animal animal = AnimalMapper.toAnimal(shelter, registerAnimalRequest);
Animal animal = new Animal(
shelter,
name,
birthDate,
type,
breed,
gender,
isNeutered,
active,
weight,
information,
imageUrls);
animalRepository.save(animal);
animalCacheRepository.saveAnimal(animal);
animalCacheRepository.increaseTotalNumberOfAnimals();
Expand Down Expand Up @@ -94,7 +113,7 @@ public FindAnimalsResponse findAnimals(
AnimalGender gender,
AnimalSize size,
Pageable pageable) {
Page<Animal> animalsWithPagination = animalRepository.findAnimals(
Page<FindAnimalsResult> animalPage = animalRepository.findAnimals(
type,
active,
neuteredFilter,
Expand All @@ -103,8 +122,7 @@ public FindAnimalsResponse findAnimals(
size,
pageable
);

return FindAnimalsResponse.from(animalsWithPagination);
return AnimalMapper.resultToResponse(animalPage);
}

@Transactional(readOnly = true)
Expand Down
Loading

0 comments on commit 76ae039

Please sign in to comment.