added folders and files

parent b539df47
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 FileService
class FileController(BaseController):
file_service = FileService()
async def get_list(self, **kwargs):
return await self.file_service.get_file_list(**kwargs)
async def get(self, id: UUID):
try:
return await self.file_service.get_file(id)
except common_exc.NotFoundException as e:
raise http_exc.HTTPNotFoundException(detail=str(e))
async def create(self, **kwargs):
try:
return await self.file_service.create_file(**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.file_service.update_file(id, **kwargs)
except common_exc.UpdateException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
async def delete(self, id: UUID):
try:
await self.file_service.delete_file(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 starlette import status
from .schemas import FileBaseSchema, FileGetSchema
from .ctrl import FileController
from ..dependencies import authenticate_user
from db.repositories import UserRepository, FolderRepository, FileRepository
from exceptions import common as common_exc, http as http_exc
router = fastapi.APIRouter(prefix='/files', tags=['File'])
ctrl = FileController()
user_repo = UserRepository()
folder_repo = FolderRepository()
file_repo = FileRepository()
@router.get('')
async def get_files(username: str = fastapi.Depends(authenticate_user)):
user = await user_repo.get_user_by_username(username)
files = await user.user_files
return [FileGetSchema.model_validate(file) for file in files]
@router.get('/{id}')
async def get_file(id: UUID, username: str = fastapi.Depends(authenticate_user)):
user = await user_repo.get_user_by_username(username)
file = await file_repo.get(id)
if user.id != file.user_id:
raise fastapi.HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to access this file",
)
file = await ctrl.get(id)
return FileGetSchema.model_validate(file)
@router.post('')
async def create_file(
body: FileBaseSchema,
username: str = fastapi.Depends(authenticate_user)
):
name = body.name
content = body.content
try:
folder = await folder_repo.get(id=body.folder)
except common_exc.NotFoundException:
raise http_exc.HTTPBadRequestException(detail='Object folder does not exist')
user = await user_repo.get_user_by_username(username)
file = await ctrl.create(name=name, content=content, folder=folder, user=user)
return FileGetSchema.model_validate(file)
@router.patch('/{id}')
async def update_file(
id: UUID, body: FileBaseSchema,
username: str = fastapi.Depends(authenticate_user)
):
user = await user_repo.get_user_by_username(username)
file = await file_repo.get(id)
if user.id != file.user_id:
raise fastapi.HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to access this file",
)
try:
folder = await folder_repo.get(id=body.folder)
except common_exc.NotFoundException:
raise http_exc.HTTPBadRequestException(detail='Object folder does not exist')
file = await ctrl.update(id, name=body.name, content=body.content, folder=folder)
return FileGetSchema.model_validate(file)
@router.delete('/{id}')
async def delete_file(
id: UUID,
username: str = fastapi.Depends(authenticate_user)
):
user = await user_repo.get_user_by_username(username)
file = await file_repo.get(id)
if user.id != file.user_id:
raise fastapi.HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to access this file",
)
return await ctrl.delete(id)
from uuid import UUID
from pydantic import BaseModel, ConfigDict
class FileBaseSchema(BaseModel):
name: str | None = None
content: str | None = None
folder: UUID | None = None
class FileIdSchema(BaseModel):
id: UUID
model_config = ConfigDict(from_attributes=True)
class FileSchema(FileBaseSchema, FileIdSchema):
pass
class FileGetSchema(FileIdSchema):
name: str | None = None
content: str | None = None
folder_id: UUID | None = None
from uuid import UUID
from db.repositories import FileRepository
from .schemas import FileSchema
class FileService:
file_repository = FileRepository()
async def get_file_list(self, **kwargs):
return await self.file_repository.get_list(**kwargs)
async def get_file(self, id: UUID):
return await self.file_repository.get(id)
async def create_file(self, **kwargs):
return await self.file_repository.create(**kwargs)
async def update_file(self, id: UUID, **kwargs):
return await self.file_repository.update(id, **kwargs)
async def delete_file(self, id: UUID):
return await self.file_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 FolderService
class FolderController(BaseController):
folder_service = FolderService()
async def get_list(self, **kwargs):
return await self.folder_service.get_folder_list(**kwargs)
async def get(self, id: UUID):
try:
return await self.folder_service.get_folder(id)
except common_exc.NotFoundException as e:
raise http_exc.HTTPNotFoundException(detail=str(e))
async def create(self, **kwargs):
try:
return await self.folder_service.create_folder(**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.folder_service.update_folder(id, **kwargs)
except common_exc.UpdateException as e:
raise http_exc.HTTPBadRequestException(detail=str(e))
async def delete(self, id: UUID):
try:
await self.folder_service.delete_folder(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 starlette import status
from .schemas import FolderBaseSchema, FolderSchema
from .ctrl import FolderController
from ..dependencies import authenticate_user
from db.repositories import UserRepository, FolderRepository
from ..files.schemas import FileGetSchema
router = fastapi.APIRouter(prefix='/folders', tags=['Folder'])
ctrl = FolderController()
user_repo = UserRepository()
folder_repo = FolderRepository()
@router.get('')
async def get_folders(username: str = fastapi.Depends(authenticate_user)):
user = await user_repo.get_user_by_username(username)
folders = await user.user_folders
return [FolderSchema.model_validate(folder) for folder in folders]
@router.get('/{id}')
async def get_folder(id: UUID, username: str = fastapi.Depends(authenticate_user)):
user = await user_repo.get_user_by_username(username)
folder = await folder_repo.get(id)
if user.id != folder.user_id:
raise fastapi.HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to access this file",
)
files_in_folder = await folder.folder_files.all()
return {
"folder": FolderSchema.model_validate(folder),
"files": [FileGetSchema.model_validate(file) for file in files_in_folder]
}
@router.post('')
async def create_folder(
body: FolderBaseSchema,
username: str = fastapi.Depends(authenticate_user)
):
name = body.name
user = await user_repo.get_user_by_username(username)
return await ctrl.create(name=name, user=user)
@router.patch('/{id}')
async def update_folder(
id: UUID, body: FolderBaseSchema,
username: str = fastapi.Depends(authenticate_user)
):
user = await user_repo.get_user_by_username(username)
folder = await folder_repo.get(id)
if user.id != folder.user_id:
raise fastapi.HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to access this file",
)
folder = await ctrl.update(id, **body.model_dump(exclude_none=True))
return FolderSchema.model_validate(folder)
@router.delete('/{id}')
async def delete_folder(
id: UUID,
username: str = fastapi.Depends(authenticate_user)
):
user = await user_repo.get_user_by_username(username)
folder = await folder_repo.get(id)
if user.id != folder.user_id:
raise fastapi.HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to access this file",
)
return await ctrl.delete(id)
from uuid import UUID
from pydantic import BaseModel, ConfigDict
class FolderBaseSchema(BaseModel):
name: str | None = None
class FolderIdSchema(BaseModel):
id: UUID
model_config = ConfigDict(from_attributes=True)
class FolderSchema(FolderBaseSchema, FolderIdSchema):
pass
from uuid import UUID
from db.repositories import FolderRepository
from .schemas import FolderSchema
class FolderService:
folder_repository = FolderRepository()
async def get_folder_list(self, **kwargs):
return await self.folder_repository.get_list(**kwargs)
async def get_folder(self, id: UUID):
folder = await self.folder_repository.get(id)
return FolderSchema.model_validate(folder)
async def create_folder(self, **kwargs):
folder = await self.folder_repository.create(**kwargs)
return FolderSchema.model_validate(folder)
async def update_folder(self, id: UUID, **kwargs):
folder = await self.folder_repository.update(id, **kwargs)
return FolderSchema.model_validate(folder)
async def delete_folder(self, id: UUID):
return await self.folder_repository.delete(id)
from fastapi import APIRouter
from .users.routes import router as user_route, token_router
from .folders.routes import router as folder_router
from .files.routes import router as file_router
router = APIRouter(prefix='/api')
router.include_router(user_route)
router.include_router(token_router)
router.include_router(folder_router)
router.include_router(file_router)
......@@ -3,7 +3,7 @@ from uuid import UUID
import tortoise
from passlib.context import CryptContext
from .models import User
from .models import User, Folder, File
from exceptions import common as common_exc
from api.dependencies import authenticate_user
......@@ -64,3 +64,11 @@ class UserRepository(BaseRepository):
async def get_user_by_username(self, username: str) -> User:
return await self.model.get(username=username)
class FolderRepository(BaseRepository):
model = Folder
class FileRepository(BaseRepository):
model = File
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