Filter appointments and adoptions by customer
This commit is contained in:
@@ -10,6 +10,8 @@ import org.springframework.data.domain.Pageable;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
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.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -27,13 +29,29 @@ public class AdoptionController {
|
|||||||
public ResponseEntity<Page<AdoptionResponse>> getAllAdoptions(
|
public ResponseEntity<Page<AdoptionResponse>> getAllAdoptions(
|
||||||
@RequestParam(required = false) String q,
|
@RequestParam(required = false) String q,
|
||||||
Pageable pageable) {
|
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}")
|
@GetMapping("/{id}")
|
||||||
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
|
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
|
||||||
public ResponseEntity<AdoptionResponse> getAdoptionById(@PathVariable Long id) {
|
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
|
@PostMapping
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import org.springframework.data.domain.Pageable;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
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.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -30,13 +32,29 @@ public class AppointmentController {
|
|||||||
public ResponseEntity<Page<AppointmentResponse>> getAllAppointments(
|
public ResponseEntity<Page<AppointmentResponse>> getAllAppointments(
|
||||||
@RequestParam(required = false) String q,
|
@RequestParam(required = false) String q,
|
||||||
Pageable pageable) {
|
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}")
|
@GetMapping("/{id}")
|
||||||
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
|
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
|
||||||
public ResponseEntity<AppointmentResponse> getAppointmentById(@PathVariable Long id) {
|
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
|
@PostMapping
|
||||||
|
|||||||
@@ -16,4 +16,12 @@ public interface AdoptionRepository extends JpaRepository<Adoption, Long> {
|
|||||||
"LOWER(a.customer.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
"LOWER(a.customer.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
||||||
"LOWER(a.pet.petName) LIKE LOWER(CONCAT('%', :q, '%'))")
|
"LOWER(a.pet.petName) LIKE LOWER(CONCAT('%', :q, '%'))")
|
||||||
Page<Adoption> searchAdoptions(@Param("q") String query, Pageable pageable);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,4 +27,13 @@ public interface AppointmentRepository extends JpaRepository<Appointment, Long>
|
|||||||
"LOWER(a.service.serviceName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
"LOWER(a.service.serviceName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
|
||||||
"LOWER(p.petName) LIKE LOWER(CONCAT('%', :q, '%'))")
|
"LOWER(p.petName) LIKE LOWER(CONCAT('%', :q, '%'))")
|
||||||
Page<Appointment> searchAppointments(@Param("q") String query, Pageable pageable);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,19 +28,34 @@ public class AdoptionService {
|
|||||||
this.customerRepository = customerRepository;
|
this.customerRepository = customerRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page<AdoptionResponse> getAllAdoptions(String query, Pageable pageable) {
|
public Page<AdoptionResponse> getAllAdoptions(String query, Pageable pageable, Long customerId) {
|
||||||
Page<Adoption> adoptions;
|
Page<Adoption> adoptions;
|
||||||
|
|
||||||
|
if (customerId != null) {
|
||||||
|
if (query != null && !query.trim().isEmpty()) {
|
||||||
|
adoptions = adoptionRepository.searchAdoptionsByCustomer(customerId, query, pageable);
|
||||||
|
} else {
|
||||||
|
adoptions = adoptionRepository.findByCustomerCustomerId(customerId, pageable);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if (query != null && !query.trim().isEmpty()) {
|
if (query != null && !query.trim().isEmpty()) {
|
||||||
adoptions = adoptionRepository.searchAdoptions(query, pageable);
|
adoptions = adoptionRepository.searchAdoptions(query, pageable);
|
||||||
} else {
|
} else {
|
||||||
adoptions = adoptionRepository.findAll(pageable);
|
adoptions = adoptionRepository.findAll(pageable);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return adoptions.map(this::mapToResponse);
|
return adoptions.map(this::mapToResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AdoptionResponse getAdoptionById(Long id) {
|
public AdoptionResponse getAdoptionById(Long id, Long customerId) {
|
||||||
Adoption adoption = adoptionRepository.findById(id)
|
Adoption adoption = adoptionRepository.findById(id)
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Adoption not found with id: " + 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);
|
return mapToResponse(adoption);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,19 +39,34 @@ public class AppointmentService {
|
|||||||
this.petRepository = petRepository;
|
this.petRepository = petRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page<AppointmentResponse> getAllAppointments(String query, Pageable pageable) {
|
public Page<AppointmentResponse> getAllAppointments(String query, Pageable pageable, Long customerId) {
|
||||||
Page<Appointment> appointments;
|
Page<Appointment> appointments;
|
||||||
|
|
||||||
|
if (customerId != null) {
|
||||||
|
if (query != null && !query.trim().isEmpty()) {
|
||||||
|
appointments = appointmentRepository.searchAppointmentsByCustomer(customerId, query, pageable);
|
||||||
|
} else {
|
||||||
|
appointments = appointmentRepository.findByCustomerCustomerId(customerId, pageable);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if (query != null && !query.trim().isEmpty()) {
|
if (query != null && !query.trim().isEmpty()) {
|
||||||
appointments = appointmentRepository.searchAppointments(query, pageable);
|
appointments = appointmentRepository.searchAppointments(query, pageable);
|
||||||
} else {
|
} else {
|
||||||
appointments = appointmentRepository.findAll(pageable);
|
appointments = appointmentRepository.findAll(pageable);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return appointments.map(this::mapToResponse);
|
return appointments.map(this::mapToResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AppointmentResponse getAppointmentById(Long id) {
|
public AppointmentResponse getAppointmentById(Long id, Long customerId) {
|
||||||
Appointment appointment = appointmentRepository.findById(id)
|
Appointment appointment = appointmentRepository.findById(id)
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Appointment not found with id: " + 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);
|
return mapToResponse(appointment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user