1
|
from typing import List
|
2
|
|
3
|
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
|
4
|
from sqlalchemy.orm import Session
|
5
|
from datetime import datetime
|
6
|
from sql_app import crud, models, schemas
|
7
|
from ..database import SessionLocal, engine
|
8
|
|
9
|
models.Base.metadata.create_all(bind=engine)
|
10
|
|
11
|
# prefix used for all endpoints in this file
|
12
|
licenses = APIRouter(prefix="/api/v1")
|
13
|
|
14
|
|
15
|
# Dependency
|
16
|
def get_db():
|
17
|
db = SessionLocal()
|
18
|
try:
|
19
|
yield db
|
20
|
finally:
|
21
|
db.close()
|
22
|
|
23
|
|
24
|
@licenses.get("/licenses", response_model=List[schemas.License])
|
25
|
def read_licenses(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
26
|
"""
|
27
|
Returns all licenses currently saved in database.
|
28
|
"""
|
29
|
licenses = crud.get_licenses(db, skip=skip, limit=limit)
|
30
|
return licenses
|
31
|
|
32
|
|
33
|
@licenses.post("/device-license", response_model=schemas.DeviceLicense)
|
34
|
def create_device_license(device_license: schemas.DeviceLicenseCreate, db: Session = Depends(get_db)):
|
35
|
"""
|
36
|
Creates entry for devices_licenses table thus connecting device with its license.
|
37
|
"""
|
38
|
print(crud.create_device_license(db=db, device=device_license.device_id, license=device_license.license_id,
|
39
|
time=datetime.now()))
|