Skip to content

Commit

Permalink
refactor(Userservice): Adding the int type in the base_service method…
Browse files Browse the repository at this point in the history
…s and adding moving the remove_by_id from incorrected domain base_repo to the correct related user_repo
  • Loading branch information
gabszs committed May 15, 2024
1 parent 0ccb1b2 commit d674987
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 14 additions & 10 deletions app/services/base_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Union
from uuid import UUID

from app.core.exceptions import AuthError
Expand All @@ -10,30 +11,33 @@ class BaseService:
def __init__(self, repository: BaseRepository) -> None:
self._repository = repository

async def validate_permission(self, id: UUID, current_user: UserModel):
if id != current_user.id:
raise AuthError(detail="Not enough permissions")
async def validate_permission(self, id: Union[UUID, int], current_user: UserModel, admin: bool = False):
if admin:
if current_user.is_superuser is not True:
raise AuthError(detail="Not enough permissions")
else:
if id != current_user.id:
raise AuthError(detail="Not enough permissions")

async def get_list(self, schema: FindBase):
return await self._repository.read_by_options(schema)
return await self._repository.read_by_options(schema, eager=True)

async def get_by_id(self, id: UUID):
async def get_by_id(self, id: Union[UUID, int]):
return await self._repository.read_by_id(id)

async def add(self, schema):
return await self._repository.create(schema)

async def patch(self, id: UUID, schema, current_user: UserModel):
async def patch(self, id: Union[UUID, int], schema, current_user: UserModel):
await self.validate_permission(id=id, current_user=current_user)
return await self._repository.update(id, schema)

async def patch_attr(self, id: UUID, attr: str, value, current_user: UserModel):
async def patch_attr(self, id: Union[UUID, int], attr: str, value, current_user: UserModel):
await self.validate_permission(id=id, current_user=current_user)
return await self._repository.update_attr(id, attr, value)

async def put_update(self, id: UUID, schema):
async def put_update(self, id: Union[UUID, int], schema):
return await self._repository.whole_update(id, schema)

async def remove_by_id(self, id: UUID, current_user: UserModel):
await self.validate_permission(id=id, current_user=current_user)
async def remove_by_id(self, id: Union[UUID, int]):
return await self._repository.delete_by_id(id)
8 changes: 8 additions & 0 deletions app/services/user_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from typing import Union
from uuid import UUID

from app.core.security import get_password_hash
from app.models import User as UserModel
from app.repository.user_repository import UserRepository
from app.schemas.user_schema import BaseUserWithPassword
from app.services.base_service import BaseService
Expand All @@ -14,3 +18,7 @@ async def add(self, user_schema: BaseUserWithPassword):
created_user = await self._repository.create(user_schema)
delattr(created_user, "password")
return created_user

async def remove_by_id(self, id: Union[UUID, int], current_user: UserModel):
await self.validate_permission(id=id, current_user=current_user)
return await self._repository.delete_by_id(id)

0 comments on commit d674987

Please sign in to comment.