Skip to content

Commit

Permalink
добавил CRUD методы работы с лицензиями в справочник
Browse files Browse the repository at this point in the history
  • Loading branch information
Puzanovim committed Apr 30, 2021
1 parent 0cdb9c5 commit 4beae65
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/reference_book/api/licence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/reference_book/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class LicenceBase(BaseModel):


class LicenceCreate(LicenceBase):
client_id: int
software_id: int


Expand Down
19 changes: 19 additions & 0 deletions src/reference_book/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4beae65

Please sign in to comment.