Backend bug fixes #275
@@ -22,6 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class AdoptionService {
|
||||
@@ -159,15 +160,37 @@ public class AdoptionService {
|
||||
|
||||
@Transactional
|
||||
public void deleteAdoption(Long id) {
|
||||
if (!adoptionRepository.existsById(id)) {
|
||||
throw new ResourceNotFoundException("Adoption not found with id: " + id);
|
||||
}
|
||||
adoptionRepository.deleteById(id);
|
||||
Adoption adoption = adoptionRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Adoption not found with id: " + id));
|
||||
Pet pet = adoption.getPet();
|
||||
String status = adoption.getAdoptionStatus();
|
||||
adoptionRepository.delete(adoption);
|
||||
resetPetIfPending(pet, status);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void bulkDeleteAdoptions(BulkDeleteRequest request) {
|
||||
adoptionRepository.deleteAllById(request.getIds());
|
||||
List<Adoption> adoptions = adoptionRepository.findAllById(request.getIds());
|
||||
adoptionRepository.deleteAll(adoptions);
|
||||
for (Adoption adoption : adoptions) {
|
||||
resetPetIfPending(adoption.getPet(), adoption.getAdoptionStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private void resetPetIfPending(Pet pet, String deletedAdoptionStatus) {
|
||||
if (!ADOPTION_STATUS_PENDING.equalsIgnoreCase(deletedAdoptionStatus)) {
|
||||
return;
|
||||
}
|
||||
if (!PET_STATUS_PENDING.equalsIgnoreCase(pet.getPetStatus())) {
|
||||
return;
|
||||
}
|
||||
boolean completedElsewhere = adoptionRepository.existsByPet_IdAndAdoptionStatusIgnoreCase(
|
||||
pet.getPetId(), ADOPTION_STATUS_COMPLETED);
|
||||
if (!completedElsewhere) {
|
||||
pet.setPetStatus(PET_STATUS_AVAILABLE);
|
||||
pet.setOwner(null);
|
||||
petRepository.save(pet);
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeFilter(String value) {
|
||||
|
||||
Reference in New Issue
Block a user