Restore deleted post

This commit is contained in:
2024-02-15 11:53:03 +03:00
parent 0a4732f731
commit 20d11cbd3c
2 changed files with 28 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common'; import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'; import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { EGetAll } from 'libs/enums/getAll.enum'; import { EGetAll } from 'libs/enums/getAll.enum';
import { ICreatePost, IEditPost } from './post.dto'; import { ICreatePost, IEditPost } from './post.dto';
@@ -69,4 +69,10 @@ export class PostController {
async getDeletedPosts() { async getDeletedPosts() {
return await this.postService.getDeletedPosts(); return await this.postService.getDeletedPosts();
} }
@ApiOperation({ description: 'Restore post by order' })
@Put('restore-post-by-order/:order')
async restorePostByOrder(@Param('order') order: string) {
return await this.postService.restorePostByOrder(order);
}
} }

View File

@@ -199,4 +199,25 @@ export class PostService {
throw new HttpException('Bad data', HttpStatus.BAD_REQUEST); throw new HttpException('Bad data', HttpStatus.BAD_REQUEST);
} }
} }
async restorePostByOrder(order: string) {
try {
const posts = await this.postRepository.find({ order: { timestamp: 'ASC' }, where: { deleted: true, posted: false } });
if (Math.abs(+order) > posts.length) {
throw new HttpException('There are only ' + posts.length + ' posts.', HttpStatus.BAD_REQUEST);
}
const post = posts[Math.abs(+order) - 1];
post.deleted = false;
this.logger.log(`[post.restorePost] Post ${post.uuid} is restored`);
await this.postRepository.save(post);
return post;
} catch (error) {
if (error instanceof HttpException) {
this.logger.debug('[post.restorePost] Not found');
throw error;
}
this.logger.debug(`[post.restorePost] error: ${JSON.stringify(error)}`);
throw new HttpException('Bad data', HttpStatus.BAD_REQUEST);
}
}
} }