File

src/insight/insight-repo.service.ts

Index

Methods

Constructor

constructor(insightRepoRepository: Repository, repoService: RepoService)
Parameters :
Name Type Optional
insightRepoRepository Repository<DbInsightRepo> No
repoService RepoService No

Methods

Async addInsightRepo
addInsightRepo(insightId: number, repoInfo: RepoInfo)
Parameters :
Name Type Optional
insightId number No
repoInfo RepoInfo No
Returns : unknown
Async findReposById
findReposById(insightId: number)
Parameters :
Name Type Optional
insightId number No
Returns : unknown
Async removeInsightRepo
removeInsightRepo(id: number)
Parameters :
Name Type Optional
id number No
Returns : unknown
import { BadRequestException, Injectable } from "@nestjs/common";
import { Repository } from "typeorm";
import { InjectRepository } from "@nestjs/typeorm";

import { RepoInfo } from "../repo/dtos/repo-info.dto";
import { RepoService } from "../repo/repo.service";
import { DbInsightRepo } from "./entities/insight-repo.entity";

@Injectable()
export class InsightRepoService {
  constructor(
    @InjectRepository(DbInsightRepo, "ApiConnection")
    private insightRepoRepository: Repository<DbInsightRepo>,
    private repoService: RepoService
  ) {}

  async addInsightRepo(insightId: number, repoInfo: RepoInfo) {
    if (!repoInfo.id && !repoInfo.fullName) {
      throw new BadRequestException("either repo id or repo full name must be provided");
    }

    let org;
    let name;

    if (repoInfo.fullName) {
      const parts = repoInfo.fullName.split("/");

      if (parts.length !== 2) {
        throw new BadRequestException("given repo full name is not valid owner/name repo");
      }

      [org, name] = parts;
    }

    const repo = await this.repoService.tryFindRepoOrMakeStub({ repoId: repoInfo.id, repoOwner: org, repoName: name });

    return this.insightRepoRepository.save({
      insight_id: insightId,
      repo_id: repo.id,
      full_name: repo.full_name,
    });
  }

  async findReposById(insightId: number) {
    return this.insightRepoRepository.find({ where: { insight_id: insightId } });
  }

  async removeInsightRepo(id: number) {
    return this.insightRepoRepository.softDelete(id);
  }
}

results matching ""

    No results matching ""