From 79107bcf0a9459bca4be7a09893cf91ea6cc4db8 Mon Sep 17 00:00:00 2001 From: aXenDeveloper Date: Wed, 28 Feb 2024 17:58:04 +0100 Subject: [PATCH] feat(forum): Add _count children, topics, posts in show query forum --- .../src/admin/forum/database/schema/forums.ts | 5 +- .../forum/forums/create/create.service.ts | 25 ++++++++- .../admin/forum/forums/edit/edit.service.ts | 45 +++++++++++++--- .../admin/forum/forums/show/dto/show.obj.ts | 6 +-- .../admin/forum/forums/show/show.service.ts | 51 ++++++++++++++++--- backend/src/forum/forums/show/dto/show.obj.ts | 32 +++++++++--- backend/src/forum/forums/show/show.service.ts | 39 ++++++++++++-- backend/src/schema.gql | 37 ++++++++++---- 8 files changed, 202 insertions(+), 38 deletions(-) diff --git a/backend/src/admin/forum/database/schema/forums.ts b/backend/src/admin/forum/database/schema/forums.ts index ea941d2cc..86cb994b3 100644 --- a/backend/src/admin/forum/database/schema/forums.ts +++ b/backend/src/admin/forum/database/schema/forums.ts @@ -7,6 +7,8 @@ import { varchar } from "drizzle-orm/pg-core"; +import { forum_topics } from "./topics"; + import { core_groups } from "@/src/admin/core/database/schema/groups"; import { core_languages } from "@/src/admin/core/database/schema/languages"; @@ -31,7 +33,8 @@ export const forum_forums_relations = relations( }), name: many(forum_forums_name), description: many(forum_forums_description), - permissions: many(forum_forums_permissions) + permissions: many(forum_forums_permissions), + topics: many(forum_topics) }) ); diff --git a/backend/src/admin/forum/forums/create/create.service.ts b/backend/src/admin/forum/forums/create/create.service.ts index 101a80737..053312f48 100644 --- a/backend/src/admin/forum/forums/create/create.service.ts +++ b/backend/src/admin/forum/forums/create/create.service.ts @@ -98,7 +98,12 @@ export class CreateForumForumsService { parent: { with: { name: true, - description: true + description: true, + topics: { + with: { + posts: true + } + } } } } @@ -110,7 +115,23 @@ export class CreateForumForumsService { return { ...forum, - children: [] + _count: { + children: 0, + topics: 0, + posts: 0 + }, + children: [], + parent: { + ...forum.parent, + _count: { + children: 0, + topics: forum.parent.topics.length, + posts: forum.parent.topics.reduce( + (acc, item) => acc + item.posts.length, + 0 + ) + } + } }; } } diff --git a/backend/src/admin/forum/forums/edit/edit.service.ts b/backend/src/admin/forum/forums/edit/edit.service.ts index 4cccb2ec2..217f34149 100644 --- a/backend/src/admin/forum/forums/edit/edit.service.ts +++ b/backend/src/admin/forum/forums/edit/edit.service.ts @@ -231,7 +231,17 @@ export class EditForumForumsService { parent: { with: { name: true, - description: true + description: true, + topics: { + with: { + posts: true + } + } + } + }, + topics: { + with: { + posts: true } } } @@ -243,10 +253,9 @@ export class EditForumForumsService { name: true, description: true, permissions: true, - parent: { + topics: { with: { - name: true, - description: true + posts: true } } } @@ -254,10 +263,34 @@ export class EditForumForumsService { return { ...dataUpdate, + _count: { + children: children.length, + topics: dataUpdate.topics.length, + posts: dataUpdate.topics.reduce( + (acc, item) => acc + item.posts.length, + 0 + ) + }, children: children.map(item => ({ ...item, - children: [] - })) + children: [], + _count: { + children: 0, + posts: item.topics.reduce((acc, item) => acc + item.posts.length, 0), + topics: item.topics.length + } + })), + parent: { + ...dataUpdate.parent, + _count: { + children: 0, + posts: dataUpdate.parent.topics.reduce( + (acc, item) => acc + item.posts.length, + 0 + ), + topics: dataUpdate.parent.topics.length + } + } }; } } diff --git a/backend/src/admin/forum/forums/show/dto/show.obj.ts b/backend/src/admin/forum/forums/show/dto/show.obj.ts index 4f9b2016e..401a89334 100644 --- a/backend/src/admin/forum/forums/show/dto/show.obj.ts +++ b/backend/src/admin/forum/forums/show/dto/show.obj.ts @@ -22,7 +22,7 @@ class GroupsPermissionsForumForums { } @ObjectType() -export class PermissionsForumForums { +export class PermissionsForumForumsAdmin { @Field(() => Boolean) can_all_view: boolean; @@ -43,8 +43,8 @@ export class PermissionsForumForums { export class ShowForumForumsAdmin extends OmitType(ShowForumForumsWithParent, [ "permissions" ] as const) { - @Field(() => PermissionsForumForums) - permissions: PermissionsForumForums; + @Field(() => PermissionsForumForumsAdmin) + permissions: PermissionsForumForumsAdmin; } @ObjectType() diff --git a/backend/src/admin/forum/forums/show/show.service.ts b/backend/src/admin/forum/forums/show/show.service.ts index 6d7cfa050..cf1793370 100644 --- a/backend/src/admin/forum/forums/show/show.service.ts +++ b/backend/src/admin/forum/forums/show/show.service.ts @@ -65,13 +65,23 @@ export class ShowForumForumsAdminService { with: { name: true, description: true, + permissions: true, parent: { with: { name: true, - description: true + description: true, + topics: { + with: { + posts: true + } + } } }, - permissions: true + topics: { + with: { + posts: true + } + } } }); @@ -91,14 +101,29 @@ export class ShowForumForumsAdminService { with: { name: true, description: true, - permissions: true + permissions: true, + topics: { + with: { + posts: true + } + } } }); return { ...forum, parent: forum.parent_id - ? { ...forum.parent, _count: { children: 0 } } + ? { + ...forum.parent, + _count: { + children: 0, + topics: forum.parent.topics.length, + posts: forum.parent.topics.reduce( + (acc, item) => acc + item.posts.length, + 0 + ) + } + } : null, permissions: { can_all_view: forum.can_all_view, @@ -107,7 +132,14 @@ export class ShowForumForumsAdminService { can_all_reply: forum.can_all_reply, groups: forum.permissions }, - _count: { children: children.length }, + _count: { + children: children.length, + topics: forum.topics.length, + posts: forum.topics.reduce( + (acc, item) => acc + item.posts.length, + 0 + ) + }, children: await Promise.all( children.map(async child => { const children = @@ -133,7 +165,14 @@ export class ShowForumForumsAdminService { groups: child.permissions } })), - _count: { children: children.length } + _count: { + children: children.length, + topics: child.topics.length, + posts: child.topics.reduce( + (acc, item) => acc + item.posts.length, + 0 + ) + } }; }) ) diff --git a/backend/src/forum/forums/show/dto/show.obj.ts b/backend/src/forum/forums/show/dto/show.obj.ts index 6acd8874b..7cd575f41 100644 --- a/backend/src/forum/forums/show/dto/show.obj.ts +++ b/backend/src/forum/forums/show/dto/show.obj.ts @@ -1,10 +1,22 @@ -import { Field, Int, ObjectType } from "@nestjs/graphql"; +import { Field, Int, ObjectType, OmitType } from "@nestjs/graphql"; import { PageInfo } from "@/types/database/pagination.type"; import { TextLanguage } from "@/types/database/text-language.type"; @ObjectType() -class PermissionsForumForumsCount { +class ShowForumForumsCounts { + @Field(() => Int) + children: number; + + @Field(() => Int) + topics: number; + + @Field(() => Int) + posts: number; +} + +@ObjectType() +class PermissionsForumForums { @Field(() => Boolean) can_read: boolean; @@ -31,18 +43,26 @@ export class ShowForumForums { @Field(() => Int) created: number; + + @Field(() => ShowForumForumsCounts) + _count: ShowForumForumsCounts; } @ObjectType() export class FirstShowForumForums extends ShowForumForums { - @Field(() => PermissionsForumForumsCount) - permissions: PermissionsForumForumsCount; + @Field(() => PermissionsForumForums) + permissions: PermissionsForumForums; } +@ObjectType() +class LastChildShowForumForums extends OmitType(ShowForumForums, [ + "_count" +] as const) {} + @ObjectType() export class ChildrenShowForumForums extends ShowForumForums { - @Field(() => [ShowForumForums]) - children: ShowForumForums[]; + @Field(() => [LastChildShowForumForums]) + children: LastChildShowForumForums[]; } @ObjectType() diff --git a/backend/src/forum/forums/show/show.service.ts b/backend/src/forum/forums/show/show.service.ts index ae60535e8..bac436bd9 100644 --- a/backend/src/forum/forums/show/show.service.ts +++ b/backend/src/forum/forums/show/show.service.ts @@ -103,6 +103,11 @@ export class ShowForumForumsService { description: true, permissions: true } + }, + topics: { + with: { + posts: true + } } } }); @@ -120,16 +125,28 @@ export class ShowForumForumsService { with: { name: true, description: true, - permissions: true + permissions: true, + topics: { + with: { + posts: true + } + } } }); return { ...forum, parent: forum.parent_id - ? { ...forum.parent, _count: { children: 0 } } + ? { ...forum.parent, _count: { children: 0, topics: 0, posts: 0 } } : null, - _count: { children: children.length }, + _count: { + children: children.length, + topics: forum.topics.length, + posts: forum.topics.reduce( + (acc, item) => acc + item.posts.length, + 0 + ) + }, children: await Promise.all( children.map(async child => { const children = @@ -142,14 +159,26 @@ export class ShowForumForumsService { with: { name: true, description: true, - permissions: true + permissions: true, + topics: { + with: { + posts: true + } + } } }); return { ...child, children, - _count: { children: children.length } + _count: { + children: children.length, + topics: child.topics.length, + posts: child.topics.reduce( + (acc, item) => acc + item.posts.length, + 0 + ) + } }; }) ) diff --git a/backend/src/schema.gql b/backend/src/schema.gql index 262ea3fd8..0ce7ec637 100644 --- a/backend/src/schema.gql +++ b/backend/src/schema.gql @@ -33,7 +33,8 @@ type AvatarUser { } type ChildrenShowForumForums { - children: [ShowForumForums!]! + _count: ShowForumForumsCounts! + children: [LastChildShowForumForums!]! created: Int! description: [TextLanguage!]! id: Int! @@ -42,6 +43,7 @@ type ChildrenShowForumForums { } type CreateForumForumsObj { + _count: ShowForumForumsCounts! children: [ChildrenShowForumForums!]! created: Int! description: [TextLanguage!]! @@ -81,6 +83,14 @@ type GroupsPermissionsForumForums { id: Int! } +type LastChildShowForumForums { + created: Int! + description: [TextLanguage!]! + id: Int! + name: [TextLanguage!]! + position: Int! +} + enum LayoutAdminInstallEnum { ACCOUNT DATABASE @@ -151,6 +161,12 @@ input PermissionsCreateForumForums { } type PermissionsForumForums { + can_create: Boolean! + can_read: Boolean! + can_reply: Boolean! +} + +type PermissionsForumForumsAdmin { can_all_create: Boolean! can_all_read: Boolean! can_all_reply: Boolean! @@ -158,12 +174,6 @@ type PermissionsForumForums { groups: [GroupsPermissionsForumForums!]! } -type PermissionsForumForumsCount { - can_create: Boolean! - can_read: Boolean! - can_reply: Boolean! -} - type Query { admin__core_groups__show(cursor: Int, first: Int, last: Int, search: String, sortBy: [ShowAdminGroupsSortByArgs!]): ShowAdminGroupsObj! admin__core_members__show(cursor: Int, first: Int, groups: [Int!], last: Int, search: String, sortBy: [ShowAdminMembersSortByArgs!]): ShowAdminMembersObj! @@ -455,6 +465,7 @@ type ShowCoreThemesObj { } type ShowForumForums { + _count: ShowForumForumsCounts! created: Int! description: [TextLanguage!]! id: Int! @@ -463,13 +474,14 @@ type ShowForumForums { } type ShowForumForumsAdmin { + _count: ShowForumForumsCounts! children: [ChildrenShowForumForums!]! created: Int! description: [TextLanguage!]! id: Int! name: [TextLanguage!]! parent: ShowForumForums - permissions: PermissionsForumForums! + permissions: PermissionsForumForumsAdmin! position: Int! } @@ -478,19 +490,26 @@ type ShowForumForumsAdminObj { pageInfo: PageInfo! } +type ShowForumForumsCounts { + children: Int! + posts: Int! + topics: Int! +} + type ShowForumForumsObj { edges: [ShowForumForumsWithParent!]! pageInfo: PageInfo! } type ShowForumForumsWithParent { + _count: ShowForumForumsCounts! children: [ChildrenShowForumForums!]! created: Int! description: [TextLanguage!]! id: Int! name: [TextLanguage!]! parent: ShowForumForums - permissions: PermissionsForumForumsCount! + permissions: PermissionsForumForums! position: Int! }