Skip to content

Commit

Permalink
refactor: AnimalService 를 수정한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
pushedrumex committed Mar 12, 2024
1 parent a74f157 commit dda3948
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public FindAnimalsResponse findAnimalsV2(
Long animalId,
@PageableDefault() Pageable pageable
) {

if (isFirstPage(type, active, neuteredFilter, age, gender, size, createdAt, animalId)) {
return animalCacheRepository.findAnimals(pageable.getPageSize(),
animalCacheRepository.getTotalNumberOfAnimals());
Expand Down Expand Up @@ -172,8 +171,7 @@ public void updateAnimalAdoptStatus(Long shelterId, Long animalId, Boolean isAdo
Animal animal = getAnimalByAnimalIdAndShelterId(animalId, shelterId);
animal.updateAdoptStatus(isAdopted);
if (isAdopted == true) {
animalCacheRepository.deleteAnimal(animal);
animalCacheRepository.decreaseTotalNumberOfAnimals();
deleteFromCache(animal);
}
}

Expand All @@ -192,15 +190,18 @@ public void updateAnimal(
String information,
List<String> imageUrls
) {
Animal foundAnimal = getAnimalByAnimalIdAndShelterIdWithImages(animalId, shelterId);
animalCacheRepository.deleteAnimal(foundAnimal);
Animal animal = getAnimalByAnimalIdAndShelterIdWithImages(animalId, shelterId);
long number = animalCacheRepository.deleteAnimal(animal);

List<String> imagesToDelete = foundAnimal.findImagesToDelete(imageUrls);
List<String> imagesToDelete = animal.findImagesToDelete(imageUrls);
applicationEventPublisher.publishEvent(new ImageDeletionEvent(imagesToDelete));

foundAnimal.updateAnimal(name, birthDate, type, breed, gender, isNeutered, active, weight,
animal.updateAnimal(name, birthDate, type, breed, gender, isNeutered, active, weight,
information, imageUrls);
animalCacheRepository.saveAnimal(foundAnimal);

if (number > 0) {
animalCacheRepository.saveAnimal(animal);
}
}

@Transactional
Expand All @@ -209,6 +210,10 @@ public void deleteAnimal(Long shelterId, Long animalId) {
List<String> imagesToDelete = animal.getImages();
applicationEventPublisher.publishEvent(new ImageDeletionEvent(imagesToDelete));
animalRepository.delete(animal);
deleteFromCache(animal);
}

private void deleteFromCache(Animal animal) {
animalCacheRepository.deleteAnimal(animal);
animalCacheRepository.decreaseTotalNumberOfAnimals();
}
Expand Down

0 comments on commit dda3948

Please sign in to comment.