Response
Here is the complete source for invoice cancellation, following the house conventions (constructor injection, `ApiResponse` envelope, domain exceptions mapped by a `@RestControllerAdvice`).
```java
package com.acme.billing;
import java.util.Optional;
/** Assumed to already exist in the domain model. */
public interface Invoice {
long customerId();
InvoiceStatus status();
void markCancelled();
}
```
```java
package com.acme.billing;
/** Assumed to already exist in the domain model. */
public enum InvoiceStatus {
UNPAID,
PAID,
CANCELLED
}
```
```java
package com.acme.billing;
import java.util.Optional;
/** Assumed to already exist. */
public interface InvoiceRepository {
Optional<Invoice> findById(long id);
}
```
```java
package com.acme.billing;
public class InvoiceNotFoundException extends RuntimeException {
public InvoiceNotFoundException(long invoiceId) {
super("Invoice " + invoiceId + " not found");
}
}
```
```java
package com.acme.billing;
public class InvoiceAccessDeniedException extends RuntimeException {
public InvoiceAccessDeniedException(long invoiceId) {
super("Invoice " + invoiceId + " does not belong to the current customer");
}
}
```
```java
package com.acme.billing;
public class InvoiceNotCancellableException extends RuntimeException {
public InvoiceNotCancellableException(long invoiceId, InvoiceStatus status) {
super("Invoice " + invoiceId + " cannot be cancelled while in status " + status);
}
}
```
```java
package com.acme.billing;
public record ApiResponse<T>(boolean success, T data, String errorCode, String errorMessage) {
public static <T> ApiResponse<T> ok(T data) {
return new ApiResponse<>(true, data, null, null);
}
public static <T> ApiResponse<T> error(String code, String message) {
return new ApiResponse<>(false, null, code, message);
}
}
```
```java
package com.acme.billing;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class BillingExceptionHandler {
@ExceptionHandler(InvoiceNotFoundException.class)
public ResponseEntity<ApiResponse<Void>> handleNotFound(InvoiceNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ApiResponse.error("INVOICE_NOT_FOUND", ex.getMessage()));
}
@ExceptionHandler(InvoiceAccessDeniedException.class)
public ResponseEntity<ApiResponse<Void>> handleAccessDenied(InvoiceAccessDeniedException ex) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(ApiResponse.error("INVOICE_ACCESS_DENIED", ex.getMessage()));
}
@ExceptionHandler(InvoiceNotCancellableException.class)
public ResponseEntity<ApiResponse<Void>> handleNotCancellable(InvoiceNotCancellableException ex) {
return ResponseEntity.status(HttpStatus.CONFLICT)
.body(ApiResponse.error("INVOICE_NOT_CANCELLABLE", ex.getMessage()));
}
}
```
```java
package com.acme.billing;
import org.springframework.stereotype.Service;
@Service
public class InvoiceService {
private final InvoiceRepository invoiceRepository;
public InvoiceService(InvoiceRepository invoiceRepository) {
this.invoiceRepository = invoiceRepository;
}
public void cancelInvoice(long invoiceId, long customerId) {
Invoice invoice = invoiceRepository.findById(invoiceId)
.orElseThrow(() -> new InvoiceNotFoundException(invoiceId));
if (invoice.customerId() != customerId) {
throw new InvoiceAccessDeniedException(invoiceId);
}
if (invoice.status() != InvoiceStatus.UNPAID) {
throw new InvoiceNotCancellableException(invoiceId, invoice.status());
}
invoice.markCancelled();
}
}
```
```java
package com.acme.billing;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/invoices")
public class InvoiceController {
private final InvoiceService invoiceService;
public InvoiceController(InvoiceService invoiceService) {
this.invoiceService = invoiceService;
}
@PostMapping("/{invoiceId}/cancel")
public ApiResponse<Void> cancel(@PathVariable long invoiceId,
@RequestAttribute long customerId) {
invoiceService.cancelInvoice(invoiceId, customerId);
return ApiResponse.ok(null);
}
}
```
Convention notes: authorization (ownership) and state checks live in the service, not the controller, per the "two locks on the door" rule, and all failures are domain exceptions translated to `ApiResponse.error(...)` by a `@RestControllerAdvice` rather than try/catch in the controller. `Invoice`, `InvoiceStatus`, and `InvoiceRepository` are printed as minimal stubs only so the listing compiles — treat them as the pre-existing types the prompt described, not new design.
One open question: the controller pulls `customerId` from a `@RequestAttribute`, assuming some upstream filter/security layer sets it — adjust that extraction to match however your app actually resolves the current customer.