Skip to content

Commit

Permalink
feat(forum): Add show_all_forums for query forums
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Feb 27, 2024
1 parent e4c52fc commit 69f1008
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 33 deletions.
8 changes: 8 additions & 0 deletions backend/src/admin/forum/forums/show/dto/show.args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ArgsType, OmitType } from "@nestjs/graphql";

import { ShowForumForumsArgs } from "@/src/forum/forums/show/dto/show.args";

@ArgsType()
export class ShowForumForumsAdminArgs extends OmitType(ShowForumForumsArgs, [
"ids"
] as const) {}
4 changes: 2 additions & 2 deletions backend/src/admin/forum/forums/show/show.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { UseGuards } from "@nestjs/common";

import { ShowForumForumsAdminService } from "./show.service";
import { ShowForumForumsAdminObj } from "./dto/show.obj";
import { ShowForumForumsAdminArgs } from "./dto/show.args";

import { ShowForumForumsArgs } from "../../../../forum/forums/show/dto/show.args";
import { AdminAuthGuards } from "@/utils/guards/admin-auth.guards";

@Resolver()
Expand All @@ -14,7 +14,7 @@ export class ShowForumForumsAdminResolver {
@Query(() => ShowForumForumsAdminObj)
@UseGuards(AdminAuthGuards)
async admin__forum_forums__show(
@Args() args: ShowForumForumsArgs
@Args() args: ShowForumForumsAdminArgs
): Promise<ShowForumForumsAdminObj> {
return await this.service.show(args);
}
Expand Down
30 changes: 16 additions & 14 deletions backend/src/admin/forum/forums/show/show.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Injectable } from "@nestjs/common";
import { and, count, eq, isNull } from "drizzle-orm";

import { ShowForumForumsAdminObj } from "./dto/show.obj";
import { ShowForumForumsAdminArgs } from "./dto/show.args";

import { ShowForumForumsArgs } from "../../../../forum/forums/show/dto/show.args";
import { DatabaseService } from "@/database/database.service";
import {
inputPaginationCursor,
Expand All @@ -20,8 +20,9 @@ export class ShowForumForumsAdminService {
cursor,
first,
last,
parent_id
}: ShowForumForumsArgs): Promise<ShowForumForumsAdminObj> {
parent_id,
show_all_forums
}: ShowForumForumsAdminArgs): Promise<ShowForumForumsAdminObj> {
const pagination = await inputPaginationCursor({
cursor,
database: forum_forums,
Expand All @@ -41,7 +42,7 @@ export class ShowForumForumsAdminService {

const forums = await this.databaseService.db.query.forum_forums.findMany({
...pagination,
where: and(pagination.where, where),
where: show_all_forums ? pagination.where : and(pagination.where, where),
with: {
name: true,
description: true,
Expand All @@ -62,16 +63,17 @@ export class ShowForumForumsAdminService {

const edges = await Promise.all(
forums.map(async forum => {
const children =
await this.databaseService.db.query.forum_forums.findMany({
where: eq(forum_forums.parent_id, forum.id),
orderBy: (table, { asc }) => [asc(table.position)],
with: {
name: true,
description: true,
permissions: true
}
});
const children = show_all_forums
? []
: await this.databaseService.db.query.forum_forums.findMany({
where: eq(forum_forums.parent_id, forum.id),
orderBy: (table, { asc }) => [asc(table.position)],
with: {
name: true,
description: true,
permissions: true
}
});

return {
...forum,
Expand Down
6 changes: 6 additions & 0 deletions backend/src/forum/forums/show/dto/show.args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ export class ShowForumForumsArgs {

@Field(() => [Int], { nullable: true })
ids: number[] | null;

@Field(() => Boolean, {
nullable: true,
description: "Show all forums without children"
})
show_all_forums: boolean;
}
44 changes: 29 additions & 15 deletions backend/src/forum/forums/show/show.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ export class ShowForumForumsService {
constructor(private databaseService: DatabaseService) {}

async show(
{ cursor, first, ids, last, parent_id }: ShowForumForumsArgs,
{
cursor,
first,
ids,
last,
parent_id,
show_all_forums
}: ShowForumForumsArgs,
user: User | null
): Promise<ShowForumForumsObj> {
const pagination = await inputPaginationCursor({
Expand Down Expand Up @@ -55,14 +62,17 @@ export class ShowForumForumsService {
)
);

const whereParent = !parent_id
? isNull(forum_forums.parent_id)
: eq(forum_forums.parent_id, parent_id);
const whereParent = parent_id
? eq(forum_forums.parent_id, parent_id)
: isNull(forum_forums.parent_id);

const idsCondition =
ids?.length > 0 ? inArray(forum_forums.id, ids) : undefined;

const where = and(
pagination.where,
wherePermissions,
ids?.length > 0 ? inArray(forum_forums.id, ids) : whereParent
show_all_forums ? idsCondition : idsCondition || whereParent
);

const forums = await this.databaseService.db.query.forum_forums.findMany({
Expand All @@ -84,16 +94,20 @@ export class ShowForumForumsService {

const edges = await Promise.all(
forums.map(async forum => {
const children =
await this.databaseService.db.query.forum_forums.findMany({
where: and(eq(forum_forums.parent_id, forum.id), wherePermissions),
orderBy: (table, { asc }) => [asc(table.position)],
with: {
name: true,
description: true,
permissions: true
}
});
const children = show_all_forums
? []
: await this.databaseService.db.query.forum_forums.findMany({
where: and(
eq(forum_forums.parent_id, forum.id),
wherePermissions
),
orderBy: (table, { asc }) => [asc(table.position)],
with: {
name: true,
description: true,
permissions: true
}
});

return {
...forum,
Expand Down
21 changes: 19 additions & 2 deletions backend/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,15 @@ type Query {
admin__core_staff_administrators__show(cursor: Int, first: Int, last: Int, sortBy: [ShowAdminStaffAdministratorsSortByArgs!]): ShowAdminStaffAdministratorsObj!
admin__core_staff_moderators__show(cursor: Int, first: Int, last: Int, sortBy: [ShowAdminStaffModeratorsSortByArgs!]): ShowAdminStaffModeratorsObj!
admin__core_themes__show(cursor: Int, first: Int, last: Int, sortBy: [ShowAdminThemesSortByArgs!]): ShowAdminThemesObj!
admin__forum_forums__show(cursor: Int, first: Int, ids: [Int!], last: Int, parent_id: Int): ShowForumForumsAdminObj!
admin__forum_forums__show(
cursor: Int
first: Int
last: Int
parent_id: Int

"""Show all forums without children"""
show_all_forums: Boolean
): ShowForumForumsAdminObj!
admin__install__layout: LayoutAdminInstallObj!
admin__sessions__authorization: AuthorizationAdminSessionsObj!
admin__settings__general__show: ShowGeneralAdminSettingsObj!
Expand All @@ -179,7 +187,16 @@ type Query {
core_nav__show(cursor: Int, first: Int, last: Int): ShowCoreNavObj!
core_sessions__authorization: AuthorizationCoreSessionsObj!
core_themes__show(cursor: Int, first: Int, last: Int): ShowCoreThemesObj!
forum_forums__show(cursor: Int, first: Int, ids: [Int!], last: Int, parent_id: Int): ShowForumForumsObj!
forum_forums__show(
cursor: Int
first: Int
ids: [Int!]
last: Int
parent_id: Int

"""Show all forums without children"""
show_all_forums: Boolean
): ShowForumForumsObj!
forum_posts__show(cursor: Int, first: Int, firstEdges: Int, last: Int, sortBy: ShowPostsForumsSortingEnum, topic_id: Int!): ShowPostsForumsObj!
forum_topics__show(cursor: Int, first: Int, forum_id: Int, id: Int, last: Int): ShowTopicsForumsObj!
}
Expand Down

0 comments on commit 69f1008

Please sign in to comment.