Moved to use enum

This commit is contained in:
2023-11-22 17:36:05 +03:00
parent a17a06cbac
commit ac778289d0
3 changed files with 14 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
export enum EGetAll {
all = 'all',
will_post = 'will-post',
posted = 'posted',
}

View File

@@ -1,5 +1,6 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common'; import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'; import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { EGetAll } from 'libs/enums/getAll.enum';
import { ICreatePost } from './post.dto'; import { ICreatePost } from './post.dto';
import { PostService } from './post.service'; import { PostService } from './post.service';
@@ -16,9 +17,9 @@ export class PostController {
@ApiOperation({ description: 'Getting all posts. By default - all' }) @ApiOperation({ description: 'Getting all posts. By default - all' })
@Get('get-all/:status') @Get('get-all/:status')
@ApiParam({ name: 'status', required: false, enum: ['will-post', 'all', 'posted'] }) @ApiParam({ name: 'status', required: false, enum: EGetAll })
async getAllPosts(@Param('status') status?: 'will-post' | 'all' | 'posted') { async getAllPosts(@Param('status') status?: EGetAll) {
return await this.postService.getAllPosts(status || 'all'); return await this.postService.getAllPosts(status || EGetAll.all);
} }
@ApiOperation({ description: 'Getting a post bu uuid' }) @ApiOperation({ description: 'Getting a post bu uuid' })

View File

@@ -2,6 +2,7 @@ import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Admin } from 'libs/database/admin.entity'; import { Admin } from 'libs/database/admin.entity';
import { Post } from 'libs/database/post.entity'; import { Post } from 'libs/database/post.entity';
import { EGetAll } from 'libs/enums/getAll.enum';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { ICreatePost } from './post.dto'; import { ICreatePost } from './post.dto';
@@ -31,17 +32,17 @@ export class PostService {
} }
} }
async getAllPosts(status: 'will-post' | 'all' | 'posted') { async getAllPosts(status: EGetAll) {
try { try {
let obj: object; let obj: object;
switch (status) { switch (status) {
case 'will-post': case EGetAll.will_post:
obj = { where: { posted: false } }; obj = { where: { posted: false } };
break; break;
case 'all': case EGetAll.all:
obj = {}; obj = {};
break; break;
case 'posted': case EGetAll.posted:
obj = { where: { posted: true } }; obj = { where: { posted: true } };
break; break;
} }