From 78dd2baf4f973514166a15f483860e261e75d8a7 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Mon, 20 Nov 2023 21:53:36 +0300 Subject: [PATCH] Some post module changes --- backend/src/modules/post/post.controller.ts | 11 +++++----- backend/src/modules/post/post.service.ts | 24 ++++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/backend/src/modules/post/post.controller.ts b/backend/src/modules/post/post.controller.ts index d3fd697..0bea36c 100644 --- a/backend/src/modules/post/post.controller.ts +++ b/backend/src/modules/post/post.controller.ts @@ -1,5 +1,5 @@ import { Body, Controller, Get, Param, Post } from '@nestjs/common'; -import { ApiOperation, ApiTags } from '@nestjs/swagger'; +import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'; import { ICreatePost } from './post.dto'; import { PostService } from './post.service'; @@ -14,10 +14,11 @@ export class PostController { return await this.postService.newPost(data); } - @ApiOperation({ description: 'Getting all posts' }) - @Get('get-all') - async getAllPosts() { - return await this.postService.getAllPosts(); + @ApiOperation({ description: 'Getting all posts. By default - all' }) + @Get('get-all/:status') + @ApiParam({ name: 'status', required: false, enum: ['will-post', 'all', 'posted'] }) + async getAllPosts(@Param('status') status?: 'will-post' | 'all' | 'posted') { + return await this.postService.getAllPosts(status || 'all'); } @ApiOperation({ description: 'Getting a post bu uuid' }) diff --git a/backend/src/modules/post/post.service.ts b/backend/src/modules/post/post.service.ts index c3a347b..4f4310a 100644 --- a/backend/src/modules/post/post.service.ts +++ b/backend/src/modules/post/post.service.ts @@ -24,24 +24,38 @@ export class PostService { timestamp: new Date(), }); this.logger.log(`Created new post: ${result.uuid}`); - return { result: 'ok' }; + return { status: 'ok' }; } catch (error) { - this.logger.log(`[post.newPost] error: ${JSON.stringify(error)}`); + this.logger.debug(`[post.newPost] error: ${JSON.stringify(error)}`); + throw new HttpException('No user with this id', HttpStatus.BAD_REQUEST); } } - async getAllPosts() { + async getAllPosts(status: 'will-post' | 'all' | 'posted') { try { - return await this.postRepository.find(); + let obj: object; + switch (status) { + case 'will-post': + obj = { where: { posted: false } }; + break; + case 'all': + obj = {}; + break; + case 'posted': + obj = { where: { posted: true } }; + break; + } + return await this.postRepository.find(obj); } catch (error) { this.logger.log(`[post.getAllPosts] error: ${JSON.stringify(error)}`); + return []; } } async getPost(postId: string) { try { this.logger.log(`[post.getPost] data: ${postId}`); - return await this.postRepository.findOne({ where: { uuid: postId } }); + return await this.postRepository.findOne({ where: { uuid: postId }, relations: { images: true } }); } catch (error) { this.logger.log(`[post.getPost] error: ${JSON.stringify(error)}`); throw new HttpException('No post with this id', HttpStatus.NOT_FOUND);