From 11f2dd71eb97009b8f12addbd30420b5a8817d21 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Wed, 22 Nov 2023 20:59:26 +0300 Subject: [PATCH] Added post editing --- backend/src/modules/post/post.controller.ts | 10 +++++++-- backend/src/modules/post/post.dto.ts | 4 ++++ backend/src/modules/post/post.service.ts | 23 +++++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/backend/src/modules/post/post.controller.ts b/backend/src/modules/post/post.controller.ts index 6ed54d7..12953f9 100644 --- a/backend/src/modules/post/post.controller.ts +++ b/backend/src/modules/post/post.controller.ts @@ -1,7 +1,7 @@ import { Body, Controller, Get, Param, Post } from '@nestjs/common'; import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'; import { EGetAll } from 'libs/enums/getAll.enum'; -import { ICreatePost } from './post.dto'; +import { ICreatePost, IEditPost } from './post.dto'; import { PostService } from './post.service'; @ApiTags('Post') @@ -22,7 +22,7 @@ export class PostController { return await this.postService.getAllPosts(status || EGetAll.all); } - @ApiOperation({ description: 'Getting a post bu uuid' }) + @ApiOperation({ description: 'Getting a post by uuid' }) @Get('get/:postId') async getPost(@Param('postId') postId: string) { return await this.postService.getPost(postId); @@ -33,4 +33,10 @@ export class PostController { async getByMediaGroup(@Param('mediaGroupId') mediaGroupId: string) { return await this.postService.getByMediaGroup(mediaGroupId); } + + @ApiOperation({ description: 'Editing a post by its uuid' }) + @Post('edit/:postId') + async editPost(@Param('postId') postId: string, @Body() data: IEditPost) { + return await this.postService.editPost(postId, data); + } } diff --git a/backend/src/modules/post/post.dto.ts b/backend/src/modules/post/post.dto.ts index 9a794d0..9c94994 100644 --- a/backend/src/modules/post/post.dto.ts +++ b/backend/src/modules/post/post.dto.ts @@ -7,3 +7,7 @@ export class ICreatePost { @ApiProperty({ description: 'Post media group id', example: '123' }) readonly media_group_id?: string; } + +export class IEditPost { + @ApiProperty({ description: 'Post text', example: 'Post text' }) readonly text!: string; +} diff --git a/backend/src/modules/post/post.service.ts b/backend/src/modules/post/post.service.ts index 394b036..34045e1 100644 --- a/backend/src/modules/post/post.service.ts +++ b/backend/src/modules/post/post.service.ts @@ -4,7 +4,7 @@ import { Admin } from 'libs/database/admin.entity'; import { Post } from 'libs/database/post.entity'; import { EGetAll } from 'libs/enums/getAll.enum'; import { Repository } from 'typeorm'; -import { ICreatePost } from './post.dto'; +import { ICreatePost, IEditPost } from './post.dto'; @Injectable() export class PostService { @@ -25,13 +25,32 @@ export class PostService { timestamp: new Date(), }); this.logger.log(`Created new post: ${result.uuid}`); - return { status: 'ok' }; + return result; } catch (error) { this.logger.debug(`[post.newPost] error: ${JSON.stringify(error)}`); throw new HttpException('No user with this id', HttpStatus.BAD_REQUEST); } } + async editPost(postId: string, data: IEditPost) { + try { + this.logger.log(`[post.editPost] data: ${JSON.stringify(data)}`); + const post = await this.postRepository.findOne({ where: { uuid: postId } }); + if (!post) { + throw new HttpException('Post not found', HttpStatus.NOT_FOUND); + } + if (post.text !== data.text) { + post.text = data.text; + post.timestamp = new Date(); + await this.postRepository.save(post); + } + return post; + } catch (error) { + this.logger.debug(`[post.editPost] error: ${JSON.stringify(error)}`); + throw new HttpException('Post not found', HttpStatus.NOT_FOUND); + } + } + async getAllPosts(status: EGetAll) { try { let obj: object;