Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forum): Add last post in forum item #249

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 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,35 @@ type LastChildShowForumForums {
position: Int!
}

type LastPostsShowForumForums {
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 +216,7 @@ type Query {
first: Int
ids: [Int!]
last: Int
last_posts_args: LastPostsShowForumForumsArgs
parent_id: Int
search: String

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

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

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

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

Expand Down
11 changes: 11 additions & 0 deletions backend/src/apps/admin/forum/forums/create/create.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ export class CreateForumForumsService {
total_posts: 0,
total_topics: 0
},
last_posts: {
edges: [],
pageInfo: {
count: 0,
totalCount: 0,
hasNextPage: false,
hasPreviousPage: false,
startCursor: null,
endCursor: null
}
},
children: []
};
}
Expand Down
29 changes: 26 additions & 3 deletions backend/src/apps/admin/forum/forums/edit/edit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import {
} from "../../database/schema/forums";
import { TextLanguageInput } from "@/types/database/text-language.type";
import { StatsShowForumForumsService } from "@/apps/forum/forums/show/stats.service";
import { LastPostsForumForumsService } from "@/apps/forum/forums/show/last_posts/last_posts.service";

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

protected updateName = async ({
Expand Down Expand Up @@ -244,19 +246,40 @@ export class EditForumForumsService {
}
});

const stats = await this.statsService.stats({ forumId: id });
const { stats, topic_ids } = await this.statsService.topicsPosts({
forumId: id
});
const last_posts = await this.lastPostsService.lastPosts({
topicIds: topic_ids,
first: null,
cursor: null,
last: null,
sortBy: null
});

return {
...dataUpdate,
last_posts,
_count: {
...stats
},
children: await Promise.all(
children.map(async item => {
const stats = await this.statsService.stats({ forumId: item.id });
const { stats, topic_ids } = await this.statsService.topicsPosts({
forumId: item.id
});

const last_posts = await this.lastPostsService.lastPosts({
topicIds: topic_ids,
first: null,
cursor: null,
last: null,
sortBy: null
});

return {
...item,
last_posts,
children: [],
_count: {
...stats
Expand Down
10 changes: 8 additions & 2 deletions backend/src/apps/forum/forums/forums.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ 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]
exports: [
ShowForumForumsService,
StatsShowForumForumsService,
LastPostsForumForumsService
]
})
export class ForumsForumModule {}
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,38 @@
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(() => 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,82 @@
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
}
},
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
});
}
}
Loading
Loading