- Create V5__create_orders_table.sql migration with orders table - UUID primary key, user_id FK to users, status CHECK constraint - Indexes on user_id and status columns - Add OrderStatus enum (PENDING_PAYMENT, PAID, LOOKUP_STARTED, SENT, DELIVERED, FAILED) - Add OrderStatusConverter for JPA VARCHAR persistence - Create Order entity with fields: id, userId, plate, template, letterText, status, amountPaid, trackingId, timestamps - Create OrderRepository with findByUserIdOrderByCreatedAtDesc and findByStatus queries - Create OrderService with createOrder (normalizes plate, sets PENDING_PAYMENT), getOrdersByUserId, getOrderById - Add OrderNotFoundException with 404 handler in GlobalExceptionHandler - Write OrderServiceTest with 8 unit tests covering status, UUID generation, plate normalization, and error handling
56 lines
2.3 KiB
Java
56 lines
2.3 KiB
Java
package se.bilhalsning.exception;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
import se.bilhalsning.dto.ErrorResponse;
|
|
|
|
@RestControllerAdvice
|
|
public class GlobalExceptionHandler {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
|
|
@ExceptionHandler(InvalidCredentialsException.class)
|
|
public ResponseEntity<ErrorResponse> handleInvalidCredentials(InvalidCredentialsException ex) {
|
|
return ResponseEntity
|
|
.status(HttpStatus.UNAUTHORIZED)
|
|
.body(new ErrorResponse(ex.getMessage()));
|
|
}
|
|
|
|
@ExceptionHandler(EmailAlreadyExistsException.class)
|
|
public ResponseEntity<ErrorResponse> handleEmailAlreadyExists(EmailAlreadyExistsException ex) {
|
|
return ResponseEntity
|
|
.status(HttpStatus.CONFLICT)
|
|
.body(new ErrorResponse("E-postadressen är redan registrerad"));
|
|
}
|
|
|
|
@ExceptionHandler(OrderNotFoundException.class)
|
|
public ResponseEntity<ErrorResponse> handleOrderNotFound(OrderNotFoundException ex) {
|
|
return ResponseEntity
|
|
.status(HttpStatus.NOT_FOUND)
|
|
.body(new ErrorResponse(ex.getMessage()));
|
|
}
|
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException ex) {
|
|
String message = ex.getBindingResult().getFieldErrors().stream()
|
|
.map(e -> e.getField() + ": " + e.getDefaultMessage())
|
|
.reduce((a, b) -> a + ", " + b)
|
|
.orElse("Ogiltig indata");
|
|
return ResponseEntity
|
|
.badRequest()
|
|
.body(new ErrorResponse(message));
|
|
}
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
public ResponseEntity<ErrorResponse> handleGeneral(Exception ex) {
|
|
log.error("Unhandled exception", ex);
|
|
return ResponseEntity
|
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
.body(new ErrorResponse("Ett internt fel uppstod"));
|
|
}
|
|
}
|