Compare commits
1 Commits
dev1
...
7e9346ec4d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e9346ec4d |
@@ -1,15 +1,20 @@
|
|||||||
from typing import Any, Protocol
|
from typing import TYPE_CHECKING, Any, Protocol
|
||||||
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from src.repository.tasks import TasksRepo
|
if TYPE_CHECKING:
|
||||||
from src.repository.users import UsersRepo
|
from src.repository.tasks import TasksRepo
|
||||||
|
from src.repository.users import UsersRepo
|
||||||
|
|
||||||
|
|
||||||
|
class HasId(Protocol):
|
||||||
|
id: Any
|
||||||
|
|
||||||
|
|
||||||
class IUOWDB(Protocol):
|
class IUOWDB(Protocol):
|
||||||
session: AsyncSession
|
session: AsyncSession
|
||||||
user: UsersRepo
|
user: 'UsersRepo'
|
||||||
task: TasksRepo
|
task: 'TasksRepo'
|
||||||
|
|
||||||
async def __aenter__(self) -> "IUOWDB": ...
|
async def __aenter__(self) -> "IUOWDB": ...
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
from typing import Any, Generic, Mapping, Sequence, Type, TypeVar
|
from typing import Any, Generic, Mapping, Sequence, Type, TypeVar
|
||||||
|
|
||||||
from sqlalchemy import delete, insert, select
|
from sqlalchemy import delete, insert, select, update
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from src.core.database import Base
|
from src.core.interfaces import HasId
|
||||||
|
|
||||||
ModelType = TypeVar("ModelType", bound=Base)
|
ModelType = TypeVar("ModelType", bound=HasId)
|
||||||
|
|
||||||
|
|
||||||
class BaseRepo(Generic[ModelType]):
|
class BaseRepo(Generic[ModelType]):
|
||||||
@@ -44,3 +44,14 @@ class BaseRepo(Generic[ModelType]):
|
|||||||
|
|
||||||
async def delete_one(self, **filter_by) -> None:
|
async def delete_one(self, **filter_by) -> None:
|
||||||
await self.session.execute(delete(self.model).filter_by(**filter_by))
|
await self.session.execute(delete(self.model).filter_by(**filter_by))
|
||||||
|
|
||||||
|
async def update_one(self, id: int, data: Mapping[str, Any]) -> ModelType:
|
||||||
|
stmt = (
|
||||||
|
update(self.model)
|
||||||
|
.where(self.model.id == id)
|
||||||
|
.values(data)
|
||||||
|
.returning(self.model)
|
||||||
|
)
|
||||||
|
result = await self.session.execute(stmt)
|
||||||
|
model = result.scalar_one()
|
||||||
|
return model
|
||||||
|
|||||||
@@ -28,7 +28,11 @@ class TaskService(BaseService):
|
|||||||
await self.session.task.delete_one(id=task_id)
|
await self.session.task.delete_one(id=task_id)
|
||||||
await self.session.commit()
|
await self.session.commit()
|
||||||
|
|
||||||
async def update_task(self, task_id: int, task_data: TaskPATCHRequest, exclude_unset: bool = True):
|
async def update_task(
|
||||||
task = await self.session.task.update_one(id=task_id, data=task_data.model_dump(exclude_unset=exclude_unset))
|
self, task_id: int, task_data: TaskPATCHRequest, exclude_unset: bool = True
|
||||||
|
):
|
||||||
|
task = await self.session.task.update_one(
|
||||||
|
id=task_id, data=task_data.model_dump(exclude_unset=exclude_unset)
|
||||||
|
)
|
||||||
await self.session.commit()
|
await self.session.commit()
|
||||||
return Task.model_validate(task)
|
return Task.model_validate(task)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import json
|
|
||||||
import pytest
|
import pytest
|
||||||
from httpx import ASGITransport, AsyncClient
|
from httpx import ASGITransport, AsyncClient
|
||||||
from sqlalchemy import NullPool, insert
|
from sqlalchemy import NullPool, insert
|
||||||
|
|||||||
Reference in New Issue
Block a user