84 lines
3.5 KiB
Python
84 lines
3.5 KiB
Python
import strawberry
|
|
from database import models
|
|
from database.transaction import get_session
|
|
from graphql_schema.entities.helpers.combobox import handle_combobox_save
|
|
from graphql_schema.entities.helpers.filters import get_detail_filters
|
|
from graphql_schema.entities.helpers.pagination import PaginationWindow
|
|
from graphql_schema.entities.resolvers.base import BaseQueryResolver, BaseMutationResolver
|
|
from graphql_schema.entities.types.types import PointOfInterest
|
|
from graphql_schema.entities.types.mutation_input import CreatePointOfInterestInput, EditPointOfInterestInput
|
|
from graphql_schema.extensions.field.auth import AuthenticatedOnly, AllowPublicAccess
|
|
from graphql_schema.extensions.field.pagination import OffsetPagination
|
|
|
|
|
|
@strawberry.type
|
|
class PointOfInterestQueries:
|
|
@strawberry.field(extensions=[OffsetPagination(item_type=PointOfInterest), AllowPublicAccess()])
|
|
async def points_of_interest(
|
|
root, info,
|
|
search: str | None = None,
|
|
public: bool = False,
|
|
) -> PaginationWindow[PointOfInterest]:
|
|
return BaseQueryResolver(PointOfInterest, models.PointOfInterest).get_query(
|
|
info.context.user_id,
|
|
only_my=bool(info.context.user_id),
|
|
include_others_public=True,
|
|
only_public=public,
|
|
search=search,
|
|
)
|
|
|
|
@strawberry.field(extensions=[AllowPublicAccess()])
|
|
async def point_of_interest(
|
|
root, info,
|
|
url_slug: str | None = None,
|
|
id: int | None = None,
|
|
public: bool = False
|
|
) -> PointOfInterest:
|
|
filter_params = get_detail_filters(id, url_slug)
|
|
|
|
return await BaseQueryResolver(PointOfInterest, models.PointOfInterest).get_one(
|
|
user_id=info.context.user_id,
|
|
only_public=public,
|
|
**filter_params
|
|
)
|
|
|
|
|
|
@strawberry.type
|
|
class PointOfInterestMutation:
|
|
@strawberry.mutation(extensions=[AuthenticatedOnly()])
|
|
async def create_point_of_interest(root, info, input: CreatePointOfInterestInput) -> PointOfInterest:
|
|
input_data = input.to_dict()
|
|
|
|
async with get_session() as db:
|
|
if input.type:
|
|
input_data['type_id'] = await handle_combobox_save(
|
|
db, models.PointOfInterestType, input.type, info.context.user_id
|
|
)
|
|
|
|
input_data['created_by_id'] = info.context.user_id
|
|
return await BaseMutationResolver(PointOfInterest, models.PointOfInterest)._do_create(
|
|
db, input_data
|
|
)
|
|
|
|
@strawberry.mutation(extensions=[AuthenticatedOnly()])
|
|
async def edit_point_of_interest(root, info, id: int, input: EditPointOfInterestInput) -> PointOfInterest:
|
|
input_data = input.to_dict()
|
|
|
|
query = BaseQueryResolver(PointOfInterest, models.PointOfInterest).get_query(
|
|
user_id=info.context.user_id, object_id=id, only_public=False
|
|
)
|
|
|
|
async with get_session() as db:
|
|
if input.type is not None:
|
|
input_data['type_id'] = await handle_combobox_save(
|
|
db, models.PointOfInterestType, input.type, info.context.user_id
|
|
)
|
|
|
|
poi = (await db.scalars(query)).one()
|
|
updated_poi = await models.PointOfInterest.update(db, obj=poi, data=input_data)
|
|
return PointOfInterest(**updated_poi.as_dict())
|
|
|
|
@strawberry.mutation(extensions=[AuthenticatedOnly()])
|
|
async def delete_point_of_interest(self, info, id: int) -> PointOfInterest:
|
|
return await BaseMutationResolver(PointOfInterest, models.PointOfInterest).delete(info.context.user_id, id=id)
|