Refaktoring a bugfixing
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
from typing import List, Optional
|
||||
from typing import List
|
||||
import strawberry
|
||||
from database import models
|
||||
from decorators.endpoints import authenticated_user_only
|
||||
from dependencies.db import get_session
|
||||
from database.transaction import get_session
|
||||
from graphql_schema.entities.helpers.combobox import handle_combobox_save
|
||||
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_input
|
||||
from graphql_schema.entities.resolvers.base import BaseQueryResolver, BaseMutationResolver
|
||||
from graphql_schema.entities.types.types import PointOfInterest
|
||||
from .resolvers.base import get_base_resolver, get_list, get_one
|
||||
from graphql_schema.entities.types.mutation_input import ComboboxInput
|
||||
from graphql_schema.entities.types.mutation_input import CreatePointOfInterestInput, EditPointOfInterestInput
|
||||
|
||||
|
||||
@strawberry.type
|
||||
@@ -15,22 +14,16 @@ class PointOfInterestQueries:
|
||||
@strawberry.field()
|
||||
@authenticated_user_only()
|
||||
async def points_of_interest(root, info) -> List[PointOfInterest]:
|
||||
query = get_base_resolver(models.PointOfInterest, user_id=info.context.user_id)
|
||||
return await get_list(models.PointOfInterest, query)
|
||||
return await BaseQueryResolver(PointOfInterest, models.PointOfInterest).get_list(info.context.user_id)
|
||||
|
||||
@strawberry.field()
|
||||
@authenticated_user_only()
|
||||
async def point_of_interest(root, info, id: int) -> PointOfInterest:
|
||||
query = get_base_resolver(models.PointOfInterest, user_id=info.context.user_id, object_id=id)
|
||||
return await get_one(models.PointOfInterest, query)
|
||||
return await BaseQueryResolver(PointOfInterest, models.PointOfInterest).get_one(id, info.context.user_id)
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class CreatePointOfInterestMutation:
|
||||
@strawberry_sqlalchemy_input(models.PointOfInterest, exclude_fields=['id', 'type_id'])
|
||||
class CreatePointOfInterestInput:
|
||||
type: Optional[ComboboxInput] = None
|
||||
|
||||
class PointOfInterestMutation:
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def create_point_of_interest(root, info, input: CreatePointOfInterestInput) -> PointOfInterest:
|
||||
@@ -42,23 +35,17 @@ class CreatePointOfInterestMutation:
|
||||
db, models.PointOfInterestType, input.type, info.context.user_id
|
||||
)
|
||||
|
||||
poi = await models.PointOfInterest.create(db, data=dict(**input_data, created_by_id=info.context.user_id))
|
||||
return PointOfInterest(**poi.as_dict())
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class EditPointOfInterestMutation:
|
||||
@strawberry_sqlalchemy_input(models.PointOfInterest, exclude_fields=['id', 'type_id'])
|
||||
class EditPointOfInterestInput:
|
||||
type: Optional[ComboboxInput] = None
|
||||
return await BaseMutationResolver(PointOfInterest, models.PointOfInterest).create(
|
||||
input_data, info.context.user_id
|
||||
)
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def edit_point_of_interest(root, info, id: int, input: EditPointOfInterestInput) -> PointOfInterest:
|
||||
input_data = input.to_dict()
|
||||
|
||||
query = get_base_resolver(
|
||||
models.PointOfInterest, user_id=info.context.user_id, object_id=id, include_public=False
|
||||
query = BaseQueryResolver(PointOfInterest, models.PointOfInterest).get_query(
|
||||
user_id=info.context.user_id, object_id=id, include_public=False
|
||||
)
|
||||
|
||||
async with get_session() as db:
|
||||
@@ -71,19 +58,7 @@ class EditPointOfInterestMutation:
|
||||
updated_poi = await models.PointOfInterest.update(db, obj=poi, data=input_data)
|
||||
return PointOfInterest(**updated_poi.as_dict())
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class DeletePointOfInterestMutation:
|
||||
|
||||
@strawberry.mutation
|
||||
@authenticated_user_only()
|
||||
async def delete_point_of_interest(self, info, id: int) -> PointOfInterest:
|
||||
query = get_base_resolver(
|
||||
models.PointOfInterest, user_id=info.context.user_id, object_id=id, include_public=False
|
||||
)
|
||||
|
||||
async with get_session() as db:
|
||||
poi = (await db.scalars(query)).one()
|
||||
|
||||
updated_poi = await models.PointOfInterest.update(db, obj=poi, data=dict(deleted=True))
|
||||
return PointOfInterest(**updated_poi.as_dict())
|
||||
return await BaseMutationResolver(PointOfInterest, models.PointOfInterest).delete(info.context.user_id, id=id)
|
||||
|
||||
Reference in New Issue
Block a user