Implement Update and Delete Endpoints in a FastAPI CRUD Service
Context
You are given a FastAPI project that already has working Create and Read endpoints and shared Pydantic models for request/response. Extend the service to complete CRUD by adding Update and Delete endpoints that are consistent with the existing code style and API contracts.
Assumptions (adjust to match the actual project):
-
The resource is an "Item" stored in memory or a repository abstraction.
-
Existing endpoints follow typical REST conventions:
-
POST /items → 201 Created with response body of the created item.
-
GET /items/{item_id} → 200 OK with item body, or 404 if not found.
-
The same Pydantic models are reused across endpoints (e.g., ItemCreate, ItemUpdate, ItemRead).
Requirements
-
Implement Update and Delete endpoints that match existing request/response schemas and status codes.
-
Preserve idempotency where applicable and return 404 vs 204 appropriately.
-
Validate inputs and handle exceptions cleanly (e.g., 422 for invalid input, 404 for not found).
-
Write unit tests covering all four endpoints (Create, Read, Update, Delete), including success and error paths.
-
Explain how you would approach this if you have never used FastAPI before, leveraging existing endpoints as templates and the FastAPI documentation.