Skip to content

Commit

Permalink
feat(forum): Add _count children, topics, posts in show query forum
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Feb 28, 2024
1 parent 4d6854a commit 79107bc
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 38 deletions.
5 changes: 4 additions & 1 deletion backend/src/admin/forum/database/schema/forums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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)
})
);

Expand Down
25 changes: 23 additions & 2 deletions backend/src/admin/forum/forums/create/create.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ export class CreateForumForumsService {
parent: {
with: {
name: true,
description: true
description: true,
topics: {
with: {
posts: true
}
}
}
}
}
Expand All @@ -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
)
}
}
};
}
}
45 changes: 39 additions & 6 deletions backend/src/admin/forum/forums/edit/edit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,17 @@ export class EditForumForumsService {
parent: {
with: {
name: true,
description: true
description: true,
topics: {
with: {
posts: true
}
}
}
},
topics: {
with: {
posts: true
}
}
}
Expand All @@ -243,21 +253,44 @@ export class EditForumForumsService {
name: true,
description: true,
permissions: true,
parent: {
topics: {
with: {
name: true,
description: true
posts: true
}
}
}
});

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
}
}
};
}
}
6 changes: 3 additions & 3 deletions backend/src/admin/forum/forums/show/dto/show.obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GroupsPermissionsForumForums {
}

@ObjectType()
export class PermissionsForumForums {
export class PermissionsForumForumsAdmin {
@Field(() => Boolean)
can_all_view: boolean;

Expand All @@ -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()
Expand Down
51 changes: 45 additions & 6 deletions backend/src/admin/forum/forums/show/show.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
});

Expand All @@ -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,
Expand All @@ -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 =
Expand All @@ -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
)
}
};
})
)
Expand Down
32 changes: 26 additions & 6 deletions backend/src/forum/forums/show/dto/show.obj.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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()
Expand Down
39 changes: 34 additions & 5 deletions backend/src/forum/forums/show/show.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export class ShowForumForumsService {
description: true,
permissions: true
}
},
topics: {
with: {
posts: true
}
}
}
});
Expand All @@ -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 =
Expand All @@ -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
)
}
};
})
)
Expand Down
Loading

0 comments on commit 79107bc

Please sign in to comment.