Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

added validation and documentation

Yevhenii Kfd10453695f7
diff --git a/src/marketplace/categories/categories.controller.ts b/src/marketplace/categories/categories.controller.ts
index 864cbc2f1..7281772e9 100644
--- a/src/marketplace/categories/categories.controller.ts
+++ b/src/marketplace/categories/categories.controller.ts
@@ -9,8 +9,14 @@ import {
Patch,
Post,
Query,
+ UseGuards,
+ UsePipes,
+ ValidationPipe,
} from '@nestjs/common'
-import { ApiTags } from '@nestjs/swagger'
+import { ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'
+import { Roles } from '../../common/decorators/roles.decorator'
+import { RolesGuard } from '../../common/guards/roles.guard'
+import { UserType } from '../../user/entities/user.entity'
import { CategoriesService } from './categories.service'
import { Category } from './category.entity'
import { CreateCategory } from './dtos/create-category.dto'
@@ -22,10 +28,25 @@ import { UpdateCategory } from './dtos/update-category.dto'
import { ISaveCategory } from './interfaces/save-category.interface'
@ApiTags('categories')
+@UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
@Controller('categories')
export class CategoriesController {
constructor(private readonly categoriesService: CategoriesService) {}
+ @ApiQuery({
+ description: 'Category offset',
+ example: '15',
+ name: 'offset',
+ required: false,
+ type: String,
+ })
+ @ApiQuery({
+ description: 'Category limit',
+ example: '75',
+ name: 'limit',
+ required: false,
+ type: String,
+ })
@Get()
async getCategories(@Query() queries: GetCategoriesQueries): Promise<{
categories: Category[]
@@ -39,6 +60,12 @@ export class CategoriesController {
return { categories, count }
}
+ @ApiParam({
+ name: 'categoryId',
+ description: 'Category ID',
+ example: '1',
+ type: String,
+ })
@Get(':categoryId')
async getCategory(@Param() params: GetCategoryParams): Promise<Category> {
const category = await this.categoriesService.findById(params.categoryId)
@@ -50,6 +77,8 @@ export class CategoriesController {
return category
}
+ @UseGuards(RolesGuard)
+ @Roles(UserType.ADMIN)
@Post()
async createCategory(
@Body() body: CreateCategory,
@@ -65,6 +94,14 @@ export class CategoriesController {
return await this.categoriesService.save(categoryInstance)
}
+ @ApiParam({
+ name: 'categoryId',
+ description: 'Category ID',
+ example: '1',
+ type: String,
+ })
+ @UseGuards(RolesGuard)
+ @Roles(UserType.ADMIN)
@Patch(':categoryId')
async updateCategory(
@Param() params: UpdateCategoryParams,
@@ -81,6 +118,14 @@ export class CategoriesController {
return body
}
+ @ApiParam({
+ name: 'categoryId',
+ description: 'Category ID',
+ example: '1',
+ type: String,
+ })
+ @UseGuards(RolesGuard)
+ @Roles(UserType.ADMIN)
@Delete(':categoryId')
async deleteCategory(@Param() params: DeleteCategoryParams): Promise<void> {
const category = await this.categoriesService.findById(params.categoryId)
diff --git a/src/marketplace/categories/categories.module.ts b/src/marketplace/categories/categories.module.ts
index fba92b928..cd0230dbb 100644
--- a/src/marketplace/categories/categories.module.ts
+++ b/src/marketplace/categories/categories.module.ts
@@ -1,11 +1,12 @@
import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
+import { UserModule } from '../../user/user.module'
import { CategoriesController } from './categories.controller'
import { CategoriesService } from './categories.service'
import { Category } from './category.entity'
@Module({
- imports: [TypeOrmModule.forFeature([Category])],
+ imports: [TypeOrmModule.forFeature([Category]), UserModule],
controllers: [CategoriesController],
providers: [CategoriesService],
})
diff --git a/src/marketplace/categories/dtos/create-category.dto.ts b/src/marketplace/categories/dtos/create-category.dto.ts
index 72f03f585..4850f67bd 100644
--- a/src/marketplace/categories/dtos/create-category.dto.ts
+++ b/src/marketplace/categories/dtos/create-category.dto.ts
@@ -1,9 +1,32 @@
import { ApiProperty } from '@nestjs/swagger'
+import { IsOptional, IsString, MaxLength, MinLength } from 'class-validator'
export class CreateCategory {
- @ApiProperty()
+ @ApiProperty({
+ description: 'Category title',
+ example: 'Marketing',
+ maxLength: 255,
+ minLength: 1,
+ type: String,
+ })
+ @IsString()
+ @MinLength(1)
+ @MaxLength(255)
readonly title!: string
- @ApiProperty()
+ @ApiProperty({
+ default: null,
+ description: 'Category description',
+ example: 'Extensions for your marketing needs.',
+ maxLength: 1024,
+ minLength: 1,
+ nullable: true,
+ required: false,
+ type: String,
+ })
+ @IsString()
+ @IsOptional()
+ @MinLength(1)
+ @MaxLength(1024)
readonly description?: string | null
}
diff --git a/src/marketplace/categories/dtos/delete-category-params.dto.ts b/src/marketplace/categories/dtos/delete-category-params.dto.ts
index a00f4578e..1e470fe4f 100644
--- a/src/marketplace/categories/dtos/delete-category-params.dto.ts
+++ b/src/marketplace/categories/dtos/delete-category-params.dto.ts
@@ -1,3 +1,6 @@
+import { IsNumberString } from 'class-validator'
+
export class DeleteCategoryParams {
+ @IsNumberString()
readonly categoryId!: number
}
diff --git a/src/marketplace/categories/dtos/get-categories-queries.dto.ts b/src/marketplace/categories/dtos/get-categories-queries.dto.ts
index 8f604366e..f42f1b10c 100644
--- a/src/marketplace/categories/dtos/get-categories-queries.dto.ts
+++ b/src/marketplace/categories/dtos/get-categories-queries.dto.ts
@@ -1,4 +1,11 @@
+import { IsNumberString, IsOptional } from 'class-validator'
+
export class GetCategoriesQueries {
+ @IsNumberString()
+ @IsOptional()
readonly offset?: number
+
+ @IsNumberString()
+ @IsOptional()
readonly limit?: number
}
diff --git a/src/marketplace/categories/dtos/get-category-params.dto.ts b/src/marketplace/categories/dtos/get-category-params.dto.ts
index 10134fd79..c68b39326 100644
--- a/src/marketplace/categories/dtos/get-category-params.dto.ts
+++ b/src/marketplace/categories/dtos/get-category-params.dto.ts
@@ -1,3 +1,6 @@
+import { IsNumberString } from 'class-validator'
+
export class GetCategoryParams {
+ @IsNumberString()
readonly categoryId!: number
}
diff --git a/src/marketplace/categories/dtos/update-category-params.dto.ts b/src/marketplace/categories/dtos/update-category-params.dto.ts
index f39dd5485..b699d4821 100644
--- a/src/marketplace/categories/dtos/update-category-params.dto.ts
+++ b/src/marketplace/categories/dtos/update-category-params.dto.ts
@@ -1,3 +1,6 @@
+import { IsNumberString } from 'class-validator'
+
export class UpdateCategoryParams {
+ @IsNumberString()
readonly categoryId!: number
}
diff --git a/src/marketplace/categories/dtos/update-category.dto.ts b/src/marketplace/categories/dtos/update-category.dto.ts
index 1496d577b..35c571872 100644
--- a/src/marketplace/categories/dtos/update-category.dto.ts
+++ b/src/marketplace/categories/dtos/update-category.dto.ts
@@ -1,9 +1,33 @@
import { ApiProperty } from '@nestjs/swagger'
+import { IsOptional, IsString, MaxLength, MinLength } from 'class-validator'
export class UpdateCategory {
- @ApiProperty()
+ @ApiProperty({
+ description: 'Category title',
+ example: 'Marketing',
+ maxLength: 255,
+ minLength: 1,
+ required: false,
+ type: String,
+ })
+ @IsString()
+ @IsOptional()
+ @MinLength(1)
+ @MaxLength(255)
readonly title?: string
- @ApiProperty()
+ @ApiProperty({
+ description: 'Category description',
+ example: 'Extensions for your marketing needs.',
+ maxLength: 1024,
+ minLength: 1,
+ nullable: true,
+ required: false,
+ type: String,
+ })
+ @IsString()
+ @IsOptional()
+ @MinLength(1)
+ @MaxLength(1024)
readonly description?: string | null
}