Skip to content

About

A Backend REST API for Managing Student EnrollmentsStudent Enrollment System is a FastAPI application that handles students, courses, and enrollments through clean CRUD endpoints, backed by SQLAlchemy and SQLite.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 FastAPI Mentorship Project

Tech Mentorship Session — Basics & CRUD Operations


📁 Project Structure

fastapi-mentorship/
│
├── app/
│   ├── main.py               # FastAPI app entry point
│   ├── database/
│   │   └── db.py             # DB engine, session, get_db dependency
│   ├── models/
│   │   └── models.py         # SQLAlchemy ORM models (DB tables)
│   ├── schemas/
│   │   └── schemas.py        # Pydantic schemas (request/response validation)
│   └── routers/
│       ├── students.py       # Student CRUD endpoints
│       ├── courses.py        # Course CRUD endpoints
│       └── enrollments.py    # Enrollment CRUD endpoints
│
├── seed.py                   # Script to populate DB with demo data
├── requirements.txt          # Python dependencies
└── README.md                 # You are here!

⚙️ Setup & Run

1. Create a virtual environment

python -m venv venv
source venv/bin/activate        # Linux/macOS
venv\Scripts\activate           # Windows

2. Install dependencies

pip install -r requirements.txt

3. Run the server

uvicorn app.main:app --reload

4. Seed sample data (optional)

python seed.py

5. Open the API docs


🧩 Core Concepts Covered

1. FastAPI Basics

Concept Where to find it
App creation & metadata app/main.py
Route decorators (@router.get, @router.post, etc.) All routers
Path parameters (/{id}) All routers
Query parameters (skip, limit) students.py, courses.py
HTTP status codes All routers
Dependency injection (Depends) All routers

2. CRUD Operations

Operation HTTP Method Example Endpoint
Create POST POST /api/v1/students
Read GET GET /api/v1/students
Read one GET GET /api/v1/students/1
Update PUT PUT /api/v1/students/1
Delete DELETE DELETE /api/v1/students/1

3. Project Architecture Layers

Request → Router (HTTP) → Schema (Validate) → Model (DB Query) → Response

📌 API Endpoints Reference

👨‍🎓 Students

Method Endpoint Description
POST /api/v1/students/ Create a student
GET /api/v1/students/ List all students
GET /api/v1/students/{id} Get student by ID
PUT /api/v1/students/{id} Update student
DELETE /api/v1/students/{id} Delete student

📚 Courses

Method Endpoint Description
POST /api/v1/courses/ Create a course
GET /api/v1/courses/ List all courses
GET /api/v1/courses/{id} Get course by ID
PUT /api/v1/courses/{id} Update course
DELETE /api/v1/courses/{id} Delete course

🔗 Enrollments

Method Endpoint Description
POST /api/v1/enrollments/ Enroll student in course
GET /api/v1/enrollments/ List all enrollments
GET /api/v1/enrollments/{id} Get enrollment by ID
GET /api/v1/enrollments/student/{id} Student's courses
PUT /api/v1/enrollments/{id} Update grade/completion
DELETE /api/v1/enrollments/{id} Remove enrollment

💡 Key Concepts for Juniors

What is Pydantic?

Pydantic validates data automatically. If a request sends the wrong type, FastAPI returns a 422 Unprocessable Entity error with details.

class StudentCreate(BaseModel):
    name: str        # Required, must be string
    email: EmailStr  # Required, must be valid email format
    phone: Optional[str] = None  # Optional field

What is SQLAlchemy ORM?

ORM (Object-Relational Mapper) lets you work with database rows as Python objects:

# Without ORM (raw SQL):
cursor.execute("INSERT INTO students (name, email) VALUES (?, ?)", (name, email))

# With SQLAlchemy ORM:
student = Student(name="Ravi", email="ravi@example.com")
db.add(student)
db.commit()

What is Dependency Injection?

FastAPI's Depends() provides shared resources like DB sessions to every route function:

def get_student(student_id: int, db: Session = Depends(get_db)):
    # db is automatically injected and closed after the request

🧪 Sample Requests (curl)

# Create a student
curl -X POST "http://localhost:8000/api/v1/students/" \
  -H "Content-Type: application/json" \
  -d '{"name": "Ravi Kumar", "email": "ravi@test.com"}'

# Get all students
curl "http://localhost:8000/api/v1/students/"

# Update a student
curl -X PUT "http://localhost:8000/api/v1/students/1" \
  -H "Content-Type: application/json" \
  -d '{"bio": "Updated bio"}'

# Delete a student
curl -X DELETE "http://localhost:8000/api/v1/students/1"

🎯 Mentorship Exercises

  1. Easy: Add a search query parameter to GET /students to filter by name
  2. Medium: Add a new Mentor model with a one-to-many relationship to students
  3. Hard: Add JWT authentication so only logged-in users can DELETE records

Built for Tech Mentorship Session — Happy Coding! 🎉

About

A Backend REST API for Managing Student EnrollmentsStudent Enrollment System is a FastAPI application that handles students, courses, and enrollments through clean CRUD endpoints, backed by SQLAlchemy and SQLite.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages