mirror of
https://github.com/MrSedan/neuro-reply-website.git
synced 2026-01-14 20:49:42 +03:00
Restore deleted post
This commit is contained in:
@@ -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 { EGetAll } from 'libs/enums/getAll.enum';
|
||||
import { ICreatePost, IEditPost } from './post.dto';
|
||||
@@ -69,4 +69,10 @@ export class PostController {
|
||||
async 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,4 +199,25 @@ export class PostService {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user