adjusted so only available pets for the selected store is displayed when adopting
This commit is contained in:
@@ -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())
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user