diff --git a/backend/src/modules/post/post.controller.ts b/backend/src/modules/post/post.controller.ts new file mode 100644 index 0000000..d3fd697 --- /dev/null +++ b/backend/src/modules/post/post.controller.ts @@ -0,0 +1,28 @@ +import { Body, Controller, Get, Param, Post } from '@nestjs/common'; +import { ApiOperation, ApiTags } from '@nestjs/swagger'; +import { ICreatePost } from './post.dto'; +import { PostService } from './post.service'; + +@ApiTags('Post') +@Controller('post') +export class PostController { + constructor(private postService: PostService) {} + + @ApiOperation({ description: 'Creates a new post' }) + @Post('new') + async newPost(@Body() data: ICreatePost) { + return await this.postService.newPost(data); + } + + @ApiOperation({ description: 'Getting all posts' }) + @Get('get-all') + async getAllPosts() { + return await this.postService.getAllPosts(); + } + + @ApiOperation({ description: 'Getting a post bu uuid' }) + @Get('get/:postId') + async getPost(@Param('postId') postId: string) { + return await this.postService.getPost(postId); + } +} diff --git a/backend/src/modules/post/post.dto.ts b/backend/src/modules/post/post.dto.ts new file mode 100644 index 0000000..9a794d0 --- /dev/null +++ b/backend/src/modules/post/post.dto.ts @@ -0,0 +1,9 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class ICreatePost { + @ApiProperty({ description: 'Post text', example: 'Post text' }) readonly text!: string; + + @ApiProperty({ description: 'An id of user that creating post', example: '1234' }) readonly from_user_id!: string; + + @ApiProperty({ description: 'Post media group id', example: '123' }) readonly media_group_id?: string; +} diff --git a/backend/src/modules/post/post.module.ts b/backend/src/modules/post/post.module.ts new file mode 100644 index 0000000..9cabfe0 --- /dev/null +++ b/backend/src/modules/post/post.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { LibsModule } from 'libs/libs.module'; +import { PostController } from './post.controller'; +import { PostService } from './post.service'; + +@Module({ + imports: [LibsModule], + controllers: [PostController], + providers: [PostService], +}) +export class PostModule {} diff --git a/backend/src/modules/post/post.service.ts b/backend/src/modules/post/post.service.ts new file mode 100644 index 0000000..c3a347b --- /dev/null +++ b/backend/src/modules/post/post.service.ts @@ -0,0 +1,50 @@ +import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Admin } from 'libs/database/admin.entity'; +import { Post } from 'libs/database/post.entity'; +import { Repository } from 'typeorm'; +import { ICreatePost } from './post.dto'; + +@Injectable() +export class PostService { + private readonly logger: Logger = new Logger(PostService.name); + constructor( + @InjectRepository(Post) private postRepository: Repository, + @InjectRepository(Admin) private adminRepository: Repository, + ) {} + + async newPost(data: ICreatePost) { + try { + this.logger.log(`[post.newPost] data: ${JSON.stringify(data)}`); + const user = await this.adminRepository.findOne({ where: { user: { id: data.from_user_id } }, relations: { user: true } }); + const result = await this.postRepository.save({ + text: data.text, + media_group_id: data.media_group_id, + from_user: user, + timestamp: new Date(), + }); + this.logger.log(`Created new post: ${result.uuid}`); + return { result: 'ok' }; + } catch (error) { + this.logger.log(`[post.newPost] error: ${JSON.stringify(error)}`); + } + } + + async getAllPosts() { + try { + return await this.postRepository.find(); + } catch (error) { + this.logger.log(`[post.getAllPosts] error: ${JSON.stringify(error)}`); + } + } + + async getPost(postId: string) { + try { + this.logger.log(`[post.getPost] data: ${postId}`); + return await this.postRepository.findOne({ where: { uuid: postId } }); + } catch (error) { + this.logger.log(`[post.getPost] error: ${JSON.stringify(error)}`); + throw new HttpException('No post with this id', HttpStatus.NOT_FOUND); + } + } +}