update a few things
This commit is contained in:
+87
-14
@@ -1,16 +1,21 @@
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, Query
|
||||
from pydantic import BaseModel
|
||||
|
||||
from fastapi_sqlalchemy import DBSessionMiddleware, db
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from rota.shifts import SingleShift, ShiftConstraint
|
||||
|
||||
from web.models import SingleShift as ModelSingleShift
|
||||
from web.models import ShiftConstraint as ModelShiftConstraint
|
||||
from models import SingleShift as ModelSingleShift
|
||||
#from models import ShiftConstraint as ModelShiftConstraint
|
||||
|
||||
from database import SessionLocal, engine
|
||||
|
||||
import crud, models, schemas
|
||||
|
||||
#from rota.wokers
|
||||
|
||||
@@ -19,21 +24,89 @@ load_dotenv('../.env')
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.add_middleware(DBSessionMiddleware, db_url="sqlite:///test.db")
|
||||
# Dependency
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@app.post('/shifts/', response_model=SingleShift)
|
||||
async def shifts(shift: ModelSingleShift):
|
||||
db_shift = ModelSingleShift(name=shift.name, length=shift.length)
|
||||
db.session.add(db_shift)
|
||||
db.session.commit()
|
||||
|
||||
#app.add_middleware(DBSessionMiddleware, db_url="sqlite:///test.db")
|
||||
models.Base.metadata.create_all(bind=engine)
|
||||
|
||||
@app.post("/shifts/", response_model=schemas.SingleShift)
|
||||
def create_shift(shift: schemas.SingleShiftCreate, db: Session = Depends(get_db)):
|
||||
db_shift = crud.get_shift_by_name(db, name=shift.name)
|
||||
if db_shift:
|
||||
raise HTTPException(status_code=400, detail="Shift name already exists")
|
||||
return crud.create_shift(db=db, shift=shift)
|
||||
|
||||
|
||||
@app.get("/shifts/", response_model=list[schemas.SingleShift])
|
||||
def read_shifts(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
||||
shifts = crud.get_shifts(db, skip=skip, limit=limit)
|
||||
return shifts
|
||||
|
||||
|
||||
@app.get("/shifts/{shift_id}", response_model=schemas.SingleShift)
|
||||
def read_shift(shift_id: int, db: Session = Depends(get_db)):
|
||||
db_shift = crud.get_shifts(db, shift_id=shift_id)
|
||||
if db_shift is None:
|
||||
raise HTTPException(status_code=404, detail="Shift not found")
|
||||
return db_shift
|
||||
|
||||
|
||||
@app.get("/shifts/")
|
||||
async def shifts():
|
||||
shifts = db.session.query(ModelSingleShift).all()
|
||||
@app.post("/workers/", response_model=schemas.Worker)
|
||||
def create_worker(worker: schemas.WorkerCreate, db: Session = Depends(get_db)):
|
||||
db_worker = crud.get_worker_by_name(db, name=worker.name)
|
||||
if db_worker:
|
||||
raise HTTPException(status_code=400, detail="worker name already exists")
|
||||
return crud.create_worker(db=db, worker=worker)
|
||||
|
||||
return shifts
|
||||
|
||||
@app.get("/workers/", response_model=list[schemas.Worker])
|
||||
def read_workers(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
||||
workers = crud.get_workers(db, skip=skip, limit=limit)
|
||||
return workers
|
||||
|
||||
|
||||
@app.get("/workers/{worker_id}", response_model=schemas.Worker)
|
||||
def read_worker(worker_id: int, db: Session = Depends(get_db)):
|
||||
db_worker = crud.get_workers(db, worker_id=worker_id)
|
||||
if db_worker is None:
|
||||
raise HTTPException(status_code=404, detail="worker not found")
|
||||
return db_worker
|
||||
|
||||
#@app.post("/users/{user_id}/items/", response_model=schemas.Item)
|
||||
#def create_item_for_user(
|
||||
# user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
|
||||
#):
|
||||
# return crud.create_user_item(db=db, item=item, user_id=user_id)
|
||||
#
|
||||
#
|
||||
#@app.get("/items/", response_model=list[schemas.Item])
|
||||
#def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
||||
# items = crud.get_items(db, skip=skip, limit=limit)
|
||||
# return items
|
||||
|
||||
#@app.post("/shifts/")
|
||||
#async def create_shift(shift: SingleShift):
|
||||
# return shift
|
||||
|
||||
#@app.post('/shifts/', response_model=SingleShift)
|
||||
#async def shifts(shift: ModelSingleShift):
|
||||
# db_shift = ModelSingleShift(name=shift.name, length=shift.length)
|
||||
# db.session.add(db_shift)
|
||||
# db.session.commit()
|
||||
# return db_shift
|
||||
|
||||
|
||||
#@app.get("/shifts/")
|
||||
#async def shifts():
|
||||
# shifts = db.session.query(ModelSingleShift).all()
|
||||
#
|
||||
# return shifts
|
||||
|
||||
|
||||
@app.get("/")
|
||||
|
||||
Reference in New Issue
Block a user