Filter appointments and adoptions by customer

This commit is contained in:
2026-03-08 15:38:52 -06:00
parent d7d294130f
commit 4394e96329
6 changed files with 97 additions and 14 deletions

View File

@@ -10,6 +10,8 @@ import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
@RestController
@@ -27,13 +29,29 @@ public class AdoptionController {
public ResponseEntity<Page<AdoptionResponse>> getAllAdoptions(
@RequestParam(required = false) String q,
Pageable pageable) {
return ResponseEntity.ok(adoptionService.getAllAdoptions(q, pageable));
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String role = authentication.getAuthorities().stream()
.findFirst()
.map(authority -> authority.getAuthority().replace("ROLE_", ""))
.orElse(null);
Long customerId = role != null && role.equals("CUSTOMER") ? 1L : null;
return ResponseEntity.ok(adoptionService.getAllAdoptions(q, pageable, customerId));
}
@GetMapping("/{id}")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<AdoptionResponse> getAdoptionById(@PathVariable Long id) {
return ResponseEntity.ok(adoptionService.getAdoptionById(id));
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String role = authentication.getAuthorities().stream()
.findFirst()
.map(authority -> authority.getAuthority().replace("ROLE_", ""))
.orElse(null);
Long customerId = role != null && role.equals("CUSTOMER") ? 1L : null;
return ResponseEntity.ok(adoptionService.getAdoptionById(id, customerId));
}
@PostMapping

View File

@@ -10,6 +10,8 @@ import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
@@ -30,13 +32,29 @@ public class AppointmentController {
public ResponseEntity<Page<AppointmentResponse>> getAllAppointments(
@RequestParam(required = false) String q,
Pageable pageable) {
return ResponseEntity.ok(appointmentService.getAllAppointments(q, pageable));
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String role = authentication.getAuthorities().stream()
.findFirst()
.map(authority -> authority.getAuthority().replace("ROLE_", ""))
.orElse(null);
Long customerId = role != null && role.equals("CUSTOMER") ? 1L : null;
return ResponseEntity.ok(appointmentService.getAllAppointments(q, pageable, customerId));
}
@GetMapping("/{id}")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<AppointmentResponse> getAppointmentById(@PathVariable Long id) {
return ResponseEntity.ok(appointmentService.getAppointmentById(id));
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String role = authentication.getAuthorities().stream()
.findFirst()
.map(authority -> authority.getAuthority().replace("ROLE_", ""))
.orElse(null);
Long customerId = role != null && role.equals("CUSTOMER") ? 1L : null;
return ResponseEntity.ok(appointmentService.getAppointmentById(id, customerId));
}
@PostMapping

View File

@@ -16,4 +16,12 @@ public interface AdoptionRepository extends JpaRepository<Adoption, Long> {
"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> findByCustomerCustomerId(Long customerId, Pageable pageable);
@Query("SELECT a FROM Adoption a WHERE a.customer.customerId = :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);
}

View File

@@ -27,4 +27,13 @@ public interface AppointmentRepository extends JpaRepository<Appointment, Long>
"LOWER(a.service.serviceName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(p.petName) LIKE LOWER(CONCAT('%', :q, '%'))")
Page<Appointment> searchAppointments(@Param("q") String query, Pageable pageable);
Page<Appointment> findByCustomerCustomerId(Long customerId, Pageable pageable);
@Query("SELECT DISTINCT a FROM Appointment a LEFT JOIN a.pets p WHERE a.customer.customerId = :customerId AND (" +
"LOWER(a.customer.firstName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(a.customer.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(a.service.serviceName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
"LOWER(p.petName) LIKE LOWER(CONCAT('%', :q, '%')))")
Page<Appointment> searchAppointmentsByCustomer(@Param("customerId") Long customerId, @Param("q") String query, Pageable pageable);
}

View File

@@ -28,19 +28,34 @@ public class AdoptionService {
this.customerRepository = customerRepository;
}
public Page<AdoptionResponse> getAllAdoptions(String query, Pageable pageable) {
public Page<AdoptionResponse> getAllAdoptions(String query, Pageable pageable, Long customerId) {
Page<Adoption> adoptions;
if (query != null && !query.trim().isEmpty()) {
adoptions = adoptionRepository.searchAdoptions(query, pageable);
if (customerId != null) {
if (query != null && !query.trim().isEmpty()) {
adoptions = adoptionRepository.searchAdoptionsByCustomer(customerId, query, pageable);
} else {
adoptions = adoptionRepository.findByCustomerCustomerId(customerId, pageable);
}
} else {
adoptions = adoptionRepository.findAll(pageable);
if (query != null && !query.trim().isEmpty()) {
adoptions = adoptionRepository.searchAdoptions(query, pageable);
} else {
adoptions = adoptionRepository.findAll(pageable);
}
}
return adoptions.map(this::mapToResponse);
}
public AdoptionResponse getAdoptionById(Long id) {
public AdoptionResponse getAdoptionById(Long id, Long customerId) {
Adoption adoption = adoptionRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Adoption not found with id: " + id));
if (customerId != null && !adoption.getCustomer().getCustomerId().equals(customerId)) {
throw new ResourceNotFoundException("You can only view your own adoptions");
}
return mapToResponse(adoption);
}

View File

@@ -39,19 +39,34 @@ public class AppointmentService {
this.petRepository = petRepository;
}
public Page<AppointmentResponse> getAllAppointments(String query, Pageable pageable) {
public Page<AppointmentResponse> getAllAppointments(String query, Pageable pageable, Long customerId) {
Page<Appointment> appointments;
if (query != null && !query.trim().isEmpty()) {
appointments = appointmentRepository.searchAppointments(query, pageable);
if (customerId != null) {
if (query != null && !query.trim().isEmpty()) {
appointments = appointmentRepository.searchAppointmentsByCustomer(customerId, query, pageable);
} else {
appointments = appointmentRepository.findByCustomerCustomerId(customerId, pageable);
}
} else {
appointments = appointmentRepository.findAll(pageable);
if (query != null && !query.trim().isEmpty()) {
appointments = appointmentRepository.searchAppointments(query, pageable);
} else {
appointments = appointmentRepository.findAll(pageable);
}
}
return appointments.map(this::mapToResponse);
}
public AppointmentResponse getAppointmentById(Long id) {
public AppointmentResponse getAppointmentById(Long id, Long customerId) {
Appointment appointment = appointmentRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Appointment not found with id: " + id));
if (customerId != null && !appointment.getCustomer().getCustomerId().equals(customerId)) {
throw new ResourceNotFoundException("You can only view your own appointments");
}
return mapToResponse(appointment);
}