adjusted so only available pets for the selected store is displayed when adopting
This commit is contained in:
@@ -42,7 +42,7 @@ public interface PetApi {
|
|||||||
Call<List<DropdownDTO>> getCustomerPets(@Path("customerId") Long customerId);
|
Call<List<DropdownDTO>> getCustomerPets(@Path("customerId") Long customerId);
|
||||||
|
|
||||||
@GET("api/v1/dropdowns/adoption-pets")
|
@GET("api/v1/dropdowns/adoption-pets")
|
||||||
Call<List<DropdownDTO>> getAdoptionPets();
|
Call<List<DropdownDTO>> getAdoptionPets(@Query("storeId") Long storeId);
|
||||||
|
|
||||||
@GET("api/v1/dropdowns/pets")
|
@GET("api/v1/dropdowns/pets")
|
||||||
Call<List<DropdownDTO>> getPetDropdowns();
|
Call<List<DropdownDTO>> getPetDropdowns();
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ public class PetRepository extends BaseRepository {
|
|||||||
/**
|
/**
|
||||||
* Retrieves a list of pets available for adoption from the dropdowns API.
|
* Retrieves a list of pets available for adoption from the dropdowns API.
|
||||||
*/
|
*/
|
||||||
public LiveData<Resource<List<DropdownDTO>>> getAdoptionPets() {
|
public LiveData<Resource<List<DropdownDTO>>> getAdoptionPets(Long storeId) {
|
||||||
return executeCall(petApi.getAdoptionPets());
|
return executeCall(petApi.getAdoptionPets(storeId));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -156,13 +156,9 @@ public class AdoptionDetailViewModel extends ViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadAvailablePetsByStore(Long storeId) {
|
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) {
|
if (r != null && r.status == Resource.Status.SUCCESS && r.data != null) {
|
||||||
List<DropdownDTO> dropdowns = new ArrayList<>();
|
petList.setValue(r.data);
|
||||||
for (com.example.petstoremobile.dtos.PetDTO pet : r.data.getContent()) {
|
|
||||||
dropdowns.add(new DropdownDTO(pet.getPetId(), pet.getPetName()));
|
|
||||||
}
|
|
||||||
petList.setValue(dropdowns);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,7 @@ import com.petshop.backend.entity.User;
|
|||||||
import com.petshop.backend.repository.*;
|
import com.petshop.backend.repository.*;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -46,20 +43,20 @@ public class DropdownController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/pets")
|
@GetMapping("/pets")
|
||||||
public ResponseEntity<List<DropdownOption>> getPets() {
|
public ResponseEntity<List<DropdownOption>> getPets(@RequestParam(required = false) Long storeId) {
|
||||||
return ResponseEntity.ok(
|
List<DropdownOption> pets = petRepository.findAll().stream()
|
||||||
petRepository.findAll().stream()
|
.filter(p -> storeId == null || (p.getStore() != null && p.getStore().getStoreId().equals(storeId)))
|
||||||
.map(p -> new DropdownOption(p.getPetId(), p.getPetName()))
|
.map(p -> new DropdownOption(p.getPetId(), p.getPetName()))
|
||||||
.sorted(Comparator.comparing(DropdownOption::getLabel, String.CASE_INSENSITIVE_ORDER))
|
.sorted(Comparator.comparing(DropdownOption::getLabel, String.CASE_INSENSITIVE_ORDER))
|
||||||
.collect(Collectors.toList())
|
.collect(Collectors.toList());
|
||||||
);
|
return ResponseEntity.ok(pets);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/adoption-pets")
|
@GetMapping("/adoption-pets")
|
||||||
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
|
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
|
||||||
public ResponseEntity<List<DropdownOption>> getAdoptionPets() {
|
public ResponseEntity<List<DropdownOption>> getAdoptionPets(@RequestParam(required = false) Long storeId) {
|
||||||
return ResponseEntity.ok(
|
return ResponseEntity.ok(
|
||||||
petRepository.findAdoptablePetsOrderByPetNameAsc().stream()
|
petRepository.findAdoptablePetsByStore(storeId).stream()
|
||||||
.map(p -> new DropdownOption(p.getPetId(), p.getPetName()))
|
.map(p -> new DropdownOption(p.getPetId(), p.getPetName()))
|
||||||
.collect(Collectors.toList())
|
.collect(Collectors.toList())
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -23,6 +23,17 @@ public interface PetRepository extends JpaRepository<Pet, Long> {
|
|||||||
") " +
|
") " +
|
||||||
"ORDER BY p.petName ASC")
|
"ORDER BY p.petName ASC")
|
||||||
List<Pet> findAdoptablePetsOrderByPetNameAsc();
|
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);
|
List<Pet> findAllByOwner_IdOrderByPetNameAsc(Long ownerId);
|
||||||
Optional<Pet> findByIdAndOwner_Id(Long id, Long ownerId);
|
Optional<Pet> findByIdAndOwner_Id(Long id, Long ownerId);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user