Merge main branch
This commit is contained in:
@@ -50,7 +50,7 @@ public class AppointmentController {
|
||||
.orElse(null);
|
||||
|
||||
Long effectiveCustomerId = customerId;
|
||||
if (role != null && role.equals("CUSTOMER")) {
|
||||
if ("CUSTOMER".equals(role)) {
|
||||
User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
|
||||
effectiveCustomerId = user.getId();
|
||||
}
|
||||
@@ -88,7 +88,7 @@ public class AppointmentController {
|
||||
.map(authority -> authority.getAuthority().replace("ROLE_", ""))
|
||||
.orElse(null);
|
||||
|
||||
if (role != null && role.equals("CUSTOMER")) {
|
||||
if ("CUSTOMER".equals(role)) {
|
||||
User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
|
||||
if (!request.getCustomerId().equals(user.getId())) {
|
||||
throw new org.springframework.security.access.AccessDeniedException("You can only create appointments for yourself");
|
||||
|
||||
@@ -52,7 +52,7 @@ public class DropdownController {
|
||||
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
|
||||
public ResponseEntity<List<DropdownOption>> getAdoptionPets() {
|
||||
return ResponseEntity.ok(
|
||||
petRepository.findAllByPetStatusIgnoreCaseOrderByPetNameAsc("Available").stream()
|
||||
petRepository.findAdoptablePetsOrderByPetNameAsc().stream()
|
||||
.map(p -> new DropdownOption(p.getPetId(), p.getPetName()))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
|
||||
@@ -5,6 +5,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.data.core.PropertyReferenceException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
@@ -118,7 +119,9 @@ public class GlobalExceptionHandler {
|
||||
request.getRequestURI(),
|
||||
LocalDateTime.now()
|
||||
);
|
||||
return ResponseEntity.status(status).body(error);
|
||||
return ResponseEntity.status(status)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(error);
|
||||
}
|
||||
|
||||
private String buildDetails(Exception ex) {
|
||||
|
||||
@@ -15,6 +15,14 @@ import java.util.Optional;
|
||||
public interface PetRepository extends JpaRepository<Pet, Long> {
|
||||
|
||||
List<Pet> findAllByPetStatusIgnoreCaseOrderByPetNameAsc(String petStatus);
|
||||
@Query("SELECT p FROM Pet p " +
|
||||
"WHERE LOWER(p.petStatus) = 'available' " +
|
||||
"AND NOT EXISTS (" +
|
||||
" SELECT 1 FROM Adoption a " +
|
||||
" WHERE a.pet = p AND LOWER(a.adoptionStatus) = 'completed'" +
|
||||
") " +
|
||||
"ORDER BY p.petName ASC")
|
||||
List<Pet> findAdoptablePetsOrderByPetNameAsc();
|
||||
List<Pet> findAllByOwner_IdOrderByPetNameAsc(Long ownerId);
|
||||
Optional<Pet> findByIdAndOwner_Id(Long id, Long ownerId);
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ public class AdoptionService {
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + request.getSourceStoreId()))
|
||||
: null;
|
||||
String adoptionStatus = normalizeAdoptionStatus(request.getAdoptionStatus());
|
||||
validatePetAvailability(pet, null);
|
||||
validatePetAvailability(pet, null, null);
|
||||
|
||||
Adoption adoption = new Adoption();
|
||||
adoption.setPet(pet);
|
||||
@@ -111,7 +111,8 @@ public class AdoptionService {
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + request.getSourceStoreId()))
|
||||
: null;
|
||||
String adoptionStatus = normalizeAdoptionStatus(request.getAdoptionStatus());
|
||||
validatePetAvailability(pet, adoption.getAdoptionId());
|
||||
Long currentPetId = adoption.getPet() != null ? adoption.getPet().getPetId() : null;
|
||||
validatePetAvailability(pet, adoption.getAdoptionId(), currentPetId);
|
||||
|
||||
adoption.setPet(pet);
|
||||
adoption.setCustomer(customer);
|
||||
@@ -201,7 +202,8 @@ public class AdoptionService {
|
||||
throw new IllegalArgumentException("Adoption status must be Pending, Completed, or Cancelled");
|
||||
}
|
||||
|
||||
private void validatePetAvailability(Pet pet, Long adoptionId) {
|
||||
private void validatePetAvailability(Pet pet, Long adoptionId, Long currentPetId) {
|
||||
boolean samePetAsCurrentAdoption = currentPetId != null && currentPetId.equals(pet.getPetId());
|
||||
boolean adoptedElsewhere = adoptionId == null
|
||||
? adoptionRepository.existsByPet_IdAndAdoptionStatusIgnoreCase(pet.getPetId(), ADOPTION_STATUS_COMPLETED)
|
||||
: adoptionRepository.existsByPet_IdAndAdoptionStatusIgnoreCaseAndAdoptionIdNot(pet.getPetId(), ADOPTION_STATUS_COMPLETED, adoptionId);
|
||||
@@ -209,7 +211,7 @@ public class AdoptionService {
|
||||
throw new IllegalArgumentException("Selected pet has already been adopted");
|
||||
}
|
||||
|
||||
if (!PET_STATUS_AVAILABLE.equalsIgnoreCase(pet.getPetStatus()) && adoptionId == null) {
|
||||
if (!samePetAsCurrentAdoption && !PET_STATUS_AVAILABLE.equalsIgnoreCase(pet.getPetStatus())) {
|
||||
throw new IllegalArgumentException("Selected pet is not available for adoption");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +247,13 @@ public class PetService {
|
||||
if (principal instanceof AppPrincipal appPrincipal) {
|
||||
return new CurrentViewer(appPrincipal.getUserId(), appPrincipal.getRole());
|
||||
}
|
||||
return null;
|
||||
String username = authentication.getName();
|
||||
if (username == null || username.isBlank() || "anonymousUser".equalsIgnoreCase(username)) {
|
||||
return null;
|
||||
}
|
||||
return userRepository.findByUsername(username)
|
||||
.map(user -> new CurrentViewer(user.getId(), user.getRole()))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private Pet findPet(Long id) {
|
||||
|
||||
@@ -245,9 +245,6 @@ public class SaleService {
|
||||
}
|
||||
|
||||
String normalized = paymentMethod.trim();
|
||||
if (normalized.equalsIgnoreCase("Debit")) {
|
||||
return "Card";
|
||||
}
|
||||
if (normalized.equalsIgnoreCase("Cash")) {
|
||||
return "Cash";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user