1
|
from typing import List
|
2
|
from fastapi import Depends, FastAPI, HTTPException, APIRouter
|
3
|
from sqlalchemy.orm import Session
|
4
|
from sql_app import crud, models, schemas
|
5
|
from ..database import SessionLocal, engine
|
6
|
|
7
|
models.Base.metadata.create_all(bind=engine)
|
8
|
|
9
|
pcs = APIRouter(prefix="/api/v1")
|
10
|
|
11
|
|
12
|
# Dependency
|
13
|
def get_db():
|
14
|
db = SessionLocal()
|
15
|
try:
|
16
|
yield db
|
17
|
finally:
|
18
|
db.close()
|
19
|
|
20
|
|
21
|
@pcs.post("/pc", response_model=schemas.PC)
|
22
|
def create_pc(pc: schemas.PCCreate, db: Session = Depends(get_db)):
|
23
|
print(crud.create_pc(db=db, user=pc.username, host=pc.hostname))
|
24
|
|
25
|
|
26
|
@pcs.get("/pcs", response_model=List[schemas.PC])
|
27
|
def read_pcs(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
28
|
pcs = crud.get_pcs(db, skip=skip, limit=limit)
|
29
|
return pcs
|
30
|
|
31
|
|
32
|
@pcs.get("/pc/{pc_id}", response_model=schemas.PC)
|
33
|
def read_pc(pc_id: int, db: Session = Depends(get_db)):
|
34
|
db_pc = crud.get_pc(db, pc_id=pc_id)
|
35
|
if db_pc is None:
|
36
|
raise HTTPException(status_code=404, detail="Device not found")
|
37
|
return db_pc
|