2024-06-11 11:46:10 +00:00
|
|
|
from sqlalchemy import select
|
2023-12-17 20:30:20 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
from orm.author import Author
|
2024-06-05 14:45:55 +00:00
|
|
|
from orm.community import Community
|
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
|
|
|
|
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
def get_communities_from_query(q):
|
|
|
|
ccc = []
|
|
|
|
with local_session() as session:
|
2024-01-23 02:03:23 +00:00
|
|
|
for [c, shouts_stat, followers_stat] in session.execute(q):
|
2023-10-23 14:47:11 +00:00
|
|
|
c.stat = {
|
2024-06-11 11:46:10 +00:00
|
|
|
"shouts": shouts_stat,
|
|
|
|
"followers": followers_stat,
|
2024-04-23 12:14:59 +00:00
|
|
|
# "authors": session.execute(select(func.count(distinct(ShoutCommunity.shout))).filter(ShoutCommunity.community == c.id)),
|
2024-01-23 02:03:23 +00:00
|
|
|
# "commented": commented_stat,
|
2023-10-23 14:47:11 +00:00
|
|
|
}
|
|
|
|
ccc.append(c)
|
|
|
|
|
|
|
|
return ccc
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
q = select(Author)
|
|
|
|
|
|
|
|
return get_communities_from_query(q)
|
|
|
|
|
|
|
|
|
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):
|
2023-10-23 14:47:11 +00:00
|
|
|
q = select(Community).where(Community.slug == slug)
|
|
|
|
|
2023-11-30 08:40:27 +00:00
|
|
|
communities = get_communities_from_query(q)
|
|
|
|
return communities[0]
|