diff --git a/src/reference_book/api/licence.py b/src/reference_book/api/licence.py index 788ff75..d767026 100644 --- a/src/reference_book/api/licence.py +++ b/src/reference_book/api/licence.py @@ -3,7 +3,8 @@ from fastapi import APIRouter, Depends, status, Response from ..schemas import Licence, LicenceCreate, LicenceDB from ..services import get_licence, get_licences, \ - get_client_licences, get_client_licence, add_client_licence, update_client_licence, delete_client_licence + get_client_licences, get_client_licence, add_client_licence, update_client_licence, delete_client_licence, \ + add_licence, update_licence, delete_licence from ...users.models import UserTable from ...users.logic import developer_user @@ -21,6 +22,21 @@ async def licence(id: int, user: UserTable = Depends(developer_user)): return await get_licence(id) +@router.post("/", response_model=LicenceDB, status_code=status.HTTP_201_CREATED) +async def create_licence(licence: LicenceCreate, user: UserTable = Depends(developer_user)): + return await add_licence(licence) + + +@router.put("/{id}", response_model=LicenceDB, status_code=status.HTTP_201_CREATED) +async def update_licence_by_id(id: int, item: LicenceCreate, user: UserTable = Depends(developer_user)): + return await update_licence(id, item) + + +@router.delete("/{id}", response_class=Response, status_code=status.HTTP_204_NO_CONTENT) +async def delete_licence_by_id(id: int, user: UserTable = Depends(developer_user)): + await delete_licence(id) + + @client_licences_router.get("/{id}/licences", response_model=List[LicenceDB], status_code=status.HTTP_200_OK) async def client_licence_list(id: int, user: UserTable = Depends(developer_user)): return await get_client_licences(id) diff --git a/src/reference_book/schemas.py b/src/reference_book/schemas.py index 2c5e264..3fbf37c 100644 --- a/src/reference_book/schemas.py +++ b/src/reference_book/schemas.py @@ -26,6 +26,7 @@ class LicenceBase(BaseModel): class LicenceCreate(LicenceBase): + client_id: int software_id: int diff --git a/src/reference_book/services.py b/src/reference_book/services.py index 02ce8ba..2777b5c 100644 --- a/src/reference_book/services.py +++ b/src/reference_book/services.py @@ -112,6 +112,25 @@ async def get_client_licence(id: int, pk: int): return None +async def add_licence(licence: LicenceCreate): + item = {**licence.dict()} + query = licences.insert().values(item) + licence_id = await database.execute(query) + await activate_client(item["client_id"]) + return {"id": licence_id, **licence.dict()} + + +async def update_licence(pk: int, licence: LicenceCreate): + query = licences.update().where(licences.c.id == pk).values(**licence.dict()) + await database.execute(query) + return {"id": pk, **licence.dict()} + + +async def delete_licence(pk: int): + query = licences.delete().where(licences.c.id == pk) + await database.execute(query) + + async def add_client_licence(id: int, licence: LicenceCreate): item = {**licence.dict(), "client_id": id} query = licences.insert().values(item)