From 1f021a48cb3f962d3b9e387b017638a442672996 Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 15 Nov 2022 12:30:28 -0600 Subject: [PATCH] feat: add repos when creating an insight page (#63) --- src/insight/dtos/create-insight.dto.ts | 21 +++++++++++++++ src/insight/entities/insight-repo.entity.ts | 2 ++ src/insight/entities/insight.entity.ts | 2 ++ src/insight/insight-repo.service.ts | 21 +++++++++++++++ src/insight/insights.module.ts | 5 ++-- src/insight/insights.service.ts | 6 ++--- src/insight/user-insight.controller.ts | 30 ++++++++++++++++----- 7 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 src/insight/dtos/create-insight.dto.ts create mode 100644 src/insight/insight-repo.service.ts diff --git a/src/insight/dtos/create-insight.dto.ts b/src/insight/dtos/create-insight.dto.ts new file mode 100644 index 00000000..1504acec --- /dev/null +++ b/src/insight/dtos/create-insight.dto.ts @@ -0,0 +1,21 @@ +import { ApiProperty } from "@nestjs/swagger"; +import { IsArray, IsString } from "class-validator"; + +export class CreateInsightDto { + @ApiProperty({ + description: "Insight Page Name", + type: String, + example: "My Team", + }) + @IsString() + name: string; + + @ApiProperty({ + description: "An array of repository IDs", + type: [Number], + isArray: true, + example: [1, 2, 3], + }) + @IsArray() + ids: number[]; +} diff --git a/src/insight/entities/insight-repo.entity.ts b/src/insight/entities/insight-repo.entity.ts index d5837241..989393eb 100644 --- a/src/insight/entities/insight-repo.entity.ts +++ b/src/insight/entities/insight-repo.entity.ts @@ -6,6 +6,7 @@ import { CreateDateColumn, ManyToOne, JoinColumn, + PrimaryGeneratedColumn, } from "typeorm"; import { ApiHideProperty } from "@nestjs/swagger"; @@ -20,6 +21,7 @@ export class DbInsightRepo extends BaseEntity { example: 237133, }) @PrimaryColumn() + @PrimaryGeneratedColumn() public id!: number; @ApiModelProperty({ diff --git a/src/insight/entities/insight.entity.ts b/src/insight/entities/insight.entity.ts index 757dce5f..5e5b53e5 100644 --- a/src/insight/entities/insight.entity.ts +++ b/src/insight/entities/insight.entity.ts @@ -6,6 +6,7 @@ import { CreateDateColumn, UpdateDateColumn, OneToMany, + PrimaryGeneratedColumn, } from "typeorm"; import { ApiModelProperty, ApiModelPropertyOptional } from "@nestjs/swagger/dist/decorators/api-model-property.decorator"; @@ -20,6 +21,7 @@ export class DbInsight extends BaseEntity { example: 237133, }) @PrimaryColumn() + @PrimaryGeneratedColumn() public id!: number; @ApiModelProperty({ diff --git a/src/insight/insight-repo.service.ts b/src/insight/insight-repo.service.ts new file mode 100644 index 00000000..be0486f7 --- /dev/null +++ b/src/insight/insight-repo.service.ts @@ -0,0 +1,21 @@ +import { Injectable } from "@nestjs/common"; +import { Repository } from "typeorm"; +import { InjectRepository } from "@nestjs/typeorm"; + +import { DbInsightRepo } from "./entities/insight-repo.entity"; + +@Injectable() +export class InsightRepoService { + constructor ( + @InjectRepository(DbInsightRepo) + private insightRepoRepository: Repository, + ) {} + + async addInsightRepo (insightId: number, repoId: number) { + const createdInsightRepo = this.insightRepoRepository.create({ insight_id: insightId, repo_id: repoId }); + + const newInsightRepo = await createdInsightRepo.save(); + + return newInsightRepo; + } +} diff --git a/src/insight/insights.module.ts b/src/insight/insights.module.ts index f7c4773e..ecc8add0 100644 --- a/src/insight/insights.module.ts +++ b/src/insight/insights.module.ts @@ -6,6 +6,7 @@ import { DbInsightRepo } from "./entities/insight-repo.entity"; import { InsightController } from "./insight.controller"; import { UserInsightsController } from "./user-insight.controller"; import { InsightsService } from "./insights.service"; +import { InsightRepoService } from "./insight-repo.service"; @Module({ controllers: [InsightController, UserInsightsController], @@ -15,7 +16,7 @@ import { InsightsService } from "./insights.service"; DbInsightRepo, ]), ], - providers: [InsightsService], - exports: [InsightsService], + providers: [InsightsService, InsightRepoService], + exports: [InsightsService, InsightRepoService], }) export class InsightsModule {} diff --git a/src/insight/insights.service.ts b/src/insight/insights.service.ts index 2d582346..5eca16d0 100644 --- a/src/insight/insights.service.ts +++ b/src/insight/insights.service.ts @@ -36,10 +36,10 @@ export class InsightsService { return item; } - async addInsight (insight: Omit) { - const newInsight = this.insightRepository.create({ ...insight }); + async addInsight (insight: Partial) { + const createdInsight = this.insightRepository.create({ ...insight }); - await newInsight.save(); + const newInsight = await createdInsight.save(); return newInsight; } diff --git a/src/insight/user-insight.controller.ts b/src/insight/user-insight.controller.ts index 276f2d84..b2fb6f6e 100644 --- a/src/insight/user-insight.controller.ts +++ b/src/insight/user-insight.controller.ts @@ -1,13 +1,15 @@ -import { Body, Controller, Get, Post, Query, UseGuards } from "@nestjs/common"; -import { ApiOperation, ApiOkResponse, ApiNotFoundResponse, ApiBearerAuth, ApiTags } from "@nestjs/swagger"; +import { BadRequestException, Body, Controller, Get, Post, Query, UseGuards } from "@nestjs/common"; +import { ApiOperation, ApiOkResponse, ApiNotFoundResponse, ApiBearerAuth, ApiTags, ApiBadRequestResponse, ApiBody } from "@nestjs/swagger"; import { SupabaseGuard } from "../auth/supabase.guard"; import { UserId } from "../auth/supabase.user.decorator"; import { ApiPaginatedResponse } from "../common/decorators/api-paginated-response.decorator"; import { PageDto } from "../common/dtos/page.dto"; +import { CreateInsightDto } from "./dtos/create-insight.dto"; import { InsightPageOptionsDto } from "./dtos/insight-page-options.dto"; import { DbInsight } from "./entities/insight.entity"; +import { InsightRepoService } from "./insight-repo.service"; import { InsightsService } from "./insights.service"; @Controller("user/insights") @@ -15,6 +17,7 @@ import { InsightsService } from "./insights.service"; export class UserInsightsController { constructor ( private readonly insightsService: InsightsService, + private readonly insightsRepoService: InsightRepoService, ) {} @Get("/") @@ -43,12 +46,27 @@ export class UserInsightsController { @UseGuards(SupabaseGuard) @ApiOkResponse({ type: DbInsight }) @ApiNotFoundResponse({ description: "Unable to add user insight" }) + @ApiBadRequestResponse({ description: "Invalid request" }) + @ApiBody({ type: CreateInsightDto }) async addInsightForUser ( - @Body() body: string, - @UserId() userId: string, + @Body() createInsightDto: CreateInsightDto, + @UserId() userId: number, ): Promise { - const newInsight = (JSON.parse(body) || {}) as DbInsight; + if (!createInsightDto.name || !Array.isArray(createInsightDto.ids)) { + throw (new BadRequestException); + } + + const newInsight = await this.insightsService.addInsight({ + name: createInsightDto.name, + user_id: userId, + }); + + const repoIds = createInsightDto.ids; + + repoIds.forEach(async repoId => { + await this.insightsRepoService.addInsightRepo(newInsight.id, repoId); + }); - return this.insightsService.addInsight({ ...newInsight, user_id: parseInt(userId) } as DbInsight); + return newInsight; } }