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 create a category

Yevhenii Kc55b00cebca1
diff --git a/src/app.module.ts b/src/app.module.ts
index e37c8a987..0b09c6ff4 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -20,6 +20,7 @@ import { WebhookModule } from './webhook/webhook.module'
import { PingModule } from './ping/ping.module'
import { TGModule } from './tg-integration/tg.module'
import { MarketplaceModule } from './marketplace/marketplace.module'
+import { Category } from './marketplace/categories/category.entity'
const modules = [
ConfigModule.forRoot({ envFilePath: '.env' }),
@@ -31,7 +32,7 @@ const modules = [
password: process.env.MYSQL_ROOT_PASSWORD,
database: process.env.MYSQL_DATABASE,
synchronize: process.env.NODE_ENV === 'development',
- entities: [User, ActionToken, Project, ProjectShare],
+ entities: [User, ActionToken, Project, ProjectShare, Category],
}),
ScheduleModule.forRoot(),
TaskManagerModule,
diff --git a/src/marketplace/categories/categories.controller.ts b/src/marketplace/categories/categories.controller.ts
index 36cd2228d..23649c83f 100644
--- a/src/marketplace/categories/categories.controller.ts
+++ b/src/marketplace/categories/categories.controller.ts
@@ -1,6 +1,23 @@
-import { Controller } from '@nestjs/common'
+import { Body, ConflictException, Controller, Post } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
+import { CategoriesService } from './categories.service'
+import { CreateCategory } from './dtos/create-category.dto'
@ApiTags('categories')
@Controller('categories')
-export class CategoriesController {}
+export class CategoriesController {
+ constructor(private readonly categoriesService: CategoriesService) {}
+
+ @Post()
+ async createCategory(@Body() body: CreateCategory): Promise<any> {
+ const title = await this.categoriesService.findTitle(body.title)
+
+ if (title) {
+ throw new ConflictException('The category already exists.')
+ }
+
+ const categoryInstance = this.categoriesService.create(body)
+
+ return await this.categoriesService.save(categoryInstance)
+ }
+}
diff --git a/src/marketplace/categories/categories.module.ts b/src/marketplace/categories/categories.module.ts
index edce9f7d1..fba92b928 100644
--- a/src/marketplace/categories/categories.module.ts
+++ b/src/marketplace/categories/categories.module.ts
@@ -1,8 +1,11 @@
import { Module } from '@nestjs/common'
+import { TypeOrmModule } from '@nestjs/typeorm'
import { CategoriesController } from './categories.controller'
import { CategoriesService } from './categories.service'
+import { Category } from './category.entity'
@Module({
+ imports: [TypeOrmModule.forFeature([Category])],
controllers: [CategoriesController],
providers: [CategoriesService],
})
diff --git a/src/marketplace/categories/categories.service.ts b/src/marketplace/categories/categories.service.ts
index 2eff05f27..acd40ffad 100644
--- a/src/marketplace/categories/categories.service.ts
+++ b/src/marketplace/categories/categories.service.ts
@@ -1,4 +1,30 @@
import { Injectable } from '@nestjs/common'
+import { InjectRepository } from '@nestjs/typeorm'
+import { FindOneOptions, Repository } from 'typeorm'
+import { Category } from './category.entity'
+import { ICreateCategory } from './interfaces/create-category.interface'
+import { ISaveCategory } from './interfaces/save-category.interface'
@Injectable()
-export class CategoriesService {}
+export class CategoriesService {
+ constructor(
+ @InjectRepository(Category)
+ private readonly categoryRepository: Repository<Category>,
+ ) {}
+
+ async findOne(options: FindOneOptions<Category>): Promise<Category> {
+ return await this.categoryRepository.findOne({ ...options })
+ }
+
+ async findTitle(title: string): Promise<Category> {
+ return await this.findOne({ where: { title }, select: ['title'] })
+ }
+
+ create(category: ICreateCategory): Category {
+ return this.categoryRepository.create(category)
+ }
+
+ async save(category: ISaveCategory): Promise<ISaveCategory & Category> {
+ return await this.categoryRepository.save(category)
+ }
+}
diff --git a/src/marketplace/categories/category.entity.ts b/src/marketplace/categories/category.entity.ts
new file mode 100644
index 000000000..8de9a8c21
--- /dev/null
+++ b/src/marketplace/categories/category.entity.ts
@@ -0,0 +1,13 @@
+import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
+
+@Entity()
+export class Category {
+ @PrimaryGeneratedColumn('increment')
+ id!: number
+
+ @Column({ type: 'varchar', unique: true })
+ title!: string
+
+ @Column({ type: 'varchar', nullable: true, default: null })
+ description!: string | null
+}
diff --git a/src/marketplace/categories/dtos/create-category.dto.ts b/src/marketplace/categories/dtos/create-category.dto.ts
new file mode 100644
index 000000000..72f03f585
--- /dev/null
+++ b/src/marketplace/categories/dtos/create-category.dto.ts
@@ -0,0 +1,9 @@
+import { ApiProperty } from '@nestjs/swagger'
+
+export class CreateCategory {
+ @ApiProperty()
+ readonly title!: string
+
+ @ApiProperty()
+ readonly description?: string | null
+}
diff --git a/src/marketplace/categories/interfaces/create-category.interface.ts b/src/marketplace/categories/interfaces/create-category.interface.ts
new file mode 100644
index 000000000..9837f2d3d
--- /dev/null
+++ b/src/marketplace/categories/interfaces/create-category.interface.ts
@@ -0,0 +1,4 @@
+export interface ICreateCategory {
+ title: string
+ description?: string | null
+}
diff --git a/src/marketplace/categories/interfaces/save-category.interface.ts b/src/marketplace/categories/interfaces/save-category.interface.ts
new file mode 100644
index 000000000..e1d3c8f77
--- /dev/null
+++ b/src/marketplace/categories/interfaces/save-category.interface.ts
@@ -0,0 +1,4 @@
+export interface ISaveCategory {
+ title: string
+ description?: string | null
+}