Skip to content

Commit e966fdd

Browse files
authored
Feat/fecth posts for specific thread (#184)
* Added PostController * Added post module * added list posts by thread query * fixed lint errors * resolved PR comments
1 parent 6a0998e commit e966fdd

16 files changed

Lines changed: 160 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './post.controller';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Controller, Get, Param, Query } from '@nestjs/common';
2+
import { ApiTags } from '@nestjs/swagger';
3+
import { ICQRSHandler } from '@common/cqrs';
4+
import { PaginationDto } from '@infrastructure/pagination';
5+
import { ListPostsByThreadQuery } from '@modules/post/cqrs/query/list-posts-by-thread.query';
6+
7+
@Controller('v1/posts')
8+
@ApiTags('Posts')
9+
export class PostController {
10+
constructor(private readonly cqrsHandler: ICQRSHandler) {}
11+
12+
@Get(':threadId')
13+
async getPostsByThread(@Param('threadId') threadId: string, @Query() { page, pageSize }: PaginationDto) {
14+
const foundPosts = await this.cqrsHandler.fetch(ListPostsByThreadQuery, { page, pageSize, filter: { threadId } });
15+
return { pageInfo: foundPosts.pageInfo, items: foundPosts.items };
16+
}
17+
}

src/api/rest/rest.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { AuthenticationController } from './auth';
1212
import { ContactController } from './contact';
1313
import { ThreadController } from './threads';
1414
import { ForumController } from './forum/v1/controller';
15+
import { PostController } from './posts/v1/controller';
1516

1617
@Module({
1718
imports: [InfrastructureModule, CQRSModule],
@@ -24,6 +25,7 @@ import { ForumController } from './forum/v1/controller';
2425
ThreadController,
2526
ContactController,
2627
ForumController,
28+
PostController,
2729
],
2830
providers: [CrowdActionService, ProfileService, AuthService],
2931
exports: [CrowdActionService, ProfileService, AuthService],

src/domain/post/entity/post.entity.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,20 @@ export class Post implements IPost {
1313
readonly subject: string;
1414
readonly message: string;
1515
readonly visible: boolean;
16+
17+
constructor(entityLike: IPost) {
18+
this.id = entityLike.id;
19+
this.threadId = entityLike.threadId;
20+
this.forumId = entityLike.forumId;
21+
this.author = entityLike.author;
22+
this.createdAt = entityLike.createdAt;
23+
this.updatedAt = entityLike.updatedAt;
24+
this.subject = entityLike.subject;
25+
this.message = entityLike.message;
26+
this.visible = entityLike.visible;
27+
}
28+
29+
static create(entityLike: Post): Post {
30+
return new Post(entityLike);
31+
}
1632
}

src/domain/post/entity/reported-post.entity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ export class ReportedPost implements IPost {
1515
readonly reason: string;
1616
readonly handledById?: string;
1717
readonly status: ReportStatusEnum;
18+
19+
readonly subject: string;
20+
readonly message: string;
21+
readonly visible: boolean;
1822
}

src/domain/post/interface/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './post.interface';
2+
export * from './post-repository.interface';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { FindCriteria, IRepository } from '@core/repository.interface';
2+
import { Post } from '../entity';
3+
import { IPost } from './post.interface';
4+
5+
export type CreatePost = Omit<IPost, 'id' | 'createdAt' | 'updatedAt'>;
6+
export type PatchPost = Partial<IPost>;
7+
export type QueryPost = Partial<Pick<IPost, 'id' | 'threadId'>>;
8+
9+
export abstract class IPostRepository implements IRepository<Post, CreatePost, PatchPost, QueryPost> {
10+
abstract findAll(query: FindCriteria<QueryPost>): Promise<Post[]>;
11+
abstract create(entityLike: CreatePost): Promise<Post>;
12+
abstract patch(id: string, entityLike: PatchPost): Promise<void>;
13+
abstract delete(id: string): Promise<void>;
14+
abstract findOne(query: FindCriteria<QueryPost>): Promise<Post>;
15+
}

src/domain/post/interface/post.interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ export interface IPost {
77
readonly author: IUserInfo;
88
readonly createdAt: Date;
99
readonly updatedAt: Date;
10+
11+
readonly subject: string;
12+
readonly message: string;
13+
readonly visible: boolean;
1014
}

src/infrastructure/mongo/mongo.module.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
ThreadRepository,
2626
ForumPermissionRepository,
2727
ForumRepository,
28+
PostRepsotory,
2829
} from '@infrastructure/mongo/repository';
2930
import { ICrowdActionRepository } from '@domain/crowdaction';
3031
import { IProfileRepository } from '@domain/profile';
@@ -33,7 +34,9 @@ import { IContactRepository } from '@domain/contact';
3334
import { ICommitmentRepository } from '@domain/commitment';
3435
import { IForumPermissionRepository, IForumRepository } from '@domain/forum';
3536
import { IThreadRepository } from '@domain/thread';
37+
import { IPostRepository } from '@domain/post';
3638
import { ContactPersistence, ContactSchema } from './persistence/contact.persistence';
39+
import { PostPersistence, PostSchema } from './persistence/post.persistence';
3740

3841
@Module({
3942
imports: [
@@ -46,6 +49,7 @@ import { ContactPersistence, ContactSchema } from './persistence/contact.persist
4649
{ name: ForumPersistence.name, schema: ForumSchema },
4750
{ name: ThreadPersistence.name, schema: ThreadSchema },
4851
{ name: ForumPermissionPersistence.name, schema: ForumPermissionSchema },
52+
{ name: PostPersistence.name, schema: PostSchema },
4953
]),
5054
],
5155
providers: [
@@ -81,6 +85,10 @@ import { ContactPersistence, ContactSchema } from './persistence/contact.persist
8185
provide: IForumPermissionRepository,
8286
useClass: ForumPermissionRepository,
8387
},
88+
{
89+
provide: IPostRepository,
90+
useClass: PostRepsotory,
91+
},
8492
],
8593
exports: [
8694
ICrowdActionRepository,
@@ -91,6 +99,7 @@ import { ContactPersistence, ContactSchema } from './persistence/contact.persist
9199
IForumRepository,
92100
IThreadRepository,
93101
IForumPermissionRepository,
102+
IPostRepository,
94103
],
95104
})
96105
export class MongoModule {}

src/infrastructure/mongo/persistence/reported-post.persistence.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,14 @@ export class ReportedPostPersistence implements Omit<IPost, 'id' | 'createdAt' |
2828

2929
@Prop({ enum: ReportStatusEnum, required: true })
3030
readonly status: ReportStatusEnum;
31+
32+
@Prop({ required: false })
33+
readonly subject: string;
34+
35+
@Prop({ required: false })
36+
readonly message: string;
37+
38+
@Prop({ required: false })
39+
readonly visible: boolean;
3140
}
3241
export const ReportedPostSchema = SchemaFactory.createForClass(ReportedPostPersistence);

0 commit comments

Comments
 (0)