-
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 #564 from dzcode-io/555-move-articles-endpoint-fro…
…m-data-to-api
- Loading branch information
Showing
17 changed files
with
200 additions
and
120 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,101 @@ | ||
import { Controller, Get, Param } from "routing-controllers"; | ||
import { OpenAPI, ResponseSchema } from "routing-controllers-openapi"; | ||
import { GithubUser } from "src/app/types/legacy"; | ||
import { DataService } from "src/data/service"; | ||
import { GithubService } from "src/github/service"; | ||
import { Service } from "typedi"; | ||
|
||
import { GetArticleResponseDto, GetArticlesResponseDto } from "./types"; | ||
|
||
@Service() | ||
@Controller("/Articles") | ||
export class ArticleController { | ||
constructor( | ||
private readonly githubService: GithubService, | ||
private readonly dataService: DataService, | ||
) {} | ||
|
||
@Get("/") | ||
@OpenAPI({ | ||
summary: "Return list of all articles", | ||
}) | ||
@ResponseSchema(GetArticlesResponseDto) | ||
public async getArticles(): Promise<GetArticlesResponseDto> { | ||
// get articles from /data folder: | ||
const articles = await this.dataService.listArticles(); | ||
|
||
return { | ||
articles, | ||
}; | ||
} | ||
|
||
@Get("/:slug") | ||
@OpenAPI({ | ||
summary: "Return info about a single article", | ||
}) | ||
@ResponseSchema(GetArticleResponseDto) | ||
public async getArticle(@Param("slug") slug: string): Promise<GetArticleResponseDto> { | ||
// get articles from /data folder: | ||
const { ...article } = await this.dataService.getArticle(slug); | ||
|
||
// get authors and contributors info from github: | ||
const authors = await Promise.all( | ||
article.authors.map(async (author) => { | ||
const githubUser = await this.githubService.getUser({ username: author }); | ||
return { | ||
id: `github/${githubUser.id}`, | ||
name: githubUser.login, | ||
link: githubUser.html_url, | ||
image: githubUser.avatar_url, | ||
}; | ||
}), | ||
); | ||
|
||
const contributorsBatches = await Promise.all([ | ||
// current place for data: | ||
this.githubService.listContributors({ | ||
owner: "dzcode-io", | ||
repository: "dzcode.io", | ||
path: `data/models/articles/${slug}`, | ||
}), | ||
// also check old place for data, to not lose contribution effort: | ||
this.githubService.listContributors({ | ||
owner: "dzcode-io", | ||
repository: "dzcode.io", | ||
path: `data/articles/${slug}`, | ||
}), | ||
]); | ||
|
||
// filter and sort contributors: | ||
const uniqUsernames: Record<string, number> = {}; | ||
const contributors: GetArticleResponseDto["article"]["contributors"] = [ | ||
...contributorsBatches[0], | ||
...contributorsBatches[1], | ||
] | ||
.reduce<GithubUser[]>((pV, cV) => { | ||
if (uniqUsernames[cV.login]) { | ||
uniqUsernames[cV.login]++; | ||
return pV; | ||
} else { | ||
uniqUsernames[cV.login] = 1; | ||
return [...pV, cV]; | ||
} | ||
}, []) | ||
.sort((a, b) => uniqUsernames[b.login] - uniqUsernames[a.login]) | ||
.map((contributor) => ({ | ||
id: `github/${contributor.id}`, | ||
name: contributor.login, | ||
link: contributor.html_url, | ||
image: contributor.avatar_url, | ||
})) | ||
.filter(({ id }) => !authors.find((author) => author.id === id)); | ||
|
||
return { | ||
article: { | ||
...article, | ||
authors, | ||
contributors, | ||
}, | ||
}; | ||
} | ||
} |
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,16 @@ | ||
import { Model } from "@dzcode.io/models/dist/_base"; | ||
import { ArticleEntity, ArticleInfoEntity } from "@dzcode.io/models/dist/article"; | ||
import { Type } from "class-transformer"; | ||
import { ValidateNested } from "class-validator"; | ||
import { GeneralResponseDto } from "src/app/types"; | ||
|
||
export class GetArticlesResponseDto extends GeneralResponseDto { | ||
@ValidateNested({ each: true }) | ||
@Type(() => ArticleInfoEntity) | ||
articles!: Model<ArticleInfoEntity>[]; | ||
} | ||
|
||
export class GetArticleResponseDto extends GeneralResponseDto { | ||
@ValidateNested() | ||
article!: Model<ArticleEntity, "authors" | "contributors">; | ||
} |
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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { Model } from "@dzcode.io/models/dist/_base"; | ||
import { ArticleInfoEntity } from "@dzcode.io/models/dist/article"; | ||
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>[]; | ||
} | ||
export type DataProjectEntity = Model<ProjectEntity, "repositories">; | ||
|
||
export type DataArticleEntity = Model<ArticleInfoEntity> & { | ||
authors: string[]; | ||
description: string; | ||
content: string; | ||
image: string; | ||
}; |
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 |
---|---|---|
|
@@ -3,5 +3,7 @@ | |
"compilerOptions": { | ||
"strict": true, | ||
"baseUrl": "." | ||
} | ||
}, | ||
"include": ["src"], | ||
"exclude": ["src/_e2e-test"] | ||
} |
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
Oops, something went wrong.