59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
from fastapi import FastAPI, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from . import models, schemas
|
|
from .database import SessionLocal, engine
|
|
from datetime import datetime
|
|
|
|
app = FastAPI()
|
|
|
|
# Tworzenie tabel w bazie danych
|
|
models.Base.metadata.create_all(bind=engine)
|
|
|
|
# Dependency do sesji z bazą danych
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
# 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()
|
|
)
|
|
db.add(db_event)
|
|
db.commit()
|
|
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()
|
|
|