This commit is contained in:
2024-04-10 03:45:52 +03:00
parent 5de5b9b3e4
commit 93eb6ae6b7
5 changed files with 107 additions and 16 deletions

View File

@@ -72,3 +72,37 @@ class ClientService(BaseService):
for client in query.all():
clients.append(ClientSchema.model_validate(client))
return ClientSearchResponse(clients=clients)
async def create(self, request: ClientCreateRequest, user: User) -> ClientCreateResponse:
try:
client = await self.get_by_name(request.data.name)
if client:
return ClientCreateResponse(ok=False, message='Client already exists')
await self.create_client_raw(user, request.data.name, request.data.details)
await self.session.commit()
return ClientCreateResponse(ok=True, message='Client created')
except Exception as e:
return ClientCreateResponse(ok=False, message=str(e))
async def update(self, request: ClientUpdateRequest, user: User) -> ClientUpdateResponse:
try:
client = await self.get_by_id(request.data.id)
if not client:
return ClientUpdateResponse(ok=False, message='Client not found')
await self.session.execute(update(Client).where(Client.id == client.id).values(name=request.data.name))
await self.update_details(user, client, request.data.details)
await self.session.commit()
return ClientUpdateResponse(ok=True, message='Client updated')
except Exception as e:
return ClientUpdateResponse(ok=False, message=str(e))
async def delete(self, request: ClientDeleteRequest) -> ClientDeleteResponse:
try:
client = await self.get_by_id(request.client_id)
if not client:
return ClientDeleteResponse(ok=False, message='Client not found')
await self.session.delete(client)
await self.session.commit()
return ClientDeleteResponse(ok=True, message='Client deleted')
except Exception as e:
return ClientDeleteResponse(ok=False, message=str(e))