Capstone Project - Student Management REST API
Problem Statement In this capstone project, you will build a Student Management System that exposes a set of RESTful APIs to manage student records. The application should store student information in a SQLite database using SQLAlchemy ORM and follow a clean, modular architecture. Throughout this project, you will implement the application layer by layer, separating database operations, business logic, and API endpoints. The application should support the following features:- Add a Student
- View All Students
- Search Students using query parameters
- View Student Details
- Update Student Details
- Delete a Student
Learning Objectives
After completing this project, you will be able to:- Build RESTful APIs using FastAPI.
- Organize applications using a modular project structure.
- Configure and use SQLite with SQLAlchemy ORM.
- Design database models using SQLAlchemy.
- Validate requests and responses using Pydantic Schemas.
- Separate database operations using the Repository Pattern.
- Implement business logic using the Service Layer.
- Handle application errors using global exception handlers.
- Perform CRUD operations on a persistent database.
- Search records using query parameters.
- Test REST APIs using Swagger UI.
Final Project Architecture
Request Flow
Development Approach
Rather than building the entire application at once, we will develop it incrementally. At the end of each step, we will run and verify the implementation before moving on to the next step. This approach makes debugging easier and helps us understand the purpose of every layer in the application. Each step consists of:- Objective
- Instructions
- Task
- Solution
- Run & Verify
Step 1: Create the Project and Git Repository
Objective Create a new FastAPI project directory, initialize Git, configure.gitignore, and prepare the development environment.
Instructions
- Create a new project directory named
student-management-api. - Navigate to the project directory.
- Initialize the project environment using uv.
- Initialize Git and set up
.gitignoreto avoid tracking virtual environments or databases.
.gitignore, and commit.
Solution
Solution
- The project directory has been created.
- The virtual environment is activated.
- A
pyproject.tomland.gitignorefiles are present. git statusreports a clean working tree after the first commit.
Step 2: Install Dependencies
Objective Install all the libraries required to build the Student Management REST API. Instructions Install the following dependencies:- FastAPI
- Uvicorn
- SQLAlchemy
- Email Validator
Solution
Solution
- All packages are installed successfully.
- The
pyproject.tomlfile contains the installed dependencies.
Step 3: Create the Project Structure
Objective Organize the application package directories to separate different layers. Instructions- Create the application package directory
app. - Create folders for
core,models,schemas,repositories,services, androutersunderapp. - Place empty
__init__.pyfiles inside each folder.
Solution
Solution
Project StructureCommit the project structure to Git:
Step 4: Configure the Database
Objective Configure SQLAlchemy so that the application can communicate with the SQLite database. Instructions Create adatabase.py file inside the core folder and implement:
- Database URL
- SQLAlchemy Engine
- Session Factory
- Declarative Base
- Database Dependency (
get_db())
Solution
Solution
File: Commit the changes to Git:
app/core/database.py- The project runs without import errors.
- The
database.pyfile has no syntax errors.
Step 5: Design the Database Schema
Objective Design the database schema required for the Student Management System before implementing the database models. Instructions Design a database table named students with the following columns:- Student ID
- Student Name
- Student Age
- Student Email Address
- Primary Key
- Data Types
- Required Fields
- Unique Constraints
Solution
Solution
Students Table
Database Schema
Step 6: Create the Student ORM Model
Objective Create the SQLAlchemy ORM model that maps the students table to a Python class. Instructions Create aStudent model in app/models/student.py that:
- Maps to the
studentstable. - Defines the required columns using SQLAlchemy 2.0
Mappedandmapped_column. - Inherits from the application’s
Baseclass.
Solution
Solution
File: Commit the model:
app/models/student.pymain.py:
- A new file named students.db is created.
- The students table exists in the database.
Step 7: Implement the Health Check Endpoint
Objective Implement a Health Check endpoint to verify that the application can receive and respond to HTTP requests. Instructions- Open the
main.pyfile. - Implement a Health Check endpoint.
- Verify the endpoint using Swagger UI.
Solution
Solution
File: Commit the entrypoint setup:
app/main.pyStep 8: Create the Pydantic Schemas
Objective Create Pydantic schemas to validate incoming requests and serialize outgoing responses. Instructions Create astudent.py file inside the schemas folder and implement:
StudentCreateStudentUpdateStudentResponse
- Name: Required, min 2 chars, max 50 chars.
- Age: Required, between 1 and 120.
- Email: Required, valid email format.
Solution
Solution
File: Commit the schemas:
app/schemas/student.pyStep 9: Implement the Repository Layer
Objective Abstract direct database operations away from business logic using the Repository Pattern. Instructions Createapp/repositories/student_repository.py and implement operations:
- Retrieve all students.
- Search students using query parameters.
- Retrieve a student by ID/Email.
- Create, update, and delete student records.
Solution
Solution
File: Commit the repository file:
app/repositories/student_repository.pyStep 10: Configure Global Exception Handling
Objective Define custom exception classes and globally catch errors inside the FastAPI application. Instructions- Create
app/core/exceptions.py. - Create
app/core/exception_handlers.py. - Define
AppException,StudentNotFoundError, and register global handler functions.
Solution
Solution
File: File: Commit exception modules:
app/core/exceptions.pyapp/core/exception_handlers.pyAppException.
Step 11: Implement the Service Layer
Objective Coordinate business operations and enforce domain validation rules inside the Service Layer. Instructions- Create
app/services/student_service.py. - Implement registration, retrieval, updating, and deletion operations.
- Throw custom exceptions like
AppExceptionwhen an email already exists.
Solution
Solution
File: Commit the service file:
app/services/student_service.pyStep 12: Implement the REST API using APIRouter
Objective Expose operations to users through API Routers usingAnnotated dependency injection.
Instructions
- Create
app/routers/student.py. - Map endpoints for creating, reading, updating, and deleting students.
- Inject database sessions cleanly using
Annotatedtypes.
Solution
Solution
File: Commit the router:
app/routers/student.pyStep 13: Integrate and Test the Student Management REST API
Objective Integrate your routers and global exception handlers in the main entrypoint and run testing verifications. Instructions- Update
app/main.py. - Include routes, registers exception handlers, and start your uvicorn development server.
Solution
Solution
File: Commit the final main configuration:
app/main.pyhttp://127.0.0.1:8000/docs to test registration, queries, and deletions.