added CRUD users and comments

parent 9dee4de2
......@@ -3,9 +3,11 @@ from fastapi import APIRouter
from .status.routes import router as status_route
from .type.routes import router as type_route
from .task.routes import router as task_route
from .user.routes import router as user_route
router = APIRouter(prefix='/api')
router.include_router(status_route)
router.include_router(type_route)
router.include_router(task_route)
router.include_router(user_route)
......@@ -48,3 +48,26 @@ class TaskController(BaseController):
except common_exc.NotFoundException as e:
raise http_exc.HTTPNotFoundException(detail=str(e))
async def get_comments(self, id: UUID):
return await self.task_service.get_comments(id)
async def add_comment(self, id: UUID, **kwargs):
try:
return await self.task_service.add_comment(id, **kwargs)
except common_exc.CreateException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
except ValidationError as e:
raise http_exc.HTTPBadRequestException(detail=f"Validation error: {str(e)}")
async def delete_comment(self, id: UUID):
try:
await self.task_service.delete_comment(id)
return fastapi.responses.Response(status_code=status.HTTP_204_NO_CONTENT)
except common_exc.DeleteException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
except common_exc.NotFoundException as e:
raise http_exc.HTTPNotFoundException(detail=str(e))
......@@ -3,7 +3,8 @@ from uuid import UUID
import fastapi
from .schemas import (
TaskBaseSchema, TaskPostSchema, TaskPatchSchema
TaskBaseSchema, TaskPostSchema, TaskPatchSchema,
CommentSchema
)
from .ctrl import TaskController
......@@ -35,3 +36,18 @@ async def update_task(id: UUID, body: TaskPatchSchema):
@router.delete('/{id}')
async def delete_task(id: UUID):
return await ctrl.delete(id)
@router.get('/{id}/comments')
async def get_comments(id: UUID):
return await ctrl.get_comments(id)
@router.post('/{id}/comments')
async def add_comment(id: UUID, body: CommentSchema):
return await ctrl.add_comment(id, **body.model_dump(exclude_none=True))
@router.delete('/comments/{id}')
async def delete_comment(id: UUID):
return await ctrl.delete_comment(id)
......@@ -28,3 +28,8 @@ class TaskPatchSchema(TaskBaseSchema):
description: str | None = None
status: UUID | None
types: List[UUID] | None = None
class CommentSchema(BaseModel):
user: UUID
text: str
from uuid import UUID
from db.repositories import TaskRepository, StatusRepository
from db.repositories import (
TaskRepository, StatusRepository, CommentRepository,
UserRepository
)
from exceptions import common as common_exc, http as http_exc
from db.models import Type, Status
from .schemas import TaskListSchema
from .schemas import TaskListSchema, CommentSchema
class TaskService:
task_repository = TaskRepository()
status_repository = StatusRepository()
comment_repository = CommentRepository()
user_repository = UserRepository()
async def get_task_list(self, **kwargs):
tasks = await self.task_repository.get_list(**kwargs)
......@@ -74,3 +79,33 @@ class TaskService:
async def delete_task(self, id: UUID):
return await self.task_repository.delete(id)
async def get_comments(self, id: UUID):
task = await self.task_repository.get(id)
return await task.task_comments.all()
async def add_comment(self, id: UUID, **kwargs):
try:
user = await self.user_repository.get(kwargs.get('user'))
except common_exc.NotFoundException:
raise http_exc.HTTPBadRequestException(detail=f"user does not exist")
task = await self.task_repository.get(id)
comment = await self.comment_repository.create(
user=user,
task=task,
text=kwargs.get('text')
)
comment_data = {
"id": comment.id,
"text": comment.text,
"task_id": comment.task.id,
"user_id": comment.user.id
}
return comment_data
async def delete_comment(self, id: UUID):
return await self.comment_repository.delete(id)
from uuid import UUID
from starlette import status
import fastapi
from tortoise.exceptions import ValidationError
from base_ctrl import BaseController
from exceptions import common as common_exc, http as http_exc
from .services import UserService
class UserController(BaseController):
user_service = UserService()
async def get_list(self, **kwargs):
return await self.user_service.get_user_list(**kwargs)
async def get(self, id: UUID):
try:
return await self.user_service.get_user(id)
except common_exc.NotFoundException as e:
raise http_exc.HTTPNotFoundException(detail=str(e))
async def create(self, **kwargs):
try:
return await self.user_service.create_user(**kwargs)
except common_exc.CreateException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
except ValidationError as e:
raise http_exc.HTTPBadRequestException(detail=f"Validation error: {str(e)}")
async def update(self, id: UUID, **kwargs):
try:
return await self.user_service.update_user(id, **kwargs)
except common_exc.UpdateException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
async def delete(self, id: UUID):
try:
await self.user_service.delete_user(id)
return fastapi.responses.Response(status_code=status.HTTP_204_NO_CONTENT)
except common_exc.DeleteException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
except common_exc.NotFoundException as e:
raise http_exc.HTTPNotFoundException(detail=str(e))
from uuid import UUID
import fastapi
from .schemas import UserBaseSchema
from .ctrl import UserController
router = fastapi.APIRouter(prefix='/users', tags=['User'])
ctrl = UserController()
@router.get('')
async def get_users(query: UserBaseSchema = fastapi.Depends()):
return await ctrl.get_list(**query.model_dump(exclude_none=True))
@router.get('/{id}')
async def get_user(id: UUID):
return await ctrl.get(id)
@router.post('')
async def create_user(body: UserBaseSchema):
return await ctrl.create(**body.model_dump(exclude_none=True))
@router.patch('/{id}')
async def update_user(id: UUID, body: UserBaseSchema):
return await ctrl.update(id, **body.model_dump(exclude_none=True))
@router.delete('/{id}')
async def delete_user(id: UUID):
return await ctrl.delete(id)
from uuid import UUID
from pydantic import BaseModel, ConfigDict
class UserBaseSchema(BaseModel):
username: str | None = None
password: str | None = None
class UserIdSchema(BaseModel):
id: UUID
model_config = ConfigDict(from_attributes=True)
class UserSchema(UserBaseSchema, UserIdSchema):
pass
from uuid import UUID
from db.repositories import UserRepository
from .schemas import UserSchema
class UserService:
user_repository = UserRepository()
async def get_user_list(self, **kwargs):
users = await self.user_repository.get_list(**kwargs)
return [UserSchema.model_validate(user) for user in users]
async def get_user(self, id: UUID):
user = await self.user_repository.get(id)
return UserSchema.model_validate(user)
async def create_user(self, **kwargs):
user = await self.user_repository.create(**kwargs)
return UserSchema.model_validate(user)
async def update_user(self, id: UUID, **kwargs):
user = await self.user_repository.update(id, **kwargs)
return UserSchema.model_validate(user)
async def delete_user(self, id: UUID):
return await self.user_repository.delete(id)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment