139 lines
5.3 KiB
Python
Executable File
139 lines
5.3 KiB
Python
Executable File
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app.database import get_db
|
|
from app.models import License, LicenseType, Product
|
|
from app.schemas import LicenseCreate, LicenseUpdate, LicenseTypeCreate, LicenseTypeUpdate, ProductCreate, ProductUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
# License routes
|
|
@router.get("/licenses")
|
|
async def get_licenses(db: Session = Depends(get_db)):
|
|
licenses = db.query(License).all()
|
|
return licenses
|
|
|
|
@router.get("/licenses/{license_id}")
|
|
async def get_license(license_id: int, db: Session = Depends(get_db)):
|
|
license = db.query(License).filter(License.id == license_id).first()
|
|
if not license:
|
|
raise HTTPException(status_code=404, detail="License not found")
|
|
return license
|
|
|
|
@router.post("/licenses")
|
|
async def create_license(license: LicenseCreate, db: Session = Depends(get_db)):
|
|
db_license = License(**license.dict())
|
|
db.add(db_license)
|
|
db.commit()
|
|
db.refresh(db_license)
|
|
return db_license
|
|
|
|
@router.put("/licenses/{license_id}")
|
|
async def update_license(license_id: int, license: LicenseUpdate, db: Session = Depends(get_db)):
|
|
db_license = db.query(License).filter(License.id == license_id).first()
|
|
if not db_license:
|
|
raise HTTPException(status_code=404, detail="License not found")
|
|
|
|
for key, value in license.dict(exclude_unset=True).items():
|
|
setattr(db_license, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_license)
|
|
return db_license
|
|
|
|
@router.delete("/licenses/{license_id}")
|
|
async def delete_license(license_id: int, db: Session = Depends(get_db)):
|
|
db_license = db.query(License).filter(License.id == license_id).first()
|
|
if not db_license:
|
|
raise HTTPException(status_code=404, detail="License not found")
|
|
|
|
db.delete(db_license)
|
|
db.commit()
|
|
return {"message": "License deleted successfully"}
|
|
|
|
# LicenseType routes
|
|
@router.get("/license_types")
|
|
async def get_license_types(db: Session = Depends(get_db)):
|
|
license_types = db.query(LicenseType).all()
|
|
return license_types
|
|
|
|
@router.get("/license_types/{license_type_id}")
|
|
async def get_license_type(license_type_id: int, db: Session = Depends(get_db)):
|
|
license_type = db.query(LicenseType).filter(LicenseType.id == license_type_id).first()
|
|
if not license_type:
|
|
raise HTTPException(status_code=404, detail="License type not found")
|
|
return license_type
|
|
|
|
@router.post("/license_types")
|
|
async def create_license_type(license_type: LicenseTypeCreate, db: Session = Depends(get_db)):
|
|
db_license_type = LicenseType(**license_type.dict())
|
|
db.add(db_license_type)
|
|
db.commit()
|
|
db.refresh(db_license_type)
|
|
return db_license_type
|
|
|
|
@router.put("/license_types/{license_type_id}")
|
|
async def update_license_type(license_type_id: int, license_type: LicenseTypeUpdate, db: Session = Depends(get_db)):
|
|
db_license_type = db.query(LicenseType).filter(LicenseType.id == license_type_id).first()
|
|
if not db_license_type:
|
|
raise HTTPException(status_code=404, detail="License type not found")
|
|
|
|
for key, value in license_type.dict(exclude_unset=True).items():
|
|
setattr(db_license_type, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_license_type)
|
|
return db_license_type
|
|
|
|
@router.delete("/license_types/{license_type_id}")
|
|
async def delete_license_type(license_type_id: int, db: Session = Depends(get_db)):
|
|
db_license_type = db.query(LicenseType).filter(LicenseType.id == license_type_id).first()
|
|
if not db_license_type:
|
|
raise HTTPException(status_code=404, detail="License type not found")
|
|
|
|
db.delete(db_license_type)
|
|
db.commit()
|
|
return {"message": "License type deleted successfully"}
|
|
|
|
# Product routes
|
|
@router.get("/products")
|
|
async def get_products(db: Session = Depends(get_db)):
|
|
products = db.query(Product).all()
|
|
return products
|
|
|
|
@router.get("/products/{product_id}")
|
|
async def get_product(product_id: int, db: Session = Depends(get_db)):
|
|
product = db.query(Product).filter(Product.id == product_id).first()
|
|
if not product:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
return product
|
|
|
|
@router.post("/products")
|
|
async def create_product(product: ProductCreate, db: Session = Depends(get_db)):
|
|
db_product = Product(**product.dict())
|
|
db.add(db_product)
|
|
db.commit()
|
|
db.refresh(db_product)
|
|
return db_product
|
|
|
|
@router.put("/products/{product_id}")
|
|
async def update_product(product_id: int, product: ProductUpdate, db: Session = Depends(get_db)):
|
|
db_product = db.query(Product).filter(Product.id == product_id).first()
|
|
if not db_product:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
|
|
for key, value in product.dict(exclude_unset=True).items():
|
|
setattr(db_product, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_product)
|
|
return db_product
|
|
|
|
@router.delete("/products/{product_id}")
|
|
async def delete_product(product_id: int, db: Session = Depends(get_db)):
|
|
db_product = db.query(Product).filter(Product.id == product_id).first()
|
|
if not db_product:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
|
|
db.delete(db_product)
|
|
db.commit()
|
|
return {"message": "Product deleted successfully"} |