-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #557 from dzcode-io/553-move-projects-endpoint-fro…
…m-data-to-api
- Loading branch information
Showing
20 changed files
with
200 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { getCollection } from "@dzcode.io/data/dist/get/collection"; | ||
import { join } from "path"; | ||
import { ConfigService } from "src/config/service"; | ||
import { Service } from "typedi"; | ||
|
||
import { ProjectReferenceEntity } from "./types"; | ||
|
||
@Service() | ||
export class DataService { | ||
constructor(private readonly configService: ConfigService) {} | ||
|
||
public listProjects = async (): Promise<ProjectReferenceEntity[]> => { | ||
const projects = getCollection<ProjectReferenceEntity>( | ||
this.dataModelsPath, | ||
"projects-v2", | ||
"list.json", | ||
); | ||
|
||
if (projects === 404) throw new Error("Projects list not found"); | ||
|
||
return projects; | ||
}; | ||
|
||
private dataModelsPath = join(__dirname, "../../../data"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Model } from "@dzcode.io/models/dist/_base"; | ||
import { ProjectEntity } from "@dzcode.io/models/dist/project"; | ||
import { RepositoryEntity } from "@dzcode.io/models/dist/repository"; | ||
|
||
export interface ProjectReferenceEntity extends Model<ProjectEntity> { | ||
repositories: Model<RepositoryEntity>[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Controller, Get } from "routing-controllers"; | ||
import { OpenAPI, ResponseSchema } from "routing-controllers-openapi"; | ||
import { DataService } from "src/data/service"; | ||
import { GithubService } from "src/github/service"; | ||
import { Service } from "typedi"; | ||
|
||
import { GetProjectsResponseDto } from "./types"; | ||
|
||
@Service() | ||
@Controller("/Projects") | ||
export class ProjectController { | ||
constructor( | ||
private readonly githubService: GithubService, | ||
private readonly dataService: DataService, | ||
) {} | ||
|
||
@Get("/") | ||
@OpenAPI({ | ||
summary: "Return all projects listed in dzcode.io website", | ||
}) | ||
@ResponseSchema(GetProjectsResponseDto) | ||
public async getProjects(): Promise<GetProjectsResponseDto> { | ||
// get projects from /data folder: | ||
const projects = await this.dataService.listProjects(); | ||
|
||
// fetch info about these projects from github: | ||
const infoRichProjects: GetProjectsResponseDto["projects"] = await Promise.all( | ||
projects.map(async (project) => { | ||
const { repositories } = project; | ||
return { | ||
...project, | ||
repositories: await Promise.all( | ||
repositories.map(async ({ provider, owner, repository }) => { | ||
return { | ||
provider, | ||
owner, | ||
repository, | ||
stats: { | ||
contributionCount: ( | ||
await this.githubService.listRepositoryIssues({ owner, repository }) | ||
).length, | ||
languages: await this.githubService.listRepositoryLanguages({ | ||
owner, | ||
repository, | ||
}), | ||
}, | ||
contributors: ( | ||
await this.githubService.listRepositoryContributors({ | ||
owner, | ||
repository, | ||
}) | ||
).map((contributor) => ({ | ||
id: `github/${contributor.id}`, | ||
username: contributor.login, | ||
avatarUrl: contributor.avatar_url, | ||
})), | ||
}; | ||
}), | ||
), | ||
}; | ||
}), | ||
); | ||
|
||
return { | ||
projects: infoRichProjects, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { Model } from "@dzcode.io/models/dist/_base"; | ||
import { ProjectEntity } from "@dzcode.io/models/dist/project"; | ||
import { RepositoryEntity } from "@dzcode.io/models/dist/repository"; | ||
import { Type } from "class-transformer"; | ||
import { ValidateNested } from "class-validator"; | ||
import { GeneralResponseDto } from "src/app/types"; | ||
|
||
export class GetProjectsResponseDto extends GeneralResponseDto { | ||
@ValidateNested({ each: true }) | ||
@Type(() => ProjectEntity) | ||
projects!: Model<ProjectEntity, "repositories">[]; | ||
// @TODO-ZM: find a way to DRY this, eg: | ||
// projects!: Model<ProjectEntity, 'repositories' | 'repositories.contributors' | 'repositories.stats'>[] | ||
projects!: Array< | ||
Model<ProjectEntity> & { repositories: Model<RepositoryEntity, "contributors" | "stats">[] } | ||
>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.