mirror of
https://github.com/MrSedan/neuro-reply-website.git
synced 2026-01-14 20:49:42 +03:00
Post delete
This commit is contained in:
@@ -37,4 +37,7 @@ export class Post {
|
|||||||
|
|
||||||
@Column({ nullable: true })
|
@Column({ nullable: true })
|
||||||
public message_entities?: string;
|
public message_entities?: string;
|
||||||
|
|
||||||
|
@Column({ default: false })
|
||||||
|
public deleted: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,4 +51,10 @@ export class PostController {
|
|||||||
async post() {
|
async post() {
|
||||||
return await this.postService.post();
|
return await this.postService.post();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation({ description: 'Delete post by order' })
|
||||||
|
@Delete('delete-post-by-order/:order')
|
||||||
|
async deletePostByOrder(@Param('order') order: number) {
|
||||||
|
return await this.postService.deletePostByOrder(order);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export class PostService {
|
|||||||
async editPostByOrderNum(order: string, data: IEditPost) {
|
async editPostByOrderNum(order: string, data: IEditPost) {
|
||||||
try {
|
try {
|
||||||
this.logger.log(`[post.editPostByOrderNum] data: ${JSON.stringify(data)}`);
|
this.logger.log(`[post.editPostByOrderNum] data: ${JSON.stringify(data)}`);
|
||||||
const posts = await this.postRepository.find({ where: { posted: false }, order: { timestamp: 'ASC' } });
|
const posts = await this.postRepository.find({ where: { posted: false, deleted: false }, order: { timestamp: 'ASC' } });
|
||||||
if (Math.abs(+order) > posts.length) {
|
if (Math.abs(+order) > posts.length) {
|
||||||
throw new HttpException('There are only ' + posts.length + ' unsent messages.', HttpStatus.BAD_REQUEST);
|
throw new HttpException('There are only ' + posts.length + ' unsent messages.', HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
@@ -86,13 +86,13 @@ export class PostService {
|
|||||||
let obj: object;
|
let obj: object;
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case EGetAll.will_post:
|
case EGetAll.will_post:
|
||||||
obj = { where: { posted: false }, order: { timestamp: 'ASC' } };
|
obj = { where: { posted: false, deleted: false }, order: { timestamp: 'ASC' } };
|
||||||
break;
|
break;
|
||||||
case EGetAll.all:
|
case EGetAll.all:
|
||||||
obj = { order: { timestamp: 'ASC' } };
|
obj = { order: { timestamp: 'ASC' }, where: { deleted: false } };
|
||||||
break;
|
break;
|
||||||
case EGetAll.posted:
|
case EGetAll.posted:
|
||||||
obj = { where: { posted: true }, order: { timestamp: 'ASC' } };
|
obj = { where: { posted: true, deleted: false }, order: { timestamp: 'ASC' } };
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return await this.postRepository.find(obj);
|
return await this.postRepository.find(obj);
|
||||||
@@ -131,7 +131,11 @@ export class PostService {
|
|||||||
|
|
||||||
async post() {
|
async post() {
|
||||||
try {
|
try {
|
||||||
const posts = await this.postRepository.find({ order: { timestamp: 'ASC' }, where: { posted: false }, relations: { images: true } });
|
const posts = await this.postRepository.find({
|
||||||
|
order: { timestamp: 'ASC' },
|
||||||
|
where: { posted: false, deleted: false },
|
||||||
|
relations: { images: true },
|
||||||
|
});
|
||||||
if (!posts.length) throw new HttpException('Nothing to post', HttpStatus.NOT_FOUND);
|
if (!posts.length) throw new HttpException('Nothing to post', HttpStatus.NOT_FOUND);
|
||||||
const post = posts[0];
|
const post = posts[0];
|
||||||
post.posted = true;
|
post.posted = true;
|
||||||
@@ -147,4 +151,25 @@ export class PostService {
|
|||||||
throw new HttpException('Bad data', HttpStatus.BAD_REQUEST);
|
throw new HttpException('Bad data', HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deletePostByOrder(order: number) {
|
||||||
|
try {
|
||||||
|
const posts = await this.postRepository.find({ order: { timestamp: 'ASC' }, where: { posted: false, deleted: 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 = true;
|
||||||
|
this.logger.log(`[post.deletePostByOrder] Post ${post.uuid} is deleted`);
|
||||||
|
await this.postRepository.save(post);
|
||||||
|
return post;
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
this.logger.debug('[post.deletePostByOrder] Not found');
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
this.logger.debug(`[post.deletePostByOrder] error: ${JSON.stringify(error)}`);
|
||||||
|
throw new HttpException('Bad data', HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user