From 743e073935fc0c52575122e80fbfa4faeafbb970 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Mon, 27 Nov 2023 00:24:36 +0300 Subject: [PATCH] Get post to post method --- backend/src/modules/post/post.controller.ts | 6 ++++++ backend/src/modules/post/post.service.ts | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/backend/src/modules/post/post.controller.ts b/backend/src/modules/post/post.controller.ts index 12953f9..08e0677 100644 --- a/backend/src/modules/post/post.controller.ts +++ b/backend/src/modules/post/post.controller.ts @@ -39,4 +39,10 @@ export class PostController { async editPost(@Param('postId') postId: string, @Body() data: IEditPost) { return await this.postService.editPost(postId, data); } + + @ApiOperation({ description: 'Get post to post' }) + @Get('post') + async post() { + return await this.postService.post(); + } } diff --git a/backend/src/modules/post/post.service.ts b/backend/src/modules/post/post.service.ts index 4b714b4..9220f0b 100644 --- a/backend/src/modules/post/post.service.ts +++ b/backend/src/modules/post/post.service.ts @@ -95,4 +95,23 @@ export class PostService { throw new HttpException("Can't find post with this media group id", HttpStatus.BAD_REQUEST); } } + + async post() { + try { + const posts = await this.postRepository.find({ order: { timestamp: 'ASC' }, where: { posted: false }, relations: { images: true } }); + if (!posts) throw new HttpException('Nothing to post', HttpStatus.NOT_FOUND); + const post = posts[0]; + post.posted = true; + this.logger.log(`[post.post] Post ${post.uuid} is posted`); + await this.postRepository.save(post); + return post; + } catch (error) { + if (error instanceof HttpException) { + this.logger.debug('[post.post] Not found'); + throw error; + } + this.logger.debug(`[post.post] error: ${JSON.stringify(error)}`); + throw new HttpException('Bad data', HttpStatus.BAD_REQUEST); + } + } }