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 route for creating extension comment

Yevhenii Kulisidif273b0352e24
diff --git a/src/marketplace/comments/comments.controller.ts b/src/marketplace/comments/comments.controller.ts
index 164c382c7..e7cad2944 100644
--- a/src/marketplace/comments/comments.controller.ts
+++ b/src/marketplace/comments/comments.controller.ts
@@ -1,22 +1,32 @@
import {
+ Body,
Controller,
Delete,
Get,
NotFoundException,
Param,
+ Post,
Query,
} from '@nestjs/common'
import { ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'
+import { UserService } from '../../user/user.service'
+import { ExtensionsService } from '../extensions/extensions.service'
import { CommentsService } from './comments.service'
+import { CreateCommentBodyDto } from './dtos/bodies/create-comment.dto'
import { DeleteCommentParamDto } from './dtos/params/delete-comment.dto'
import { GetCommentParamDto } from './dtos/params/get-comment.dto'
+import { CreateCommentQueryDto } from './dtos/queries/create-comment.dto'
import { GetCommentsQueryDto } from './dtos/queries/get-comments.dto'
import { Comment } from './entities/comment.entity'
@ApiTags('comments')
@Controller('comments')
export class CommentsController {
- constructor(private readonly commentsService: CommentsService) {}
+ constructor(
+ private readonly commentsService: CommentsService,
+ private readonly extensionsService: ExtensionsService,
+ private readonly userService: UserService,
+ ) {}
@Get()
@ApiQuery({ name: 'offset', required: false, type: String })
@@ -53,6 +63,41 @@ export class CommentsController {
return comment
}
+ @Post()
+ @ApiQuery({ name: 'userId', required: true, type: String })
+ async createComment(
+ @Query() queries: CreateCommentQueryDto,
+ @Body() body: CreateCommentBodyDto,
+ ): Promise<Comment> {
+ const extension = await this.extensionsService.findOne({
+ where: { id: body.extensionId },
+ })
+
+ if (!extension) {
+ throw new NotFoundException('Extension not found.')
+ }
+
+ const user = await this.userService.findOne(queries.userId)
+
+ if (!user) {
+ throw new NotFoundException('User not found.')
+ }
+
+ const comment = await this.commentsService.findOne({
+ where: { extensionId: body.extensionId, userId: queries.userId },
+ })
+
+ if (comment) {
+ throw new NotFoundException('Comment already exists.')
+ }
+
+ return await this.commentsService.save({
+ ...body,
+ extensionId: Number(body.extensionId),
+ userId: Number(queries.userId),
+ })
+ }
+
@Delete(':commentId')
@ApiParam({ name: 'commentId', required: true, type: String })
async deleteComment(@Param() params: DeleteCommentParamDto): Promise<void> {
diff --git a/src/marketplace/comments/comments.module.ts b/src/marketplace/comments/comments.module.ts
index b35c970da..21ac9eb91 100644
--- a/src/marketplace/comments/comments.module.ts
+++ b/src/marketplace/comments/comments.module.ts
@@ -1,11 +1,13 @@
import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
+import { UserModule } from '../../user/user.module'
+import { ExtensionsModule } from '../extensions/extensions.module'
import { CommentsController } from './comments.controller'
import { CommentsService } from './comments.service'
import { Comment } from './entities/comment.entity'
@Module({
- imports: [TypeOrmModule.forFeature([Comment])],
+ imports: [TypeOrmModule.forFeature([Comment]), ExtensionsModule, UserModule],
controllers: [CommentsController],
providers: [CommentsService],
})
diff --git a/src/marketplace/comments/comments.service.ts b/src/marketplace/comments/comments.service.ts
index 0e8715389..18a31b623 100644
--- a/src/marketplace/comments/comments.service.ts
+++ b/src/marketplace/comments/comments.service.ts
@@ -19,6 +19,12 @@ export class CommentsService {
return await this.commentsRepository.findOne({ ...options })
}
+ async save(
+ comment: Omit<Comment, 'id' | 'addedAt' | 'extension' | 'user'>,
+ ): Promise<Comment> {
+ return await this.commentsRepository.save(comment)
+ }
+
async delete(id: string): Promise<void> {
await this.commentsRepository.delete(id)
}
diff --git a/src/marketplace/comments/dtos/bodies/create-comment.dto.ts b/src/marketplace/comments/dtos/bodies/create-comment.dto.ts
new file mode 100644
index 000000000..90754d382
--- /dev/null
+++ b/src/marketplace/comments/dtos/bodies/create-comment.dto.ts
@@ -0,0 +1,20 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsNumber, IsOptional, IsString, Max, Min } from 'class-validator'
+
+export class CreateCommentBodyDto {
+ @ApiProperty()
+ @IsNumber()
+ extensionId: number
+
+ @ApiProperty()
+ @IsString()
+ @IsOptional()
+ text: string | null
+
+ @ApiProperty()
+ @IsNumber()
+ @Min(0)
+ @Max(5)
+ @IsOptional()
+ rating: number | null
+}
diff --git a/src/marketplace/comments/dtos/queries/create-comment.dto.ts b/src/marketplace/comments/dtos/queries/create-comment.dto.ts
new file mode 100644
index 000000000..74f460f78
--- /dev/null
+++ b/src/marketplace/comments/dtos/queries/create-comment.dto.ts
@@ -0,0 +1,8 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsNumberString } from 'class-validator'
+
+export class CreateCommentQueryDto {
+ @ApiProperty()
+ @IsNumberString()
+ userId: string
+}
diff --git a/src/marketplace/comments/entities/comment.entity.ts b/src/marketplace/comments/entities/comment.entity.ts
index b6f93818a..5a5c12b13 100644
--- a/src/marketplace/comments/entities/comment.entity.ts
+++ b/src/marketplace/comments/entities/comment.entity.ts
@@ -21,10 +21,10 @@ export class Comment {
userId: number
@Column('text', { nullable: true, default: null })
- text: string
+ text: string | null
@Column('int', { nullable: true, default: null })
- rating: number
+ rating: number | null
@CreateDateColumn()
addedAt: Date
diff --git a/src/marketplace/extensions/extensions.module.ts b/src/marketplace/extensions/extensions.module.ts
index 44cdd96c8..d911e4248 100644
--- a/src/marketplace/extensions/extensions.module.ts
+++ b/src/marketplace/extensions/extensions.module.ts
@@ -20,5 +20,6 @@ import { ProjectModule } from '../../project/project.module'
],
controllers: [ExtensionsController],
providers: [ExtensionsService],
+ exports: [ExtensionsService],
})
export class ExtensionsModule {}