chore: code cleanup
Browse files
Backend/app/api/v1/api.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from fastapi import APIRouter
|
| 2 |
-
from app.api.v1.endpoints import auth,
|
| 3 |
|
| 4 |
api_router = APIRouter()
|
| 5 |
|
|
@@ -10,14 +10,8 @@ api_router.include_router(
|
|
| 10 |
tags=["Authentication"]
|
| 11 |
)
|
| 12 |
|
| 13 |
-
# Include student routes
|
| 14 |
-
api_router.include_router(
|
| 15 |
-
students.router,
|
| 16 |
-
prefix="/students",
|
| 17 |
-
tags=["Students"]
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
# Include
|
| 21 |
api_router.include_router(
|
| 22 |
quiz.router,
|
| 23 |
prefix="/quiz",
|
|
|
|
| 1 |
from fastapi import APIRouter
|
| 2 |
+
from app.api.v1.endpoints import auth, quiz
|
| 3 |
|
| 4 |
api_router = APIRouter()
|
| 5 |
|
|
|
|
| 10 |
tags=["Authentication"]
|
| 11 |
)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Include quiz routes
|
| 15 |
api_router.include_router(
|
| 16 |
quiz.router,
|
| 17 |
prefix="/quiz",
|
Backend/app/api/v1/endpoints/notes.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
+
from chromadb import AsyncHttpClient
|
| 3 |
+
from app.models import User
|
| 4 |
+
from app.api.deps import get_db, get_current_user, get_chroma_client
|
| 5 |
+
from app.schema import Quiz_input, QuizOutput, IngestRequest
|
| 6 |
+
from .prompts import SYSTEM_PROMPT
|
| 7 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 8 |
+
from chromadb.api.models.Collection import Collection # Import Collection type
|
| 9 |
+
from app.api.deps import get_chroma_collection
|
| 10 |
+
from app.llm import call_llm
|
| 11 |
+
import uuid
|
Backend/app/api/v1/endpoints/students.py
DELETED
|
@@ -1,178 +0,0 @@
|
|
| 1 |
-
from fastapi import APIRouter, HTTPException, Depends, status
|
| 2 |
-
from sqlalchemy.ext.asyncio import AsyncSession
|
| 3 |
-
from sqlalchemy import select
|
| 4 |
-
from typing import List
|
| 5 |
-
from app.schema import StudentCreate, StudentUpdate, StudentResponse
|
| 6 |
-
from app.models import Student, User
|
| 7 |
-
from app.api.deps import get_db, get_current_user
|
| 8 |
-
|
| 9 |
-
router = APIRouter()
|
| 10 |
-
|
| 11 |
-
@router.post("/", response_model=StudentResponse, status_code=status.HTTP_201_CREATED)
|
| 12 |
-
async def create_student(
|
| 13 |
-
student: StudentCreate,
|
| 14 |
-
db:AsyncSession = Depends(get_db),
|
| 15 |
-
current_user: User = Depends(get_current_user)
|
| 16 |
-
):
|
| 17 |
-
try:
|
| 18 |
-
result = await db.execute(select(Student).filter(Student.email == student.email))
|
| 19 |
-
existing_user = result.scalar_one_or_none()
|
| 20 |
-
|
| 21 |
-
if existing_user:
|
| 22 |
-
raise HTTPException(
|
| 23 |
-
status_code= status.HTTP_400_BAD_REQUEST,
|
| 24 |
-
detail=f'student with email {student.email} already exists'
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
new_student = Student(**student.model_dump())
|
| 28 |
-
db.add(new_student)
|
| 29 |
-
await db.commit()
|
| 30 |
-
await db.refresh(new_student)
|
| 31 |
-
|
| 32 |
-
return new_student
|
| 33 |
-
except HTTPException:
|
| 34 |
-
raise
|
| 35 |
-
except Exception as e :
|
| 36 |
-
raise HTTPException(
|
| 37 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 38 |
-
detail=f'failed to create student: {str(e)}'
|
| 39 |
-
)
|
| 40 |
-
@router.get("/", response_model=List[StudentResponse])
|
| 41 |
-
async def get_all_students(
|
| 42 |
-
skip: int = 0,
|
| 43 |
-
limit: int = 100,
|
| 44 |
-
db: AsyncSession = Depends(get_db),
|
| 45 |
-
current_user: User = Depends(get_current_user)
|
| 46 |
-
):
|
| 47 |
-
"""Get all students with pagination (Protected)"""
|
| 48 |
-
try:
|
| 49 |
-
result = await db.execute(
|
| 50 |
-
select(Student).offset(skip).limit(limit)
|
| 51 |
-
)
|
| 52 |
-
students = result.scalars().all()
|
| 53 |
-
return students
|
| 54 |
-
except Exception as e:
|
| 55 |
-
raise HTTPException(
|
| 56 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 57 |
-
detail=f"Failed to fetch students: {str(e)}"
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
@router.get("/{student_id}", response_model=StudentResponse)
|
| 62 |
-
async def get_student(
|
| 63 |
-
student_id: int,
|
| 64 |
-
db: AsyncSession = Depends(get_db),
|
| 65 |
-
current_user: User = Depends(get_current_user)
|
| 66 |
-
):
|
| 67 |
-
"""Get a specific student by ID (Protected)"""
|
| 68 |
-
try:
|
| 69 |
-
result = await db.execute(select(Student).filter(Student.id == student_id))
|
| 70 |
-
student = result.scalar_one_or_none()
|
| 71 |
-
|
| 72 |
-
if not student:
|
| 73 |
-
raise HTTPException(
|
| 74 |
-
status_code=status.HTTP_404_NOT_FOUND,
|
| 75 |
-
detail=f"Student with ID {student_id} not found"
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
return student
|
| 79 |
-
except HTTPException:
|
| 80 |
-
raise
|
| 81 |
-
except Exception as e:
|
| 82 |
-
raise HTTPException(
|
| 83 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 84 |
-
detail=f"Failed to fetch student: {str(e)}"
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
@router.put("/{student_id}", response_model=StudentResponse)
|
| 89 |
-
async def update_student(
|
| 90 |
-
student_id: int,
|
| 91 |
-
student_update: StudentUpdate,
|
| 92 |
-
db: AsyncSession = Depends(get_db),
|
| 93 |
-
current_user: User = Depends(get_current_user)
|
| 94 |
-
):
|
| 95 |
-
"""Update a student's information (Protected)"""
|
| 96 |
-
try:
|
| 97 |
-
result = await db.execute(select(Student).filter(Student.id == student_id))
|
| 98 |
-
student = result.scalar_one_or_none()
|
| 99 |
-
|
| 100 |
-
if not student:
|
| 101 |
-
raise HTTPException(
|
| 102 |
-
status_code=status.HTTP_404_NOT_FOUND,
|
| 103 |
-
detail=f"Student with ID {student_id} not found"
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
# Update only provided fields
|
| 107 |
-
update_data = student_update.model_dump(exclude_unset=True)
|
| 108 |
-
|
| 109 |
-
# Check email uniqueness if email is being updated
|
| 110 |
-
if "email" in update_data:
|
| 111 |
-
result = await db.execute(
|
| 112 |
-
select(Student).filter(
|
| 113 |
-
Student.email == update_data["email"],
|
| 114 |
-
Student.id != student_id
|
| 115 |
-
)
|
| 116 |
-
)
|
| 117 |
-
existing = result.scalar_one_or_none()
|
| 118 |
-
if existing:
|
| 119 |
-
raise HTTPException(
|
| 120 |
-
status_code=status.HTTP_400_BAD_REQUEST,
|
| 121 |
-
detail=f"Email {update_data['email']} is already in use"
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
for key, value in update_data.items():
|
| 125 |
-
setattr(student, key, value)
|
| 126 |
-
|
| 127 |
-
await db.commit()
|
| 128 |
-
await db.refresh(student)
|
| 129 |
-
|
| 130 |
-
return student
|
| 131 |
-
except HTTPException:
|
| 132 |
-
raise
|
| 133 |
-
except Exception as e:
|
| 134 |
-
raise HTTPException(
|
| 135 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 136 |
-
detail=f"Failed to update student: {str(e)}"
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
@router.patch("/{student_id}", response_model=StudentResponse)
|
| 141 |
-
async def partial_update_student(
|
| 142 |
-
student_id: int,
|
| 143 |
-
student_update: StudentUpdate,
|
| 144 |
-
db: AsyncSession = Depends(get_db),
|
| 145 |
-
current_user: User = Depends(get_current_user)
|
| 146 |
-
):
|
| 147 |
-
"""Partially update a student (same as PUT for this implementation) (Protected)"""
|
| 148 |
-
return await update_student(student_id, student_update, db, current_user)
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
@router.delete("/{student_id}", status_code=status.HTTP_204_NO_CONTENT)
|
| 152 |
-
async def delete_student(
|
| 153 |
-
student_id: int,
|
| 154 |
-
db: AsyncSession = Depends(get_db),
|
| 155 |
-
current_user: User = Depends(get_current_user)
|
| 156 |
-
):
|
| 157 |
-
"""Delete a student (Protected)"""
|
| 158 |
-
try:
|
| 159 |
-
result = await db.execute(select(Student).filter(Student.id == student_id))
|
| 160 |
-
student = result.scalar_one_or_none()
|
| 161 |
-
|
| 162 |
-
if not student:
|
| 163 |
-
raise HTTPException(
|
| 164 |
-
status_code=status.HTTP_404_NOT_FOUND,
|
| 165 |
-
detail=f"Student with ID {student_id} not found"
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
-
await db.delete(student)
|
| 169 |
-
await db.commit()
|
| 170 |
-
|
| 171 |
-
return None
|
| 172 |
-
except HTTPException:
|
| 173 |
-
raise
|
| 174 |
-
except Exception as e:
|
| 175 |
-
raise HTTPException(
|
| 176 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 177 |
-
detail=f"Failed to delete student: {str(e)}"
|
| 178 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Backend/app/config.py
CHANGED
|
@@ -6,7 +6,7 @@ class Settings(BaseSettings):
|
|
| 6 |
ALGORITHM: str = "HS256"
|
| 7 |
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
| 8 |
|
| 9 |
-
APP_NAME: str = "
|
| 10 |
APP_VERSION: str = "1.0.0"
|
| 11 |
APP_DESCRIPTION: str = "FastAPI + PostgreSQL with SQLAlchemy async"
|
| 12 |
|
|
|
|
| 6 |
ALGORITHM: str = "HS256"
|
| 7 |
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
| 8 |
|
| 9 |
+
APP_NAME: str = "prepAI"
|
| 10 |
APP_VERSION: str = "1.0.0"
|
| 11 |
APP_DESCRIPTION: str = "FastAPI + PostgreSQL with SQLAlchemy async"
|
| 12 |
|
Backend/app/models/__init__.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from app.models.tables import
|
| 2 |
|
| 3 |
|
| 4 |
-
__all__ = [
|
|
|
|
| 1 |
+
from app.models.tables import User
|
| 2 |
|
| 3 |
|
| 4 |
+
__all__ = [ "User"]
|
Backend/app/models/tables.py
CHANGED
|
@@ -4,14 +4,6 @@ from datetime import datetime
|
|
| 4 |
from app.database import Base
|
| 5 |
|
| 6 |
|
| 7 |
-
class Student(Base):
|
| 8 |
-
__tablename__ = "students"
|
| 9 |
-
|
| 10 |
-
id: Mapped[int] = mapped_column(primary_key=True, index= True)
|
| 11 |
-
name: Mapped[str] = mapped_column(String(100))
|
| 12 |
-
email: Mapped[str] = mapped_column(String(100), unique=True, index=True)
|
| 13 |
-
created_at: Mapped[datetime] = mapped_column(default=datetime.now())
|
| 14 |
-
|
| 15 |
class User(Base):
|
| 16 |
__tablename__ = "users"
|
| 17 |
|
|
|
|
| 4 |
from app.database import Base
|
| 5 |
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
class User(Base):
|
| 8 |
__tablename__ = "users"
|
| 9 |
|
Backend/app/schema/__init__.py
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
from app.schema.models import
|
| 2 |
|
| 3 |
-
__all__ = ["
|
|
|
|
| 1 |
+
from app.schema.models import UserCreate, Token, LoginRequest, Quiz_input, QuizOutput, IngestRequest
|
| 2 |
|
| 3 |
+
__all__ = ["UserCreate", "Token", "LoginRequest", "Quiz_input", "QuizOutput", "IngestRequest"]
|
Backend/app/schema/models.py
CHANGED
|
@@ -2,30 +2,6 @@ from pydantic import BaseModel, EmailStr, Field, field_validator, ConfigDict
|
|
| 2 |
from typing import Optional, Literal, List
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
-
class StudentBase(BaseModel):
|
| 6 |
-
name: str = Field(..., min_length=2, max_length=100)
|
| 7 |
-
email: EmailStr = Field(...)
|
| 8 |
-
|
| 9 |
-
@field_validator("name")
|
| 10 |
-
def validate_name(cls, v):
|
| 11 |
-
if not v.strip():
|
| 12 |
-
raise ValueError('Name cannot be empty or just whitespace')
|
| 13 |
-
return v.strip()
|
| 14 |
-
|
| 15 |
-
class StudentCreate(StudentBase):
|
| 16 |
-
pass
|
| 17 |
-
|
| 18 |
-
class StudentUpdate(BaseModel):
|
| 19 |
-
name: Optional[str] = Field(None, min_length=2, max_length=100)
|
| 20 |
-
email: Optional[EmailStr] = None
|
| 21 |
-
|
| 22 |
-
class StudentResponse(StudentBase):
|
| 23 |
-
id: int
|
| 24 |
-
created_at: datetime
|
| 25 |
-
|
| 26 |
-
model_config = ConfigDict(from_attributes=True)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
class UserCreate(BaseModel):
|
| 30 |
username: str = Field(..., min_length=3, max_length=50)
|
| 31 |
email: EmailStr = Field(...)
|
|
|
|
| 2 |
from typing import Optional, Literal, List
|
| 3 |
from datetime import datetime
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
class UserCreate(BaseModel):
|
| 6 |
username: str = Field(..., min_length=3, max_length=50)
|
| 7 |
email: EmailStr = Field(...)
|