adjusted so only available pets for the selected store is displayed when adopting

This commit is contained in:
Alex
2026-04-12 18:00:24 -06:00
parent 4eaf98834d
commit b493357f31
5 changed files with 24 additions and 20 deletions

View File

@@ -42,7 +42,7 @@ public interface PetApi {
Call<List<DropdownDTO>> getCustomerPets(@Path("customerId") Long customerId);
@GET("api/v1/dropdowns/adoption-pets")
Call<List<DropdownDTO>> getAdoptionPets();
Call<List<DropdownDTO>> getAdoptionPets(@Query("storeId") Long storeId);
@GET("api/v1/dropdowns/pets")
Call<List<DropdownDTO>> getPetDropdowns();

View File

@@ -43,8 +43,8 @@ public class PetRepository extends BaseRepository {
/**
* Retrieves a list of pets available for adoption from the dropdowns API.
*/
public LiveData<Resource<List<DropdownDTO>>> getAdoptionPets() {
return executeCall(petApi.getAdoptionPets());
public LiveData<Resource<List<DropdownDTO>>> getAdoptionPets(Long storeId) {
return executeCall(petApi.getAdoptionPets(storeId));
}
/**

View File

@@ -156,13 +156,9 @@ public class AdoptionDetailViewModel extends ViewModel {
}
private void loadAvailablePetsByStore(Long storeId) {
observeOnce(petRepository.getAvailablePetsByStore(storeId), r -> {
observeOnce(petRepository.getAdoptionPets(storeId), r -> {
if (r != null && r.status == Resource.Status.SUCCESS && r.data != null) {
List<DropdownDTO> dropdowns = new ArrayList<>();
for (com.example.petstoremobile.dtos.PetDTO pet : r.data.getContent()) {
dropdowns.add(new DropdownDTO(pet.getPetId(), pet.getPetName()));
}
petList.setValue(dropdowns);
petList.setValue(r.data);
}
});
}

View File

@@ -5,10 +5,7 @@ import com.petshop.backend.entity.User;
import com.petshop.backend.repository.*;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.Comparator;
import java.util.HashSet;
@@ -46,20 +43,20 @@ public class DropdownController {
}
@GetMapping("/pets")
public ResponseEntity<List<DropdownOption>> getPets() {
return ResponseEntity.ok(
petRepository.findAll().stream()
public ResponseEntity<List<DropdownOption>> getPets(@RequestParam(required = false) Long storeId) {
List<DropdownOption> pets = petRepository.findAll().stream()
.filter(p -> storeId == null || (p.getStore() != null && p.getStore().getStoreId().equals(storeId)))
.map(p -> new DropdownOption(p.getPetId(), p.getPetName()))
.sorted(Comparator.comparing(DropdownOption::getLabel, String.CASE_INSENSITIVE_ORDER))
.collect(Collectors.toList())
);
.collect(Collectors.toList());
return ResponseEntity.ok(pets);
}
@GetMapping("/adoption-pets")
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
public ResponseEntity<List<DropdownOption>> getAdoptionPets() {
public ResponseEntity<List<DropdownOption>> getAdoptionPets(@RequestParam(required = false) Long storeId) {
return ResponseEntity.ok(
petRepository.findAdoptablePetsOrderByPetNameAsc().stream()
petRepository.findAdoptablePetsByStore(storeId).stream()
.map(p -> new DropdownOption(p.getPetId(), p.getPetName()))
.collect(Collectors.toList())
);

View File

@@ -23,6 +23,17 @@ public interface PetRepository extends JpaRepository<Pet, Long> {
") " +
"ORDER BY p.petName ASC")
List<Pet> findAdoptablePetsOrderByPetNameAsc();
@Query("SELECT p FROM Pet p " +
"WHERE LOWER(p.petStatus) = 'available' " +
"AND (:storeId IS NULL OR p.store.storeId = :storeId) " +
"AND NOT EXISTS (" +
" SELECT 1 FROM Adoption a " +
" WHERE a.pet = p AND LOWER(a.adoptionStatus) = 'completed'" +
") " +
"ORDER BY p.petName ASC")
List<Pet> findAdoptablePetsByStore(@Param("storeId") Long storeId);
List<Pet> findAllByOwner_IdOrderByPetNameAsc(Long ownerId);
Optional<Pet> findByIdAndOwner_Id(Long id, Long ownerId);