fix auth and logic bugs

This commit is contained in:
2026-04-15 15:54:46 -06:00
parent 1ab4df0b81
commit c24d49fa5b
4 changed files with 8 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
package com.petshop.backend.controller; package com.petshop.backend.controller;
import com.petshop.backend.entity.User; import com.petshop.backend.entity.User;
import com.petshop.backend.exception.ResourceNotFoundException;
import com.petshop.backend.repository.UserRepository; import com.petshop.backend.repository.UserRepository;
import com.petshop.backend.service.EmailService; import com.petshop.backend.service.EmailService;
import com.petshop.backend.util.AuthenticationHelper; import com.petshop.backend.util.AuthenticationHelper;
@@ -33,7 +34,7 @@ public class ContactController {
@PostMapping @PostMapping
public ResponseEntity<Void> sendContactEmail(@Valid @RequestBody ContactRequest req) { public ResponseEntity<Void> sendContactEmail(@Valid @RequestBody ContactRequest req) {
Long userId = AuthenticationHelper.getAuthenticatedUserId(); Long userId = AuthenticationHelper.getAuthenticatedUserId();
User user = userRepository.findById(userId).orElseThrow(); User user = userRepository.findById(userId).orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + userId));
emailService.sendContactMessage(user, req.subject(), req.body()); emailService.sendContactMessage(user, req.subject(), req.body());
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }

View File

@@ -35,11 +35,13 @@ public class StoreController {
} }
@PostMapping @PostMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<StoreResponse> createStore(@Valid @RequestBody StoreRequest request) { public ResponseEntity<StoreResponse> createStore(@Valid @RequestBody StoreRequest request) {
return ResponseEntity.status(HttpStatus.CREATED).body(storeService.createStore(request)); return ResponseEntity.status(HttpStatus.CREATED).body(storeService.createStore(request));
} }
@PutMapping("/{id}") @PutMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<StoreResponse> updateStore( public ResponseEntity<StoreResponse> updateStore(
@PathVariable Long id, @PathVariable Long id,
@Valid @RequestBody StoreRequest request) { @Valid @RequestBody StoreRequest request) {
@@ -47,12 +49,14 @@ public class StoreController {
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> deleteStore(@PathVariable Long id) { public ResponseEntity<Void> deleteStore(@PathVariable Long id) {
storeService.deleteStore(id); storeService.deleteStore(id);
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();
} }
@DeleteMapping @DeleteMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> bulkDeleteStores(@Valid @RequestBody BulkDeleteRequest request) { public ResponseEntity<Void> bulkDeleteStores(@Valid @RequestBody BulkDeleteRequest request) {
storeService.bulkDeleteStores(request); storeService.bulkDeleteStores(request);
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();

View File

@@ -100,7 +100,7 @@ public class PetRequest {
Objects.equals(petSpecies, that.petSpecies) && Objects.equals(petSpecies, that.petSpecies) &&
Objects.equals(petBreed, that.petBreed) && Objects.equals(petBreed, that.petBreed) &&
Objects.equals(petAge, that.petAge) && Objects.equals(petAge, that.petAge) &&
petStatus == that.petStatus && Objects.equals(petStatus, that.petStatus) &&
Objects.equals(petPrice, that.petPrice); Objects.equals(petPrice, that.petPrice);
} }

View File

@@ -264,7 +264,7 @@ public class AppointmentService {
List<Appointment> pastBookedAppointments = appointmentRepository.findPastBookedAppointments(currentDate, currentTime); List<Appointment> pastBookedAppointments = appointmentRepository.findPastBookedAppointments(currentDate, currentTime);
for (Appointment appointment : pastBookedAppointments) { for (Appointment appointment : pastBookedAppointments) {
appointment.setAppointmentStatus("COMPLETED"); appointment.setAppointmentStatus("Completed");
appointmentRepository.save(appointment); appointmentRepository.save(appointment);
} }