Skip to content

Commit

Permalink
feat: add repos when creating an insight page (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Nov 15, 2022
1 parent 1b66716 commit 1f021a4
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 11 deletions.
21 changes: 21 additions & 0 deletions 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[];
}
2 changes: 2 additions & 0 deletions src/insight/entities/insight-repo.entity.ts
Expand Up @@ -6,6 +6,7 @@ import {
CreateDateColumn,
ManyToOne,
JoinColumn,
PrimaryGeneratedColumn,
} from "typeorm";
import { ApiHideProperty } from "@nestjs/swagger";

Expand All @@ -20,6 +21,7 @@ export class DbInsightRepo extends BaseEntity {
example: 237133,
})
@PrimaryColumn()
@PrimaryGeneratedColumn()
public id!: number;

@ApiModelProperty({
Expand Down
2 changes: 2 additions & 0 deletions src/insight/entities/insight.entity.ts
Expand Up @@ -6,6 +6,7 @@ import {
CreateDateColumn,
UpdateDateColumn,
OneToMany,
PrimaryGeneratedColumn,
} from "typeorm";

import { ApiModelProperty, ApiModelPropertyOptional } from "@nestjs/swagger/dist/decorators/api-model-property.decorator";
Expand All @@ -20,6 +21,7 @@ export class DbInsight extends BaseEntity {
example: 237133,
})
@PrimaryColumn()
@PrimaryGeneratedColumn()
public id!: number;

@ApiModelProperty({
Expand Down
21 changes: 21 additions & 0 deletions 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<DbInsightRepo>,
) {}

async addInsightRepo (insightId: number, repoId: number) {
const createdInsightRepo = this.insightRepoRepository.create({ insight_id: insightId, repo_id: repoId });

const newInsightRepo = await createdInsightRepo.save();

return newInsightRepo;
}
}
5 changes: 3 additions & 2 deletions src/insight/insights.module.ts
Expand Up @@ -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],
Expand All @@ -15,7 +16,7 @@ import { InsightsService } from "./insights.service";
DbInsightRepo,
]),
],
providers: [InsightsService],
exports: [InsightsService],
providers: [InsightsService, InsightRepoService],
exports: [InsightsService, InsightRepoService],
})
export class InsightsModule {}
6 changes: 3 additions & 3 deletions src/insight/insights.service.ts
Expand Up @@ -36,10 +36,10 @@ export class InsightsService {
return item;
}

async addInsight (insight: Omit<DbInsight, "id" | "created_at" | "updated_at">) {
const newInsight = this.insightRepository.create({ ...insight });
async addInsight (insight: Partial<DbInsight>) {
const createdInsight = this.insightRepository.create({ ...insight });

await newInsight.save();
const newInsight = await createdInsight.save();

return newInsight;
}
Expand Down
30 changes: 24 additions & 6 deletions src/insight/user-insight.controller.ts
@@ -1,20 +1,23 @@
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")
@ApiTags("Insights service")
export class UserInsightsController {
constructor (
private readonly insightsService: InsightsService,
private readonly insightsRepoService: InsightRepoService,
) {}

@Get("/")
Expand Down Expand Up @@ -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<DbInsight> {
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;
}
}

0 comments on commit 1f021a4

Please sign in to comment.