From ac778289d0e7f0bb219cd2fad609f1e70daefd6a Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Wed, 22 Nov 2023 17:36:05 +0300 Subject: [PATCH] Moved to use enum --- backend/libs/enums/getAll.enum.ts | 5 +++++ backend/src/modules/post/post.controller.ts | 7 ++++--- backend/src/modules/post/post.service.ts | 9 +++++---- 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 backend/libs/enums/getAll.enum.ts diff --git a/backend/libs/enums/getAll.enum.ts b/backend/libs/enums/getAll.enum.ts new file mode 100644 index 0000000..814872b --- /dev/null +++ b/backend/libs/enums/getAll.enum.ts @@ -0,0 +1,5 @@ +export enum EGetAll { + all = 'all', + will_post = 'will-post', + posted = 'posted', +} diff --git a/backend/src/modules/post/post.controller.ts b/backend/src/modules/post/post.controller.ts index 29b33f5..6ed54d7 100644 --- a/backend/src/modules/post/post.controller.ts +++ b/backend/src/modules/post/post.controller.ts @@ -1,5 +1,6 @@ import { Body, Controller, Get, Param, Post } from '@nestjs/common'; import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'; +import { EGetAll } from 'libs/enums/getAll.enum'; import { ICreatePost } from './post.dto'; import { PostService } from './post.service'; @@ -16,9 +17,9 @@ export class PostController { @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'); + @ApiParam({ name: 'status', required: false, enum: EGetAll }) + async getAllPosts(@Param('status') status?: EGetAll) { + return await this.postService.getAllPosts(status || EGetAll.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 e1e1924..394b036 100644 --- a/backend/src/modules/post/post.service.ts +++ b/backend/src/modules/post/post.service.ts @@ -2,6 +2,7 @@ import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Admin } from 'libs/database/admin.entity'; import { Post } from 'libs/database/post.entity'; +import { EGetAll } from 'libs/enums/getAll.enum'; import { Repository } from 'typeorm'; import { ICreatePost } from './post.dto'; @@ -31,17 +32,17 @@ export class PostService { } } - async getAllPosts(status: 'will-post' | 'all' | 'posted') { + async getAllPosts(status: EGetAll) { try { let obj: object; switch (status) { - case 'will-post': + case EGetAll.will_post: obj = { where: { posted: false } }; break; - case 'all': + case EGetAll.all: obj = {}; break; - case 'posted': + case EGetAll.posted: obj = { where: { posted: true } }; break; }