-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
218 lines (171 loc) · 6.62 KB
/
main.py
File metadata and controls
218 lines (171 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from typing import List, Optional
from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel
from sqlalchemy import create_engine, Column, Integer, String, Float, Boolean, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship, declarative_base, Session
# --------------------------
# Database Setup
# --------------------------
# SQLite database file
DATABASE_URL = "sqlite:///./app.db"
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
# Base class for database models
Base = declarative_base()
# --------------------------
# Database Models
# --------------------------
class Project(Base):
"""Database model for projects"""
__tablename__ = "projects"
id = Column(Integer, primary_key=True, index=True)
project_name = Column(String, index=True)
project_description = Column(String)
# A project can have many students
students = relationship("Student", back_populates="project")
class Student(Base):
"""Database model for students"""
__tablename__ = "students"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
email = Column(String, unique=True, index=True)
linkedin_profile = Column(String, nullable=True)
about_you = Column(String, nullable=True)
specialisation = Column(String, nullable=True)
cgpa = Column(Float, nullable=True)
favourite_language = Column(String, nullable=True)
favourite_framework = Column(String, nullable=True)
is_leader = Column(Boolean, default=False)
# Each student may belong to a project
project_id = Column(Integer, ForeignKey("projects.id"), nullable=True)
# Relationship back to project
project = relationship("Project", back_populates="students")
# --------------------------
# Pydantic Schemas (for validation and responses)
# --------------------------
# Project schemas
class ProjectBase(BaseModel):
project_name: str
project_description: str
class ProjectSchema(ProjectBase):
id: int
class Config:
orm_mode = True
# Student schemas
class StudentBase(BaseModel):
name: str
email: str
linkedin_profile: Optional[str] = None
about_you: Optional[str] = None
specialisation: Optional[str] = None
cgpa: Optional[float] = None
favourite_language: Optional[str] = None
favourite_framework: Optional[str] = None
is_leader: bool = False
project_id: Optional[int] = None
class StudentSchema(StudentBase):
id: int
class Config:
orm_mode = True
# --------------------------
# FastAPI app
# --------------------------
app = FastAPI()
# Create database tables if not exist
Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# --------------------------
# Project Endpoints
# --------------------------
@app.post("/projects/", response_model=ProjectSchema, status_code=201)
def create_project(project: ProjectBase, db: Session = Depends(get_db)):
new_project = Project(**project.model_dump())
db.add(new_project)
db.commit()
db.refresh(new_project)
return new_project
@app.get("/projects/", response_model=List[ProjectSchema])
def list_projects(db: Session = Depends(get_db)):
return db.query(Project).all()
@app.get("/projects/{project_id}", response_model=ProjectSchema)
def get_project(project_id: int, db: Session = Depends(get_db)):
project = db.query(Project).get(project_id)
if not project:
raise HTTPException(404, "Project not found")
return project
@app.put("/projects/{project_id}", response_model=ProjectSchema)
def update_project(project_id: int, updated: ProjectBase, db: Session = Depends(get_db)):
project = db.query(Project).get(project_id)
if not project:
raise HTTPException(404, "Project not found")
project.project_name = updated.project_name
project.project_description = updated.project_description
db.commit()
db.refresh(project)
return project
@app.delete("/projects/{project_id}", status_code=204)
def delete_project(project_id: int, db: Session = Depends(get_db)):
project = db.query(Project).get(project_id)
if not project:
raise HTTPException(404, "Project not found")
for student in project.students:
student.project_id = None
db.delete(project)
db.commit()
return {"message": "Project deleted successfully"}
@app.get("/project_students/{project_id}")
def list_projects_with_students(project_id: int, db: Session = Depends(get_db)):
project = db.query(Project).get(project_id)
if not project:
raise HTTPException(404, "Project not found")
project.students
return project
# --------------------------
# Student Endpoints
# --------------------------
@app.post("/students/", response_model=StudentSchema, status_code=201)
def create_student(student: StudentBase, db: Session = Depends(get_db)):
# If a project_id is given, check if it exists
if student.project_id is not None:
if not db.query(Project).get(student.project_id):
raise HTTPException(400, f"Project {student.project_id} does not exist")
new_student = Student(**student.model_dump())
db.add(new_student)
db.commit()
db.refresh(new_student)
return new_student
@app.get("/students/")
def list_students(db: Session = Depends(get_db)):
return db.query(Student).all()
@app.get("/students/{student_id}", response_model=StudentSchema)
def get_student(student_id: int, db: Session = Depends(get_db)):
student = db.query(Student).get(student_id)
if not student:
raise HTTPException(404, "Student not found")
return student
@app.put("/students/{student_id}", response_model=StudentSchema)
def update_student(student_id: int, updated: StudentBase, db: Session = Depends(get_db)):
student = db.query(Student).get(student_id)
if not student:
raise HTTPException(404, "Student not found")
if updated.project_id is not None:
if not db.query(Project).get(updated.project_id):
raise HTTPException(400, f"Project {updated.project_id} does not exist")
for field, value in updated.model_dump().items():
setattr(student, field, value)
db.commit()
db.refresh(student)
return student
@app.delete("/students/{student_id}", status_code=204)
def delete_student(student_id: int, db: Session = Depends(get_db)):
student = db.query(Student).get(student_id)
if not student:
raise HTTPException(404, "Student not found")
db.delete(student)
db.commit()
return {"message": "Student deleted successfully"}