2024-10-23 12:20:21 +00:00
|
|
|
from fastapi import FastAPI, Depends, HTTPException
|
|
|
|
from sqlalchemy.orm import Session
|
2024-10-24 11:24:33 +00:00
|
|
|
from . import models, schemas
|
|
|
|
from .database import SessionLocal, engine
|
|
|
|
from datetime import datetime
|
2024-10-23 12:20:21 +00:00
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
2024-10-24 11:24:33 +00:00
|
|
|
# Tworzenie tabel w bazie danych
|
|
|
|
models.Base.metadata.create_all(bind=engine)
|
2024-10-23 12:20:21 +00:00
|
|
|
|
2024-10-24 11:24:33 +00:00
|
|
|
# Dependency do sesji z bazą danych
|
2024-10-23 12:20:21 +00:00
|
|
|
def get_db():
|
|
|
|
db = SessionLocal()
|
|
|
|
try:
|
|
|
|
yield db
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
2024-10-24 11:24:33 +00:00
|
|
|
# Endpoint do pobierania ról
|
|
|
|
@app.get("/roles/", response_model=list[schemas.Role])
|
|
|
|
def read_roles(db: Session = Depends(get_db)):
|
|
|
|
return db.query(models.Role).all()
|
|
|
|
|
|
|
|
# Endpoint do dodania roli
|
|
|
|
@app.post("/roles/", response_model=schemas.Role)
|
|
|
|
def create_role(role: schemas.RoleCreate, db: Session = Depends(get_db)):
|
|
|
|
db_role = models.Role(name=role.name)
|
|
|
|
db.add(db_role)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(db_role)
|
|
|
|
return db_role
|
|
|
|
|
|
|
|
# Endpoint do pobierania zdarzeń
|
|
|
|
@app.get("/events/", response_model=list[schemas.Event])
|
|
|
|
def read_events(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
|
|
|
|
return db.query(models.Event).offset(skip).limit(limit).all()
|
|
|
|
|
|
|
|
# Endpoint do dodania zdarzenia
|
|
|
|
@app.post("/events/", response_model=schemas.Event)
|
|
|
|
def create_event(event: schemas.EventCreate, db: Session = Depends(get_db)):
|
|
|
|
db_event = models.Event(
|
|
|
|
role_id=event.role_id,
|
|
|
|
student_id=event.student_id,
|
|
|
|
criteria_id=event.criteria_id,
|
|
|
|
description=event.description,
|
|
|
|
event_date=event.event_date or datetime.now()
|
2024-10-23 12:20:21 +00:00
|
|
|
)
|
2024-10-24 11:24:33 +00:00
|
|
|
db.add(db_event)
|
2024-10-23 12:20:21 +00:00
|
|
|
db.commit()
|
2024-10-24 11:24:33 +00:00
|
|
|
db.refresh(db_event)
|
|
|
|
return db_event
|
|
|
|
|
|
|
|
# Endpoint do pobierania informacji o zdarzeniach dla konkretnego ucznia
|
|
|
|
@app.get("/events/student/{student_id}", response_model=list[schemas.Event])
|
|
|
|
def read_student_events(student_id: int, db: Session = Depends(get_db)):
|
|
|
|
return db.query(models.Event).filter(models.Event.student_id == student_id).all()
|
2024-10-23 12:20:21 +00:00
|
|
|
|