Skip to content

Commit

Permalink
feat(forum): Add last posts into show forums query
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Mar 2, 2024
1 parent 169ff8f commit 7187c1a
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 15 deletions.
41 changes: 41 additions & 0 deletions backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ChildrenShowForumForums {
created: Int!
description: [TextLanguage!]!
id: Int!
last_posts: LastPostsShowForumForumsObj!
name: [TextLanguage!]!
position: Int!
}
Expand All @@ -48,6 +49,7 @@ type CreateForumForumsObj {
created: Int!
description: [TextLanguage!]!
id: Int!
last_posts: LastPostsShowForumForumsObj!
name: [TextLanguage!]!
position: Int!
}
Expand Down Expand Up @@ -90,6 +92,36 @@ type LastChildShowForumForums {
position: Int!
}

type LastPostsShowForumForums {
content: [TextLanguage!]!
created: Int!
id: Int!
topic: TopicLastPostsShowForumForums!
user: User!
}

input LastPostsShowForumForumsArgs {
cursor: Int
first: Int
last: Int
sortBy: [LastPostsShowForumForumsSortByArgs!]
}

type LastPostsShowForumForumsObj {
edges: [LastPostsShowForumForums!]!
pageInfo: PageInfo!
}

input LastPostsShowForumForumsSortByArgs {
column: LastPostsShowForumForumsSortingColumnEnum!
direction: SortDirectionEnum!
}

enum LastPostsShowForumForumsSortingColumnEnum {
created
updated
}

enum LayoutAdminInstallEnum {
ACCOUNT
DATABASE
Expand Down Expand Up @@ -185,6 +217,7 @@ type Query {
first: Int
ids: [Int!]
last: Int
last_posts_args: LastPostsShowForumForumsArgs
parent_id: Int
search: String

Expand All @@ -204,6 +237,7 @@ type Query {
first: Int
ids: [Int!]
last: Int
last_posts_args: LastPostsShowForumForumsArgs
parent_id: Int
search: String

Expand Down Expand Up @@ -470,6 +504,7 @@ type ShowForumForumsAdmin {
created: Int!
description: [TextLanguage!]!
id: Int!
last_posts: LastPostsShowForumForumsObj!
name: [TextLanguage!]!
permissions: PermissionsForumForumsAdmin!
position: Int!
Expand Down Expand Up @@ -498,6 +533,7 @@ type ShowForumForumsWithChildren {
created: Int!
description: [TextLanguage!]!
id: Int!
last_posts: LastPostsShowForumForumsObj!
name: [TextLanguage!]!
permissions: PermissionsForumForums!
position: Int!
Expand Down Expand Up @@ -581,6 +617,11 @@ enum TopicActions {
unlock
}

type TopicLastPostsShowForumForums {
id: Int!
title: [TextLanguage!]!
}

"""The `Upload` scalar type represents a file upload."""
scalar Upload

Expand Down
4 changes: 3 additions & 1 deletion backend/src/apps/forum/forums/forums.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { Module } from "@nestjs/common";
import { ShowForumForumsResolver } from "./show/show.resolver";
import { ShowForumForumsService } from "./show/show.service";
import { StatsShowForumForumsService } from "./show/stats.service";
import { LastPostsForumForumsService } from "./show/last_posts/last_posts.service";

@Module({
providers: [
ShowForumForumsResolver,
ShowForumForumsService,
StatsShowForumForumsService
StatsShowForumForumsService,
LastPostsForumForumsService
],
exports: [ShowForumForumsService, StatsShowForumForumsService]
})
Expand Down
5 changes: 5 additions & 0 deletions backend/src/apps/forum/forums/show/dto/show.args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ArgsType, Field, Int } from "@nestjs/graphql";

import { LastPostsShowForumForumsArgs } from "../last_posts/dto/last_posts.args";

@ArgsType()
export class ShowForumForumsArgs {
@Field(() => Int, { nullable: true })
Expand All @@ -25,4 +27,7 @@ export class ShowForumForumsArgs {

@Field(() => String, { nullable: true })
search: string | null;

@Field(() => LastPostsShowForumForumsArgs, { nullable: true })
last_posts_args: LastPostsShowForumForumsArgs | null;
}
8 changes: 7 additions & 1 deletion backend/src/apps/forum/forums/show/dto/show.obj.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Field, Int, ObjectType, OmitType } from "@nestjs/graphql";

import { LastPostsShowForumForumsObj } from "../last_posts/dto/last_posts.obj";

import { PageInfo } from "@/types/database/pagination.type";
import { TextLanguage } from "@/types/database/text-language.type";

Expand Down Expand Up @@ -49,6 +51,9 @@ export class ShowForumForums {

@Field(() => ShowForumForumsCounts)
_count: ShowForumForumsCounts;

@Field(() => LastPostsShowForumForumsObj)
last_posts: LastPostsShowForumForumsObj;
}

@ObjectType()
Expand All @@ -59,7 +64,8 @@ export class FirstShowForumForums extends ShowForumForums {

@ObjectType()
class LastChildShowForumForums extends OmitType(ShowForumForums, [
"_count"
"_count",
"last_posts"
] as const) {}

@ObjectType()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Field, InputType, Int, registerEnumType } from "@nestjs/graphql";

import { SortDirectionEnum } from "@/types/database/sortDirection.type";

enum LastPostsShowForumForumsSortingColumnEnum {
created = "created",
updated = "updated"
}

registerEnumType(LastPostsShowForumForumsSortingColumnEnum, {
name: "LastPostsShowForumForumsSortingColumnEnum"
});

@InputType()
class LastPostsShowForumForumsSortByArgs {
@Field(() => LastPostsShowForumForumsSortingColumnEnum)
column: LastPostsShowForumForumsSortingColumnEnum;

@Field(() => SortDirectionEnum)
direction: SortDirectionEnum;
}

@InputType()
export class LastPostsShowForumForumsArgs {
@Field(() => Int, { nullable: true })
cursor: number | null;

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

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

@Field(() => [LastPostsShowForumForumsSortByArgs], { nullable: true })
sortBy: LastPostsShowForumForumsSortByArgs[] | null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Field, Int, ObjectType } from "@nestjs/graphql";

import { PageInfo } from "@/types/database/pagination.type";
import { TextLanguage } from "@/types/database/text-language.type";
import { User } from "@/utils/decorators/user.decorator";

@ObjectType()
export class TopicLastPostsShowForumForums {
@Field(() => Int)
id: number;

@Field(() => [TextLanguage])
title: TextLanguage[];
}

@ObjectType()
export class LastPostsShowForumForums {
@Field(() => Int)
id: number;

@Field(() => Int)
created: number;

@Field(() => User)
user: User;

@Field(() => [TextLanguage])
content: TextLanguage[];

@Field(() => TopicLastPostsShowForumForums)
topic: TopicLastPostsShowForumForums;
}

@ObjectType()
export class LastPostsShowForumForumsObj {
@Field(() => [LastPostsShowForumForums])
edges: LastPostsShowForumForums[];

@Field(() => PageInfo)
pageInfo: PageInfo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Injectable } from "@nestjs/common";
import { and, count, inArray } from "drizzle-orm";

import { LastPostsShowForumForumsArgs } from "./dto/last_posts.args";
import { LastPostsShowForumForumsObj } from "./dto/last_posts.obj";

import { DatabaseService } from "@/database/database.service";
import {
inputPaginationCursor,
outputPagination
} from "@/functions/database/pagination";
import { forum_posts } from "@/apps/admin/forum/database/schema/posts";
import { SortDirectionEnum } from "@/types/database/sortDirection.type";

interface Args extends LastPostsShowForumForumsArgs {
topicIds: number[];
}

@Injectable()
export class LastPostsForumForumsService {
constructor(private databaseService: DatabaseService) {}

async lastPosts({
cursor,
first,
last,
sortBy,
topicIds
}: Args): Promise<LastPostsShowForumForumsObj> {
const pagination = await inputPaginationCursor({
cursor,
database: forum_posts,
databaseService: this.databaseService,
first,
last,
primaryCursor: { order: "ASC", key: "id", schema: forum_posts.id },
defaultSortBy: {
direction: SortDirectionEnum.desc,
column: "created"
},
sortBy
});

const where =
topicIds.length > 0 ? inArray(forum_posts.topic_id, topicIds) : undefined;

const edges = await this.databaseService.db.query.forum_posts.findMany({
...pagination,
where: and(pagination.where, where),
with: {
topic: {
with: {
title: true
}
},
content: true,
user: {
with: {
group: {
with: {
name: true
}
},
avatar: true
}
}
}
});

const totalCount = await this.databaseService.db
.select({ count: count() })
.from(forum_posts)
.where(where);

return outputPagination({
edges,
totalCount,
first,
cursor,
last
});
}
}
21 changes: 18 additions & 3 deletions backend/src/apps/forum/forums/show/show.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ShowForumForumsWithChildren
} from "./dto/show.obj";
import { StatsShowForumForumsService } from "./stats.service";
import { LastPostsForumForumsService } from "./last_posts/last_posts.service";

import { User } from "@/utils/decorators/user.decorator";
import { AccessDeniedError } from "@/utils/errors/AccessDeniedError";
Expand Down Expand Up @@ -46,7 +47,8 @@ interface ShowForumForumsWithPermissions
export class ShowForumForumsService {
constructor(
private databaseService: DatabaseService,
private statsService: StatsShowForumForumsService
private statsService: StatsShowForumForumsService,
private lastPostsService: LastPostsForumForumsService
) {}

protected async whereAccessToView({
Expand Down Expand Up @@ -84,6 +86,7 @@ export class ShowForumForumsService {
ids,
isAdmin,
last,
last_posts_args,
parent_id,
search,
show_all_forums
Expand Down Expand Up @@ -158,10 +161,17 @@ export class ShowForumForumsService {
}
});

const stats = await this.statsService.stats({ forumId: forum.id });
const { stats, topic_ids } = await this.statsService.topicsPosts({
forumId: forum.id
});
const last_posts = await this.lastPostsService.lastPosts({
topicIds: topic_ids,
...last_posts_args
});

return {
...forum,
last_posts,
_count: {
...stats
},
Expand All @@ -178,13 +188,18 @@ export class ShowForumForumsService {
}
});

const stats = await this.statsService.stats({
const { stats, topic_ids } = await this.statsService.topicsPosts({
forumId: child.id
});
const last_posts = await this.lastPostsService.lastPosts({
topicIds: topic_ids,
...last_posts_args
});

return {
...child,
children,
last_posts,
_count: {
...stats
}
Expand Down
Loading

0 comments on commit 7187c1a

Please sign in to comment.