Skip to content

Commit

Permalink
Merge pull request #239 from aXenDeveloper/forum/stats
Browse files Browse the repository at this point in the history
feat(forum): Add stats to forum item
  • Loading branch information
aXenDeveloper authored Mar 1, 2024
2 parents a9531cf + 9a66625 commit 0bbfab6
Show file tree
Hide file tree
Showing 39 changed files with 1,319 additions and 1,115 deletions.
4 changes: 2 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
"@types/archiver": "^6.0.2",
"@types/bcrypt": "^5.0.2",
"@types/busboy": "^1.5.3",
"@types/cookie-parser": "^1.4.6",
"@types/cookie-parser": "^1.4.7",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.22",
"@types/node": "^20.11.24",
"@types/object-path": "^0.11.4",
"@types/pg": "^8.11.2",
"@types/supertest": "^6.0.2",
Expand Down
20 changes: 5 additions & 15 deletions backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type CreateForumForumsObj {
description: [TextLanguage!]!
id: Int!
name: [TextLanguage!]!
parent: ShowForumForums
position: Int!
}

Expand Down Expand Up @@ -184,6 +183,7 @@ type Query {
admin__forum_forums__show(
cursor: Int
first: Int
ids: [Int!]
last: Int
parent_id: Int
search: String
Expand Down Expand Up @@ -464,23 +464,13 @@ type ShowCoreThemesObj {
pageInfo: PageInfo!
}

type ShowForumForums {
_count: ShowForumForumsCounts!
created: Int!
description: [TextLanguage!]!
id: Int!
name: [TextLanguage!]!
position: Int!
}

type ShowForumForumsAdmin {
_count: ShowForumForumsCounts!
children: [ChildrenShowForumForums!]!
created: Int!
description: [TextLanguage!]!
id: Int!
name: [TextLanguage!]!
parent: ShowForumForums
permissions: PermissionsForumForumsAdmin!
position: Int!
}
Expand All @@ -491,24 +481,24 @@ type ShowForumForumsAdminObj {
}

type ShowForumForumsCounts {
children: Int!
posts: Int!
topics: Int!
total_posts: Int!
total_topics: Int!
}

type ShowForumForumsObj {
edges: [ShowForumForumsWithParent!]!
edges: [ShowForumForumsWithChildren!]!
pageInfo: PageInfo!
}

type ShowForumForumsWithParent {
type ShowForumForumsWithChildren {
_count: ShowForumForumsCounts!
children: [ChildrenShowForumForums!]!
created: Int!
description: [TextLanguage!]!
id: Int!
name: [TextLanguage!]!
parent: ShowForumForums
permissions: PermissionsForumForums!
position: Int!
}
Expand Down
5 changes: 4 additions & 1 deletion backend/src/apps/admin/forum/forum.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { EditForumForumsService } from "./forums/edit/edit.service";
import { DeleteForumForumsResolver } from "./forums/delete/delete.resolver";
import { DeleteForumForumsService } from "./forums/delete/delete.service";

import { ForumsForumModule } from "@/apps/forum/forums/forums.module";

@Module({
providers: [
CreateForumForumsService,
Expand All @@ -23,6 +25,7 @@ import { DeleteForumForumsService } from "./forums/delete/delete.service";
EditForumForumsService,
DeleteForumForumsResolver,
DeleteForumForumsService
]
],
imports: [ForumsForumModule]
})
export class AdminForumModule {}
33 changes: 5 additions & 28 deletions backend/src/apps/admin/forum/forums/create/create.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,7 @@ export class CreateForumForumsService {
with: {
name: true,
description: true,
permissions: true,
parent: {
with: {
name: true,
description: true,
topics: {
with: {
posts: true
}
}
}
}
permissions: true
}
});

Expand All @@ -116,24 +105,12 @@ export class CreateForumForumsService {
return {
...forum,
_count: {
children: 0,
topics: 0,
posts: 0
posts: 0,
total_posts: 0,
total_topics: 0
},
children: [],
parent: forum.parent
? {
...forum.parent,
_count: {
children: 0,
topics: forum.parent.topics.length,
posts: forum.parent.topics.reduce(
(acc, item) => acc + item.posts.length,
0
)
}
}
: null
children: []
};
}
}
9 changes: 5 additions & 4 deletions backend/src/apps/admin/forum/forums/create/dto/create.obj.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ObjectType, OmitType } from "@nestjs/graphql";

import { ShowForumForumsWithParent } from "../../../../../forum/forums/show/dto/show.obj";
import { ShowForumForumsWithChildren } from "../../../../../forum/forums/show/dto/show.obj";

@ObjectType()
export class CreateForumForumsObj extends OmitType(ShowForumForumsWithParent, [
"permissions"
] as const) {}
export class CreateForumForumsObj extends OmitType(
ShowForumForumsWithChildren,
["permissions"] as const
) {}
71 changes: 21 additions & 50 deletions backend/src/apps/admin/forum/forums/edit/edit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import {
forum_forums_permissions
} from "../../database/schema/forums";
import { TextLanguageInput } from "@/types/database/text-language.type";
import { StatsShowForumForumsService } from "@/apps/forum/forums/show/stats.service";

@Injectable()
export class EditForumForumsService {
constructor(private databaseService: DatabaseService) {}
constructor(
private databaseService: DatabaseService,
private statsService: StatsShowForumForumsService
) {}

protected updateName = async ({
id,
Expand Down Expand Up @@ -227,23 +231,7 @@ export class EditForumForumsService {
with: {
name: true,
description: true,
permissions: true,
parent: {
with: {
name: true,
description: true,
topics: {
with: {
posts: true
}
}
}
},
topics: {
with: {
posts: true
}
}
permissions: true
}
});

Expand All @@ -252,47 +240,30 @@ export class EditForumForumsService {
with: {
name: true,
description: true,
permissions: true,
topics: {
with: {
posts: true
}
}
permissions: true
}
});

const stats = await this.statsService.stats({ forumId: id });

return {
...dataUpdate,
_count: {
children: children.length,
topics: dataUpdate.topics.length,
posts: dataUpdate.topics.reduce(
(acc, item) => acc + item.posts.length,
0
)
...stats
},
children: children.map(item => ({
...item,
children: [],
_count: {
children: 0,
posts: item.topics.reduce((acc, item) => acc + item.posts.length, 0),
topics: item.topics.length
}
})),
parent: dataUpdate.parent
? {
...dataUpdate.parent,
children: await Promise.all(
children.map(async item => {
const stats = await this.statsService.stats({ forumId: item.id });

return {
...item,
children: [],
_count: {
children: 0,
posts: dataUpdate.parent.topics.reduce(
(acc, item) => acc + item.posts.length,
0
),
topics: dataUpdate.parent.topics.length
...stats
}
}
: null
};
})
)
};
}
}
8 changes: 0 additions & 8 deletions backend/src/apps/admin/forum/forums/show/dto/show.args.ts

This file was deleted.

9 changes: 5 additions & 4 deletions backend/src/apps/admin/forum/forums/show/dto/show.obj.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Field, Int, ObjectType, OmitType } from "@nestjs/graphql";

import { ShowForumForumsWithParent } from "../../../../../forum/forums/show/dto/show.obj";
import { ShowForumForumsWithChildren } from "../../../../../forum/forums/show/dto/show.obj";
import { PageInfo } from "@/types/database/pagination.type";

@ObjectType()
Expand Down Expand Up @@ -40,9 +40,10 @@ export class PermissionsForumForumsAdmin {
}

@ObjectType()
export class ShowForumForumsAdmin extends OmitType(ShowForumForumsWithParent, [
"permissions"
] as const) {
export class ShowForumForumsAdmin extends OmitType(
ShowForumForumsWithChildren,
["permissions"] as const
) {
@Field(() => PermissionsForumForumsAdmin)
permissions: PermissionsForumForumsAdmin;
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/apps/admin/forum/forums/show/show.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { UseGuards } from "@nestjs/common";

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

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

@Resolver()
export class ShowForumForumsAdminResolver {
Expand All @@ -14,7 +14,7 @@ export class ShowForumForumsAdminResolver {
@Query(() => ShowForumForumsAdminObj)
@UseGuards(AdminAuthGuards)
async admin__forum_forums__show(
@Args() args: ShowForumForumsAdminArgs
@Args() args: ShowForumForumsArgs
): Promise<ShowForumForumsAdminObj> {
return await this.service.show(args);
}
Expand Down
Loading

0 comments on commit 0bbfab6

Please sign in to comment.