Skip to content

Commit

Permalink
feat: add get repository by id and full name
Browse files Browse the repository at this point in the history
  • Loading branch information
0-vortex committed Aug 5, 2022
1 parent dc9a56f commit 4552c66
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
33 changes: 31 additions & 2 deletions src/repo/repo.controller.ts
@@ -1,7 +1,7 @@
import { Controller, Get, HttpCode, HttpStatus, Query } from "@nestjs/common";
import { Controller, Get, HttpCode, HttpStatus, NotFoundException, Param, Query } from "@nestjs/common";
import { RepoService } from "./repo.service";
import { Repo } from "./repo.entity";
import { ApiOkResponse, ApiTags } from "@nestjs/swagger";
import { ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiTags } from "@nestjs/swagger";
import { PageOptionsDto } from "../common/dtos/page-options.dto";
import { PageDto } from "../common/dtos/page.dto";
import { ApiPaginatedResponse } from "../common/decorators/api-paginated-response.decorator";
Expand All @@ -11,6 +11,35 @@ import { ApiPaginatedResponse } from "../common/decorators/api-paginated-respons
export class RepoController {
constructor(private readonly repoService: RepoService) {}

@Get("/:id")
@ApiOperation({
operationId: "findOneById",
summary: "Finds a repo by :id",
})
@HttpCode(HttpStatus.OK)
@ApiOkResponse({ type: Repo })
@ApiNotFoundResponse({ type: NotFoundException })
async findOneById(
@Param("id") id: number,
): Promise<Repo> {
return this.repoService.findOneById(id);
}

@Get("/:owner/:repo")
@ApiOperation({
operationId: "findOneByOwnerAndRepo",
summary: "Finds a repo by :owner and :repo",
})
@HttpCode(HttpStatus.OK)
@ApiOkResponse({ type: Repo })
@ApiNotFoundResponse({ type: NotFoundException })
async findOneByOwnerAndRepo(
@Param("owner") owner: string,
@Param("repo") repo: string,
): Promise<Repo> {
return this.repoService.findOneByOwnerAndRepo(owner, repo);
}

@Get("/list")
@HttpCode(HttpStatus.OK)
@ApiPaginatedResponse(Repo)
Expand Down
54 changes: 46 additions & 8 deletions src/repo/repo.service.ts
@@ -1,4 +1,4 @@
import { Injectable } from "@nestjs/common";
import { Injectable, NotFoundException } from "@nestjs/common";
import { Repository } from "typeorm";
import { InjectRepository } from "@nestjs/typeorm";

Expand All @@ -14,25 +14,63 @@ export class RepoService {
private repoRepository: Repository<Repo>,
) {}

async findAll(
pageOptionsDto: PageOptionsDto
): Promise<PageDto<Repo>> {
const builder = this.repoRepository.createQueryBuilder("repo")
baseQueryBuilder() {
return this.repoRepository.createQueryBuilder("repo")
// .select(['repo.id'])
.leftJoinAndSelect("repo.user", "user")
.leftJoinAndSelect("repo.contributions", "contributions")
.loadRelationCountAndMap("repo.votesCount", "repo.repoToUserVotes")
.loadRelationCountAndMap("repo.starsCount", "repo.repoToUserStars")
.loadRelationCountAndMap("repo.submissionsCount", "repo.repoToUserSubmissions")
.loadRelationCountAndMap("repo.stargazersCount", "repo.repoToUserStargazers")
.loadRelationCountAndMap("repo.stargazersCount", "repo.repoToUserStargazers");
}

async findOneById(id: number): Promise<Repo> {
const queryBuilder = this.baseQueryBuilder();

queryBuilder
.where("repo.id = :id", { id });

const item = await queryBuilder.getOne();

if (!item) {
throw new NotFoundException();
}

return item;
}

async findOneByOwnerAndRepo(owner: string, repo: string): Promise<Repo> {
const queryBuilder = this.baseQueryBuilder();

queryBuilder
.where("repo.full_name = :name", {
name: `${owner}/${repo}`
});

const item = await queryBuilder.getOne();

if (!item) {
throw new NotFoundException();
}

return item;
}

async findAll(
pageOptionsDto: PageOptionsDto
): Promise<PageDto<Repo>> {
const queryBuilder = this.baseQueryBuilder();

queryBuilder
.orderBy("repo.pushed_at", pageOptionsDto.order)
.skip(pageOptionsDto.skip)
.take(pageOptionsDto.take);

// console.log(builder.getSql());

const itemCount = await builder.getCount();
const entities = await builder.getMany();
const itemCount = await queryBuilder.getCount();
const entities = await queryBuilder.getMany();

const pageMetaDto = new PageMetaDto({ itemCount, pageOptionsDto });

Expand Down

0 comments on commit 4552c66

Please sign in to comment.