-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forum): Add last posts into show forums query
- Loading branch information
1 parent
169ff8f
commit 7187c1a
Showing
11 changed files
with
295 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
backend/src/apps/forum/forums/show/last_posts/dto/last_posts.args.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
41 changes: 41 additions & 0 deletions
41
backend/src/apps/forum/forums/show/last_posts/dto/last_posts.obj.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
83 changes: 83 additions & 0 deletions
83
backend/src/apps/forum/forums/show/last_posts/last_posts.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.