/
nollieundergrob
/
PythonPackageStorage
Обзор
Документация
Войти
/
nollieundergrob
/
PythonPackageStorage
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
backend/src/routers/node.py
85 строк
3 KB
nollieundergrob
asdsadsad
25 мар 2025, 10:29
25 мар 2025, 10:29
979fa50
Код
Авторство
О чём код?
from fastapi.routing import APIRouter from fastapi import Depends from sqlalchemy.ext.asyncio import AsyncSession from data.db_helper import db_helper from typing import Union from schemas.errors import ErrorResponse from fastapi import HTTPException from fastapi import status from schemas.nodejs import Library as Schema from schemas.nodejs import LibraryCreate, LibraryModify,DetailLibrary from service.node_packages import LibraryService as service nodejs_router = APIRouter(prefix="/dev/nodejs") @nodejs_router.get("", response_model=list[Schema]) async def get_all(session: AsyncSession = Depends(db_helper.session_dependency)): return await service.get_all(session=session) @nodejs_router.get("/{library_id}", response_model=Union[DetailLibrary, ErrorResponse]) async def get_detail(library_id: int, session: AsyncSession = Depends(db_helper.session_dependency)): try: return await service.get_detail(library_id=library_id, session=session) except ValueError as e: return HTTPException(detail=str(e), status_code=status.HTTP_404_NOT_FOUND) @nodejs_router.post("", response_model=Union[Schema, ErrorResponse]) async def create( library_in: LibraryCreate, session: AsyncSession = Depends(db_helper.session_dependency), ): try: return await service.create(library_in=library_in, session=session) except ValueError as e: return HTTPException(detail=str(e), status_code=status.HTTP_400_BAD_REQUEST) @nodejs_router.put("/{library_id}", response_model=Schema) async def replace( library_in: LibraryCreate, library_id: int, session: AsyncSession = Depends(db_helper.session_dependency), ) -> Schema: try: return await service.replace( session=session, library_id=library_id, library_in=library_in, ) except ValueError as e: raise HTTPException(detail=str(e), status_code=status.HTTP_400_BAD_REQUEST) @nodejs_router.patch("/{library_id}", response_model=Union[Schema, ErrorResponse]) async def modify( library_id: int, library_in: LibraryModify, session: AsyncSession = Depends(db_helper.session_dependency), ): try: updated_library = await service.modify(session, library_id, library_in) return updated_library except ValueError as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) @nodejs_router.delete("/{library_id}", response_model=Schema) async def delete( library_id: int, session: AsyncSession = Depends(db_helper.session_dependency), # current_user: Library = Depends(get_current_user) ): try: deleted_library = await service.delete( session=session, library_id=library_id, # current_user=current_user # Для проверки прав доступа ) return deleted_library except ValueError as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) except PermissionError as e: raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=str(e))