1 |
6c1e92e3
|
Matej Zeman
|
from typing import List
|
2 |
|
|
from fastapi import Depends, FastAPI, HTTPException, APIRouter
|
3 |
|
|
from sqlalchemy.orm import Session
|
4 |
ef87a1d2
|
Matej Zeman
|
from sql_app import crud, models, schemas
|
5 |
6c1e92e3
|
Matej Zeman
|
from ..database import SessionLocal, engine
|
6 |
c0f38eab
|
Matej Zeman
|
from fastapi import FastAPI, Request
|
7 |
|
|
from fastapi.responses import HTMLResponse
|
8 |
|
|
from fastapi.staticfiles import StaticFiles
|
9 |
|
|
from fastapi.templating import Jinja2Templates
|
10 |
6c1e92e3
|
Matej Zeman
|
|
11 |
|
|
models.Base.metadata.create_all(bind=engine)
|
12 |
8fd3d7e5
|
Matej Zeman
|
templates = Jinja2Templates(directory="templates/pcs")
|
13 |
6c1e92e3
|
Matej Zeman
|
|
14 |
2229e0e1
|
Matej Zeman
|
pcs = APIRouter(prefix="/api/v1")
|
15 |
6c1e92e3
|
Matej Zeman
|
|
16 |
|
|
|
17 |
|
|
# Dependency
|
18 |
|
|
def get_db():
|
19 |
|
|
db = SessionLocal()
|
20 |
|
|
try:
|
21 |
|
|
yield db
|
22 |
|
|
finally:
|
23 |
|
|
db.close()
|
24 |
|
|
|
25 |
|
|
|
26 |
2229e0e1
|
Matej Zeman
|
@pcs.get("/pcs-web", response_class=HTMLResponse)
|
27 |
c0f38eab
|
Matej Zeman
|
async def read_pcs(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
28 |
|
|
pcs = crud.get_pcs(db, skip=skip, limit=limit)
|
29 |
|
|
return templates.TemplateResponse("pcs.html", {"request": request, "pcs": pcs})
|
30 |
|
|
|
31 |
|
|
|
32 |
2229e0e1
|
Matej Zeman
|
@pcs.post("/pc", response_model=schemas.PC)
|
33 |
6c1e92e3
|
Matej Zeman
|
def create_pc(pc: schemas.PCCreate, db: Session = Depends(get_db)):
|
34 |
|
|
print(crud.create_pc(db=db, user=pc.username, host=pc.hostname))
|
35 |
|
|
|
36 |
|
|
|
37 |
2229e0e1
|
Matej Zeman
|
@pcs.get("/pcs", response_model=List[schemas.PC])
|
38 |
6c1e92e3
|
Matej Zeman
|
def read_pcs(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
39 |
|
|
pcs = crud.get_pcs(db, skip=skip, limit=limit)
|
40 |
|
|
return pcs
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
@pcs.get("/pc/{pc_id}", response_model=schemas.PC)
|
44 |
|
|
def read_pc(pc_id: int, db: Session = Depends(get_db)):
|
45 |
|
|
db_pc = crud.get_pc(db, pc_id=pc_id)
|
46 |
|
|
if db_pc is None:
|
47 |
|
|
raise HTTPException(status_code=404, detail="Device not found")
|
48 |
|
|
return db_pc
|