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