Post editing by its order num

This commit is contained in:
2024-01-24 15:49:17 +03:00
parent 53817e0fdb
commit db67834cfd
3 changed files with 31 additions and 1 deletions

View File

@@ -40,6 +40,12 @@ export class PostController {
return await this.postService.editPost(postId, data);
}
@ApiOperation({ description: 'Editing post text by its order num' })
@Post('edit-post-by-order-num/:order')
async editPostByOrderNum(@Param('order') order: string, @Body() data: IEditPost) {
return await this.postService.editPostByOrderNum(order, data);
}
@ApiOperation({ description: 'Get post to post' })
@Get('post')
async post() {

View File

@@ -52,6 +52,30 @@ export class PostService {
}
}
async editPostByOrderNum(order: string, data: IEditPost) {
try {
this.logger.log(`[post.editPostByOrderNum] data: ${JSON.stringify(data)}`);
const posts = await this.postRepository.find({ where: { posted: false }, order: { timestamp: 'ASC' } });
if (Math.abs(+order) > posts.length) {
throw new HttpException('There are only ' + posts.length + ' unsent messages.', HttpStatus.BAD_REQUEST);
}
const post = posts[Math.abs(+order) - 1];
if (post.text !== data.text) {
post.text = data.text;
post.edit_timestamp = new Date();
await this.postRepository.save(post);
}
return post;
} catch (error) {
if (error instanceof HttpException) {
this.logger.debug(`[post.editPostByOrderNum] Order: ${order} is bad`);
throw error;
}
this.logger.debug(`[post.editPostByOrderNum] Bad data. Order: ${order}. Data: ${JSON.stringify(data)}`);
throw new HttpException('Server error', HttpStatus.BAD_GATEWAY);
}
}
async getAllPosts(status: EGetAll) {
try {
let obj: object;

View File

@@ -5,7 +5,7 @@ services:
container_name: neuro_db_dev
image: postgres:alpine
environment:
- POSTGRES_USER=${DATABASE_USER}
- POSTGRES_USERNAME=${DATABASE_USERNAME}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
- POSTGRES_DB=${DATABASE_NAME}
- PGDATA=/var/lib/postgresql/data/pgdata