In-Memory Data Flow
Since you’re not using a database yet, your application’s state lives in the server’s RAM. Incoming HTTP requests read from or write to a global Python dictionary:Setting Up the Pydantic Schemas
Good schema design separates incoming data (EmployeeCreate), partial updates (EmployeeUpdate), and the outgoing response (EmployeeOut):
The Complete CRUD Implementation
Create a file namedems_app.py in your project folder:
Running and Testing the API
1
Start the server
2
Open Swagger UI
Navigate to
http://127.0.0.1:8000/docs in your browser to see all five endpoints listed and interactive.3
Test the full CRUD lifecycle
- POST
/employees— add a new employee. You should receive a201 Createdresponse with the new record and its assignedid. - GET
/employees— verify the new employee appears in the list. - GET
/employees/1— retrieve a single employee by ID. - PUT
/employees/1— update the employee’s department. Only send the field(s) you want to change. - DELETE
/employees/2— remove an employee. You should receive a204 No Contentwith no body.