2023-10-23 14:47:11 +00:00
|
|
|
from orm.author 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-04-08 07:38:58 +00:00
|
|
|
from services.schema import query
|
2024-01-25 19:41:27 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_communities_all")
|
2023-10-23 14:47:11 +00:00
|
|
|
async def get_communities_all(_, _info):
|
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")
|
2024-02-24 10:22:35 +00:00
|
|
|
async def get_community(_, _info, slug: str):
|
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")
|
|
|
|
async def get_communities_by_author(_, _info, slug="", user="", author_id=0):
|
|
|
|
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:
|
|
|
|
author_id = session.query(Author).where(Author.user == user).first().id
|
|
|
|
q = q.where(CommunityFollower.author == author_id)
|
|
|
|
if author_id:
|
|
|
|
q = q.where(CommunityFollower.author == author_id)
|
|
|
|
return q.all()
|
|
|
|
return []
|