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 a new route to add an extension and changed the structure of the entity

Yevhenii K96225bca1eb2
diff --git a/src/marketplace/extensions/dtos/add-extension.dto.ts b/src/marketplace/extensions/dtos/add-extension.dto.ts
new file mode 100644
index 000000000..6a4f66e18
--- /dev/null
+++ b/src/marketplace/extensions/dtos/add-extension.dto.ts
@@ -0,0 +1,50 @@
+import { ApiProperty } from '@nestjs/swagger'
+import {
+ IsOptional,
+ IsString,
+ Matches,
+ MaxLength,
+ MinLength,
+} from 'class-validator'
+
+export class AddExtension {
+ @ApiProperty({
+ description: 'Extension title',
+ example: '', // TODO: Add example
+ maxLength: 255,
+ minLength: 1,
+ type: String,
+ })
+ @IsString()
+ @MinLength(1)
+ @MaxLength(255)
+ readonly title!: string
+
+ @ApiProperty({
+ default: null,
+ description: 'Extension description',
+ example: '', // Add example
+ maxLength: 1024,
+ minLength: 1,
+ nullable: true,
+ required: false,
+ type: String,
+ })
+ @IsString()
+ @IsOptional()
+ @MinLength(1)
+ @MaxLength(1024)
+ readonly description?: string | null
+
+ @ApiProperty({
+ description: 'Extension version',
+ example: '1.0.0',
+ maxLength: 255,
+ minLength: 5,
+ type: String,
+ })
+ @IsString()
+ @Matches(/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/)
+ @MaxLength(255)
+ readonly version!: string
+}
diff --git a/src/marketplace/extensions/extension.entity.ts b/src/marketplace/extensions/extension.entity.ts
index b5ac63f99..702880d4c 100644
--- a/src/marketplace/extensions/extension.entity.ts
+++ b/src/marketplace/extensions/extension.entity.ts
@@ -6,13 +6,13 @@ export class Extension {
@PrimaryGeneratedColumn('increment')
id!: number
- @Column('varchar')
+ @Column({ type: 'varchar', unique: true })
title!: string
@Column({ type: 'text', nullable: true, default: null })
description!: string | null
- @Column({ type: 'uuid', unique: true })
+ @Column({ type: 'uuid', generated: 'uuid', unique: true })
url: string
@Column('varchar')
diff --git a/src/marketplace/extensions/extensions.controller.ts b/src/marketplace/extensions/extensions.controller.ts
index 71eb1e805..333cbc787 100644
--- a/src/marketplace/extensions/extensions.controller.ts
+++ b/src/marketplace/extensions/extensions.controller.ts
@@ -1,5 +1,16 @@
-import { Controller, UsePipes, ValidationPipe } from '@nestjs/common'
+import {
+ Body,
+ ConflictException,
+ Controller,
+ Post,
+ UsePipes,
+ ValidationPipe,
+} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
+import { AddExtension } from './dtos/add-extension.dto'
+import { Extension } from './extension.entity'
+import { ExtensionsService } from './extensions.service'
+import { ISaveExtension } from './interfaces/save-extension.interface'
@ApiTags('extensions')
@UsePipes(
@@ -11,4 +22,21 @@ import { ApiTags } from '@nestjs/swagger'
}),
)
@Controller('extensions')
-export class ExtensionsController {}
+export class ExtensionsController {
+ constructor(private readonly extensionsService: ExtensionsService) {}
+
+ @Post()
+ async addExtension(
+ @Body() body: AddExtension,
+ ): Promise<ISaveExtension & Extension> {
+ const title = await this.extensionsService.findTitle(body.title)
+
+ if (title) {
+ throw new ConflictException('The extension already exists.')
+ }
+
+ const extensionInstance = this.extensionsService.create(body)
+
+ return await this.extensionsService.save(extensionInstance)
+ }
+}
diff --git a/src/marketplace/extensions/extensions.service.ts b/src/marketplace/extensions/extensions.service.ts
index 03e2eaca9..3f0c25702 100644
--- a/src/marketplace/extensions/extensions.service.ts
+++ b/src/marketplace/extensions/extensions.service.ts
@@ -1,4 +1,30 @@
import { Injectable } from '@nestjs/common'
+import { InjectRepository } from '@nestjs/typeorm'
+import { FindOneOptions, Repository } from 'typeorm'
+import { Extension } from './extension.entity'
+import { ICreateExtension } from './interfaces/create-extension.interface'
+import { ISaveExtension } from './interfaces/save-extension.interface'
@Injectable()
-export class ExtensionsService {}
+export class ExtensionsService {
+ constructor(
+ @InjectRepository(Extension)
+ private readonly extensionRepository: Repository<Extension>,
+ ) {}
+
+ async findOne(options: FindOneOptions<Extension>): Promise<Extension> {
+ return await this.extensionRepository.findOne({ ...options })
+ }
+
+ async findTitle(title: string): Promise<Extension> {
+ return await this.findOne({ where: { title }, select: ['title'] })
+ }
+
+ create(extension: ICreateExtension): Extension {
+ return this.extensionRepository.create(extension)
+ }
+
+ async save(category: ISaveExtension): Promise<ISaveExtension & Extension> {
+ return await this.extensionRepository.save(category)
+ }
+}
diff --git a/src/marketplace/extensions/interfaces/create-extension.interface.ts b/src/marketplace/extensions/interfaces/create-extension.interface.ts
new file mode 100644
index 000000000..92355fc39
--- /dev/null
+++ b/src/marketplace/extensions/interfaces/create-extension.interface.ts
@@ -0,0 +1,5 @@
+export interface ICreateExtension {
+ title: string
+ description?: string | null
+ version: string
+}
diff --git a/src/marketplace/extensions/interfaces/save-extension.interface.ts b/src/marketplace/extensions/interfaces/save-extension.interface.ts
new file mode 100644
index 000000000..3bf1ad2b7
--- /dev/null
+++ b/src/marketplace/extensions/interfaces/save-extension.interface.ts
@@ -0,0 +1,5 @@
+export interface ISaveExtension {
+ title: string
+ description?: string | null
+ version: string
+}