Skip to content

Commit

Permalink
rewrite delete methods
Browse files Browse the repository at this point in the history
added endpoints_services -> delete_obj
  • Loading branch information
Sovraska committed Dec 16, 2023
1 parent 04b2432 commit 3c7cf44
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 30 deletions.
7 changes: 3 additions & 4 deletions app/api/endpoints/achievement.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.validators import check_name_duplicate, check_obj_exists
from app.api.validators import check_name_duplicate
from app.schemas.achievement import AchievementRead, AchievementCreate
from app.core.db import get_async_session
from app.crud import achievement_crud
from app.services.endpoints_services import delete_obj

router = APIRouter()

Expand Down Expand Up @@ -35,6 +36,4 @@ async def delete_achievement(
session: AsyncSession = Depends(get_async_session),
):
"""Удалить объект"""
await check_obj_exists(obj_id, achievement_crud, session)
achievement = await achievement_crud.get(obj_id=obj_id, session=session)
return await achievement_crud.remove(db_obj=achievement, session=session)
return await delete_obj(obj_id=obj_id, crud=achievement_crud, session=session)
7 changes: 3 additions & 4 deletions app/api/endpoints/course.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.validators import check_name_duplicate, check_obj_exists
from app.api.validators import check_name_duplicate
from app.schemas.course import CourseCreate, CourseRead
from app.core.db import get_async_session
from app.crud import course_crud
from app.services.endpoints_services import delete_obj

router = APIRouter()

Expand Down Expand Up @@ -35,6 +36,4 @@ async def delete_course(
session: AsyncSession = Depends(get_async_session),
):
"""Удалить объект"""
await check_obj_exists(obj_id, course_crud, session)
course = await course_crud.get(obj_id=obj_id, session=session)
return await course_crud.remove(db_obj=course, session=session)
return await delete_obj(obj_id=obj_id, crud=course_crud, session=session)
7 changes: 3 additions & 4 deletions app/api/endpoints/examination.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.validators import check_name_duplicate, check_obj_exists
from app.api.validators import check_name_duplicate
from app.schemas.examination import ExaminationRead, ExaminationCreate
from app.core.db import get_async_session
from app.crud import examination_crud
from app.services.endpoints_services import delete_obj

router = APIRouter()

Expand Down Expand Up @@ -35,6 +36,4 @@ async def delete_examination(
session: AsyncSession = Depends(get_async_session),
):
"""Удалить объект"""
await check_obj_exists(obj_id, examination_crud, session)
examination = await examination_crud.get(obj_id=obj_id, session=session)
return await examination_crud.remove(db_obj=examination, session=session)
return await delete_obj(obj_id=obj_id, crud=examination_crud, session=session)
7 changes: 3 additions & 4 deletions app/api/endpoints/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from app.schemas.group import GroupRead, GroupCreate
from app.core.db import get_async_session
from app.crud import group_crud
from app.api.validators import check_name_duplicate, check_obj_exists
from app.api.validators import check_name_duplicate
from app.services.endpoints_services import delete_obj

router = APIRouter()

Expand Down Expand Up @@ -35,6 +36,4 @@ async def delete_group(
session: AsyncSession = Depends(get_async_session),
):
"""Удалить объект"""
await check_obj_exists(obj_id, group_crud, session)
group = await group_crud.get(obj_id=obj_id, session=session)
return await group_crud.remove(db_obj=group, session=session)
return await delete_obj(obj_id=obj_id, crud=group_crud, session=session)
6 changes: 2 additions & 4 deletions app/api/endpoints/profile.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.validators import check_obj_exists
from app.models import User
from app.schemas.profile import ProfileRead, ProfileCreate
from app.core.db import get_async_session
from app.crud import profile_crud, user_crud
from app.core.user import current_user
from app.services.endpoints_services import delete_obj

router = APIRouter()

Expand Down Expand Up @@ -42,6 +42,4 @@ async def delete_profile(
session: AsyncSession = Depends(get_async_session),
):
"""Удалить объект"""
await check_obj_exists(obj_id, profile_crud, session)
profile = await profile_crud.get(obj_id=obj_id, session=session)
return await profile_crud.remove(db_obj=profile, session=session)
return await delete_obj(obj_id=obj_id, crud=profile_crud, session=session)
7 changes: 3 additions & 4 deletions app/api/endpoints/tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from app.schemas.user import UserRead
from app.core.db import get_async_session
from app.crud import tariff_crud, user_crud
from app.api.validators import check_obj_exists, check_name_duplicate
from app.api.validators import check_name_duplicate
from app.services.endpoints_services import delete_obj

router = APIRouter()

Expand Down Expand Up @@ -52,6 +53,4 @@ async def delete_tariff(
session: AsyncSession = Depends(get_async_session),
):
"""Удалить объект"""
await check_obj_exists(obj_id, tariff_crud, session)
tariff = await tariff_crud.get(obj_id=obj_id, session=session)
return await tariff_crud.remove(db_obj=tariff, session=session)
return await delete_obj(obj_id=obj_id, crud=tariff_crud, session=session)
7 changes: 3 additions & 4 deletions app/api/endpoints/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from app.schemas.task import TaskRead, TaskCreate
from app.core.db import get_async_session
from app.crud import task_crud
from app.api.validators import check_name_duplicate, check_obj_exists
from app.api.validators import check_name_duplicate
from app.services.endpoints_services import delete_obj

router = APIRouter()

Expand Down Expand Up @@ -37,6 +38,4 @@ async def delete_task(
session: AsyncSession = Depends(get_async_session),
):
"""Удалить объект"""
await check_obj_exists(obj_id, task_crud, session)
task = await task_crud.get(obj_id=obj_id, session=session)
return await task_crud.remove(db_obj=task, session=session)
return await delete_obj(obj_id=obj_id, crud=task_crud, session=session)
3 changes: 1 addition & 2 deletions app/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ async def check_obj_exists(
if obj is None:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail=f"Объект {crud.model.__tablename__} не найден!"
detail=f"Объект {crud.model.__tablename__} с id {obj_id} не найден."
)
return obj


async def check_name_duplicate(
Expand Down
20 changes: 20 additions & 0 deletions app/services/endpoints_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from http import HTTPStatus

from fastapi import HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm.exc import UnmappedInstanceError


async def delete_obj(
obj_id: int,
crud,
session: AsyncSession,
):
try:
db_obj = await crud.get(obj_id, session)
return await crud.remove(db_obj, session)
except UnmappedInstanceError:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail=f'Объект {crud.model.__tablename__} с id {obj_id} не найден.'
)

0 comments on commit 3c7cf44

Please sign in to comment.