2025-06-01 23:56:11 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from graphql import GraphQLResolveInfo
|
|
|
|
|
2025-05-16 06:23:48 +00:00
|
|
|
from auth.orm import Author
|
2024-10-21 07:52:23 +00:00
|
|
|
from orm.community import Community, CommunityFollower
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.db import local_session
|
2024-10-21 13:42:30 +00:00
|
|
|
from services.schema import mutation, query
|
2024-01-25 19:41:27 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_communities_all")
|
2025-06-01 23:56:11 +00:00
|
|
|
async def get_communities_all(_: None, _info: GraphQLResolveInfo) -> list[Community]:
|
2024-10-21 07:52:23 +00:00
|
|
|
return local_session().query(Community).all()
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_community")
|
2025-06-01 23:56:11 +00:00
|
|
|
async def get_community(_: None, _info: GraphQLResolveInfo, slug: str) -> Community | None:
|
2024-10-21 07:52:23 +00:00
|
|
|
q = local_session().query(Community).where(Community.slug == slug)
|
|
|
|
return q.first()
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2024-10-21 07:52:23 +00:00
|
|
|
|
|
|
|
@query.field("get_communities_by_author")
|
2025-06-01 23:56:11 +00:00
|
|
|
async def get_communities_by_author(
|
|
|
|
_: None, _info: GraphQLResolveInfo, slug: str = "", user: str = "", author_id: int = 0
|
|
|
|
) -> list[Community]:
|
2024-10-21 07:52:23 +00:00
|
|
|
with local_session() as session:
|
|
|
|
q = session.query(Community).join(CommunityFollower)
|
|
|
|
if slug:
|
|
|
|
author_id = session.query(Author).where(Author.slug == slug).first().id
|
|
|
|
q = q.where(CommunityFollower.author == author_id)
|
|
|
|
if user:
|
2025-05-20 22:34:02 +00:00
|
|
|
author_id = session.query(Author).where(Author.id == user).first().id
|
2024-10-21 07:52:23 +00:00
|
|
|
q = q.where(CommunityFollower.author == author_id)
|
|
|
|
if author_id:
|
|
|
|
q = q.where(CommunityFollower.author == author_id)
|
|
|
|
return q.all()
|
|
|
|
return []
|
2024-10-21 13:42:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("join_community")
|
2025-06-01 23:56:11 +00:00
|
|
|
async def join_community(_: None, info: GraphQLResolveInfo, slug: str) -> dict[str, Any]:
|
2024-10-21 13:42:30 +00:00
|
|
|
author_dict = info.context.get("author", {})
|
|
|
|
author_id = author_dict.get("id")
|
|
|
|
with local_session() as session:
|
|
|
|
community = session.query(Community).where(Community.slug == slug).first()
|
|
|
|
if not community:
|
|
|
|
return {"ok": False, "error": "Community not found"}
|
2025-06-01 23:56:11 +00:00
|
|
|
session.add(CommunityFollower(community=community.id, follower=author_id))
|
2024-10-21 13:42:30 +00:00
|
|
|
session.commit()
|
|
|
|
return {"ok": True}
|
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("leave_community")
|
2025-06-01 23:56:11 +00:00
|
|
|
async def leave_community(_: None, info: GraphQLResolveInfo, slug: str) -> dict[str, Any]:
|
2024-10-21 13:42:30 +00:00
|
|
|
author_dict = info.context.get("author", {})
|
|
|
|
author_id = author_dict.get("id")
|
|
|
|
with local_session() as session:
|
|
|
|
session.query(CommunityFollower).where(
|
|
|
|
CommunityFollower.author == author_id, CommunityFollower.community == slug
|
|
|
|
).delete()
|
|
|
|
session.commit()
|
|
|
|
return {"ok": True}
|
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("create_community")
|
2025-06-01 23:56:11 +00:00
|
|
|
async def create_community(_: None, info: GraphQLResolveInfo, community_data: dict[str, Any]) -> dict[str, Any]:
|
2024-10-21 13:42:30 +00:00
|
|
|
author_dict = info.context.get("author", {})
|
|
|
|
author_id = author_dict.get("id")
|
|
|
|
with local_session() as session:
|
|
|
|
session.add(Community(author=author_id, **community_data))
|
|
|
|
session.commit()
|
|
|
|
return {"ok": True}
|
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("update_community")
|
2025-06-01 23:56:11 +00:00
|
|
|
async def update_community(_: None, info: GraphQLResolveInfo, community_data: dict[str, Any]) -> dict[str, Any]:
|
2024-10-21 13:42:30 +00:00
|
|
|
author_dict = info.context.get("author", {})
|
|
|
|
author_id = author_dict.get("id")
|
|
|
|
slug = community_data.get("slug")
|
|
|
|
if slug:
|
|
|
|
with local_session() as session:
|
|
|
|
try:
|
2025-05-29 09:37:39 +00:00
|
|
|
session.query(Community).where(Community.created_by == author_id, Community.slug == slug).update(
|
|
|
|
community_data
|
|
|
|
)
|
2024-10-21 13:42:30 +00:00
|
|
|
session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
return {"ok": False, "error": str(e)}
|
|
|
|
return {"ok": True}
|
|
|
|
return {"ok": False, "error": "Please, set community slug in input"}
|
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("delete_community")
|
2025-06-01 23:56:11 +00:00
|
|
|
async def delete_community(_: None, info: GraphQLResolveInfo, slug: str) -> dict[str, Any]:
|
2024-10-21 13:42:30 +00:00
|
|
|
author_dict = info.context.get("author", {})
|
|
|
|
author_id = author_dict.get("id")
|
|
|
|
with local_session() as session:
|
|
|
|
try:
|
2025-05-29 09:37:39 +00:00
|
|
|
session.query(Community).where(Community.slug == slug, Community.created_by == author_id).delete()
|
2024-10-21 13:42:30 +00:00
|
|
|
session.commit()
|
|
|
|
return {"ok": True}
|
|
|
|
except Exception as e:
|
|
|
|
return {"ok": False, "error": str(e)}
|