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!

new route to update the category

Yevhenii K03068f22aa29
diff --git a/src/marketplace/categories/categories.controller.ts b/src/marketplace/categories/categories.controller.ts
index 23649c83f..cab30a170 100644
--- a/src/marketplace/categories/categories.controller.ts
+++ b/src/marketplace/categories/categories.controller.ts
@@ -1,7 +1,19 @@
-import { Body, ConflictException, Controller, Post } from '@nestjs/common'
+import {
+ Body,
+ ConflictException,
+ Controller,
+ NotFoundException,
+ Param,
+ Patch,
+ Post,
+} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { CategoriesService } from './categories.service'
+import { Category } from './category.entity'
import { CreateCategory } from './dtos/create-category.dto'
+import { UpdateCategoryParams } from './dtos/update-category-params.dto'
+import { UpdateCategory } from './dtos/update-category.dto'
+import { ISaveCategory } from './interfaces/save-category.interface'
@ApiTags('categories')
@Controller('categories')
@@ -9,7 +21,9 @@ export class CategoriesController {
constructor(private readonly categoriesService: CategoriesService) {}
@Post()
- async createCategory(@Body() body: CreateCategory): Promise<any> {
+ async createCategory(
+ @Body() body: CreateCategory,
+ ): Promise<ISaveCategory & Category> {
const title = await this.categoriesService.findTitle(body.title)
if (title) {
@@ -20,4 +34,20 @@ export class CategoriesController {
return await this.categoriesService.save(categoryInstance)
}
+
+ @Patch(':categoryId')
+ async updateCategory(
+ @Param() params: UpdateCategoryParams,
+ @Body() body: UpdateCategory,
+ ): Promise<UpdateCategory> {
+ const category = await this.categoriesService.findById(params.categoryId)
+
+ if (!category) {
+ throw new NotFoundException('Category not found.')
+ }
+
+ await this.categoriesService.update(category.id, body)
+
+ return body
+ }
}
diff --git a/src/marketplace/categories/categories.service.ts b/src/marketplace/categories/categories.service.ts
index acd40ffad..14041e582 100644
--- a/src/marketplace/categories/categories.service.ts
+++ b/src/marketplace/categories/categories.service.ts
@@ -4,6 +4,7 @@ import { FindOneOptions, Repository } from 'typeorm'
import { Category } from './category.entity'
import { ICreateCategory } from './interfaces/create-category.interface'
import { ISaveCategory } from './interfaces/save-category.interface'
+import { IUpdateCategory } from './interfaces/update-category.interface'
@Injectable()
export class CategoriesService {
@@ -27,4 +28,12 @@ export class CategoriesService {
async save(category: ISaveCategory): Promise<ISaveCategory & Category> {
return await this.categoryRepository.save(category)
}
+
+ async findById(id: number): Promise<Category> {
+ return await this.findOne({ where: { id } })
+ }
+
+ async update(id: number, category: IUpdateCategory): Promise<void> {
+ await this.categoryRepository.update({ id }, category)
+ }
}
diff --git a/src/marketplace/categories/dtos/update-category-params.dto.ts b/src/marketplace/categories/dtos/update-category-params.dto.ts
new file mode 100644
index 000000000..f39dd5485
--- /dev/null
+++ b/src/marketplace/categories/dtos/update-category-params.dto.ts
@@ -0,0 +1,3 @@
+export class UpdateCategoryParams {
+ readonly categoryId!: number
+}
diff --git a/src/marketplace/categories/dtos/update-category.dto.ts b/src/marketplace/categories/dtos/update-category.dto.ts
new file mode 100644
index 000000000..1496d577b
--- /dev/null
+++ b/src/marketplace/categories/dtos/update-category.dto.ts
@@ -0,0 +1,9 @@
+import { ApiProperty } from '@nestjs/swagger'
+
+export class UpdateCategory {
+ @ApiProperty()
+ readonly title?: string
+
+ @ApiProperty()
+ readonly description?: string | null
+}
diff --git a/src/marketplace/categories/interfaces/update-category.interface.ts b/src/marketplace/categories/interfaces/update-category.interface.ts
new file mode 100644
index 000000000..efb362c70
--- /dev/null
+++ b/src/marketplace/categories/interfaces/update-category.interface.ts
@@ -0,0 +1,4 @@
+export interface IUpdateCategory {
+ title?: string
+ description?: string | null
+}