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!

Controller refactoring performed

Yevhenii Kulisidi26eca1f3cbf5
diff --git a/src/marketplace/categories/categories.controller.ts b/src/marketplace/categories/categories.controller.ts
index bf12ff1a3..4f1cbe609 100644
--- a/src/marketplace/categories/categories.controller.ts
+++ b/src/marketplace/categories/categories.controller.ts
@@ -1,5 +1,6 @@
import {
Body,
+ ConflictException,
Controller,
Delete,
Get,
@@ -73,7 +74,6 @@ export class CategoriesController {
example: 1,
type: Number,
})
- // TODO: add pagination for extensions
@Get(':categoryId')
async getCategory(@Param() params: GetCategoryParams): Promise<Category> {
const category = await this.categoriesService.findOne({
@@ -96,7 +96,14 @@ export class CategoriesController {
async createCategory(
@Body() body: CreateCategory,
): Promise<ISaveCategory & Category> {
+ const categoryName = await this.categoriesService.findByName(body.name)
+
+ if (categoryName) {
+ throw new ConflictException('A category with that name already exists.')
+ }
+
const categoryInstance = this.categoriesService.create(body)
+
return await this.categoriesService.save(categoryInstance)
}
@@ -119,6 +126,14 @@ export class CategoriesController {
throw new NotFoundException('Category not found.')
}
+ if (body.name) {
+ const categoryName = await this.categoriesService.findByName(body.name)
+
+ if (categoryName) {
+ throw new ConflictException('A category with that name already exists.')
+ }
+ }
+
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 99a885ade..cdcd6d90c 100644
--- a/src/marketplace/categories/categories.service.ts
+++ b/src/marketplace/categories/categories.service.ts
@@ -42,4 +42,8 @@ export class CategoriesService {
): Promise<[Category[], number]> {
return await this.categoryRepository.findAndCount({ ...options })
}
+
+ async findByName(name: string): Promise<Category> {
+ return await this.findOne({ where: { name }, select: ['name'] })
+ }
}