fixed creating adoption for the backend and implemented adoption to andriod for changes
This commit is contained in:
@@ -33,6 +33,8 @@ public class AdoptionController {
|
||||
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
|
||||
public ResponseEntity<Page<AdoptionResponse>> getAllAdoptions(
|
||||
@RequestParam(required = false) String q,
|
||||
@RequestParam(required = false) Long customerId,
|
||||
@RequestParam(required = false) String status,
|
||||
Pageable pageable) {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
String role = authentication.getAuthorities().stream()
|
||||
@@ -40,13 +42,13 @@ public class AdoptionController {
|
||||
.map(authority -> authority.getAuthority().replace("ROLE_", ""))
|
||||
.orElse(null);
|
||||
|
||||
Long customerId = null;
|
||||
Long effectiveCustomerId = customerId;
|
||||
if (role != null && role.equals("CUSTOMER")) {
|
||||
User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
|
||||
customerId = user.getId();
|
||||
effectiveCustomerId = user.getId();
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(adoptionService.getAllAdoptions(q, pageable, customerId));
|
||||
return ResponseEntity.ok(adoptionService.getAllAdoptions(q, effectiveCustomerId, status, pageable));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
||||
@@ -14,22 +14,24 @@ import java.util.Optional;
|
||||
public interface AdoptionRepository extends JpaRepository<Adoption, Long> {
|
||||
|
||||
@Query("SELECT a FROM Adoption a WHERE " +
|
||||
"(:q IS NULL OR (" +
|
||||
"LOWER(a.customer.firstName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
||||
"LOWER(a.customer.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
||||
"LOWER(a.pet.petName) LIKE LOWER(CONCAT('%', :q, '%'))")
|
||||
Page<Adoption> searchAdoptions(@Param("q") String query, Pageable pageable);
|
||||
|
||||
Page<Adoption> findByCustomerId(Long customerId, Pageable pageable);
|
||||
|
||||
@Query("SELECT a FROM Adoption a WHERE a.customer.id = :customerId AND (" +
|
||||
"LOWER(a.customer.firstName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
||||
"LOWER(a.customer.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
||||
"LOWER(a.pet.petName) LIKE LOWER(CONCAT('%', :q, '%')))")
|
||||
Page<Adoption> searchAdoptionsByCustomer(@Param("customerId") Long customerId, @Param("q") String query, Pageable pageable);
|
||||
"LOWER(a.pet.petName) LIKE LOWER(CONCAT('%', :q, '%'))" +
|
||||
")) AND " +
|
||||
"(:customerId IS NULL OR a.customer.id = :customerId) AND " +
|
||||
"(:status IS NULL OR LOWER(a.adoptionStatus) = LOWER(:status))")
|
||||
Page<Adoption> searchAdoptions(
|
||||
@Param("q") String query,
|
||||
@Param("customerId") Long customerId,
|
||||
@Param("status") String status,
|
||||
Pageable pageable);
|
||||
|
||||
Optional<Adoption> findFirstByPet_IdAndAdoptionStatusOrderByAdoptionDateDesc(Long petId, String adoptionStatus);
|
||||
|
||||
boolean existsByPetPetIdAndAdoptionStatusIgnoreCaseAndAdoptionIdNot(Long petId, String adoptionStatus, Long adoptionId);
|
||||
@Query("SELECT CASE WHEN COUNT(a) > 0 THEN true ELSE false END FROM Adoption a WHERE a.pet.id = :petId AND LOWER(a.adoptionStatus) = LOWER(:adoptionStatus) AND a.adoptionId <> :adoptionId")
|
||||
boolean existsByPetPetIdAndAdoptionStatusIgnoreCaseAndAdoptionIdNot(@Param("petId") Long petId, @Param("adoptionStatus") String adoptionStatus, @Param("adoptionId") Long adoptionId);
|
||||
|
||||
boolean existsByPetPetIdAndAdoptionStatusIgnoreCase(Long petId, String adoptionStatus);
|
||||
@Query("SELECT CASE WHEN COUNT(a) > 0 THEN true ELSE false END FROM Adoption a WHERE a.pet.id = :petId AND LOWER(a.adoptionStatus) = LOWER(:adoptionStatus)")
|
||||
boolean existsByPetPetIdAndAdoptionStatusIgnoreCase(@Param("petId") Long petId, @Param("adoptionStatus") String adoptionStatus);
|
||||
}
|
||||
|
||||
@@ -38,22 +38,16 @@ public class AdoptionService {
|
||||
this.storeRepository = storeRepository;
|
||||
}
|
||||
|
||||
public Page<AdoptionResponse> getAllAdoptions(String query, Pageable pageable, Long customerId) {
|
||||
Page<Adoption> adoptions;
|
||||
public Page<AdoptionResponse> getAllAdoptions(String query, Long customerId, String status, Pageable pageable) {
|
||||
String normalizedQuery = normalizeFilter(query);
|
||||
String normalizedStatus = normalizeFilter(status);
|
||||
|
||||
if (customerId != null) {
|
||||
if (query != null && !query.trim().isEmpty()) {
|
||||
adoptions = adoptionRepository.searchAdoptionsByCustomer(customerId, query, pageable);
|
||||
} else {
|
||||
adoptions = adoptionRepository.findByCustomerId(customerId, pageable);
|
||||
}
|
||||
} else {
|
||||
if (query != null && !query.trim().isEmpty()) {
|
||||
adoptions = adoptionRepository.searchAdoptions(query, pageable);
|
||||
} else {
|
||||
adoptions = adoptionRepository.findAll(pageable);
|
||||
}
|
||||
}
|
||||
Page<Adoption> adoptions = adoptionRepository.searchAdoptions(
|
||||
normalizedQuery,
|
||||
customerId,
|
||||
normalizedStatus,
|
||||
pageable
|
||||
);
|
||||
|
||||
return adoptions.map(this::mapToResponse);
|
||||
}
|
||||
@@ -140,6 +134,14 @@ public class AdoptionService {
|
||||
adoptionRepository.deleteAllById(request.getIds());
|
||||
}
|
||||
|
||||
private String normalizeFilter(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
String trimmed = value.trim();
|
||||
return trimmed.isEmpty() ? null : trimmed;
|
||||
}
|
||||
|
||||
private AdoptionResponse mapToResponse(Adoption adoption) {
|
||||
StoreLocation sourceStore = adoption.getSourceStore();
|
||||
return new AdoptionResponse(
|
||||
|
||||
Reference in New Issue
Block a user