Skip to content

Commit 5841d1f

Browse files
committed
updated endpoints to use lang param
1 parent 01fcc62 commit 5841d1f

File tree

28 files changed

+142
-134
lines changed

28 files changed

+142
-134
lines changed

api/src/_utils/language.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { LANGUAGE_CODES, LanguageCode } from "@dzcode.io/utils/dist/language";
2+
import { IsIn } from "class-validator";
3+
4+
export class LanguageQuery {
5+
@IsIn(LANGUAGE_CODES)
6+
lang!: LanguageCode;
7+
}

api/src/contribution/controller.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, NotFoundError, Param } from "routing-controllers";
1+
import { Controller, Get, NotFoundError, Param, QueryParams } from "routing-controllers";
22
import { Service } from "typedi";
33

44
import { ContributionRepository } from "./repository";
@@ -8,15 +8,18 @@ import {
88
GetContributionsResponse,
99
GetContributionsForSitemapResponse,
1010
} from "./types";
11+
import { LanguageQuery } from "src/_utils/language";
1112

1213
@Service()
1314
@Controller("/contributions")
1415
export class ContributionController {
1516
constructor(private readonly contributionRepository: ContributionRepository) {}
1617

1718
@Get("/")
18-
public async getContributions(): Promise<GetContributionsResponse> {
19-
const contributions = await this.contributionRepository.findForList();
19+
public async getContributions(
20+
@QueryParams() { lang }: LanguageQuery,
21+
): Promise<GetContributionsResponse> {
22+
const contributions = await this.contributionRepository.findForList(lang);
2023

2124
return {
2225
contributions,
@@ -33,8 +36,11 @@ export class ContributionController {
3336
}
3437

3538
@Get("/:id")
36-
public async getContribution(@Param("id") id: string): Promise<GetContributionResponse> {
37-
const contribution = await this.contributionRepository.findByIdWithStats(id);
39+
public async getContribution(
40+
@Param("id") id: string,
41+
@QueryParams() { lang }: LanguageQuery,
42+
): Promise<GetContributionResponse> {
43+
const contribution = await this.contributionRepository.findByIdWithStats(id, lang);
3844

3945
return {
4046
contribution,

api/src/contribution/repository.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { PostgresService } from "src/postgres/service";
99
import { Service } from "typedi";
1010

1111
import { ContributionRow, contributionsTable } from "./table";
12+
import { LanguageCode } from "@dzcode.io/utils/dist/language";
1213

1314
@Service()
1415
export class ContributionRepository {
@@ -113,7 +114,7 @@ export class ContributionRepository {
113114
.where(ne(contributionsTable.runId, runId));
114115
}
115116

116-
public async findForList() {
117+
public async findForList(lang: LanguageCode) {
117118
const statement = sql`
118119
SELECT
119120
p.id as id,
@@ -146,7 +147,7 @@ export class ContributionRepository {
146147
'id',
147148
cr.id,
148149
'name',
149-
cr.name,
150+
cr.name_${sql.raw(lang)},
150151
'username',
151152
cr.username,
152153
'avatar_url',
@@ -187,7 +188,7 @@ export class ContributionRepository {
187188
return sortedUpdatedAt;
188189
}
189190

190-
public async findByIdWithStats(id: string) {
191+
public async findByIdWithStats(id: string, lang: LanguageCode) {
191192
const statement = sql`
192193
SELECT
193194
p.id as id,
@@ -220,7 +221,7 @@ export class ContributionRepository {
220221
'id',
221222
cr.id,
222223
'name',
223-
cr.name,
224+
cr.name_${sql.raw(lang)},
224225
'username',
225226
cr.username,
226227
'avatar_url',

api/src/contributor/controller.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, NotFoundError, Param } from "routing-controllers";
1+
import { Controller, Get, NotFoundError, Param, QueryParams } from "routing-controllers";
22
import { Service } from "typedi";
33

44
import { ContributorRepository } from "./repository";
@@ -10,7 +10,7 @@ import {
1010
} from "./types";
1111
import { ProjectRepository } from "src/project/repository";
1212
import { ContributionRepository } from "src/contribution/repository";
13-
import { Language } from "@dzcode.io/utils/dist/language";
13+
import { LanguageQuery } from "src/_utils/language";
1414

1515
@Service()
1616
@Controller("/contributors")
@@ -22,9 +22,9 @@ export class ContributorController {
2222
) {}
2323

2424
@Get("/")
25-
public async getContributors(): Promise<GetContributorsResponse> {
26-
// todo: lang query param
27-
const lang: Language = "en";
25+
public async getContributors(
26+
@QueryParams() { lang }: LanguageQuery,
27+
): Promise<GetContributorsResponse> {
2828
const contributors = await this.contributorRepository.findForList(lang);
2929

3030
return {
@@ -42,10 +42,10 @@ export class ContributorController {
4242
}
4343

4444
@Get("/:id")
45-
public async getContributor(@Param("id") id: string): Promise<GetContributorResponse> {
46-
// todo: lang query param
47-
const lang: Language = "en";
48-
45+
public async getContributor(
46+
@Param("id") id: string,
47+
@QueryParams() { lang }: LanguageQuery,
48+
): Promise<GetContributorResponse> {
4949
const [contributor, projects, contributions] = await Promise.all([
5050
this.contributorRepository.findWithStats(id, lang),
5151
this.projectRepository.findForContributor(id),
@@ -64,10 +64,10 @@ export class ContributorController {
6464
}
6565

6666
@Get("/:id/name")
67-
public async getContributorName(@Param("id") id: string): Promise<GetContributorNameResponse> {
68-
// todo: lang query param
69-
const lang: Language = "en";
70-
67+
public async getContributorName(
68+
@Param("id") id: string,
69+
@QueryParams() { lang }: LanguageQuery,
70+
): Promise<GetContributorNameResponse> {
7171
const contributor = await this.contributorRepository.findName(id, lang);
7272

7373
if (!contributor) throw new NotFoundError("Contributor not found");

api/src/contributor/repository.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import {
1111
ContributorRow,
1212
contributorsTable,
1313
} from "./table";
14-
import { Language } from "@dzcode.io/utils/dist/language";
14+
import { LanguageCode } from "@dzcode.io/utils/dist/language";
1515

1616
@Service()
1717
export class ContributorRepository {
1818
constructor(private readonly postgresService: PostgresService) {}
1919

20-
public async findName(contributorId: string, lang: Language) {
20+
public async findName(contributorId: string, lang: LanguageCode) {
2121
const statement = sql`
2222
SELECT
2323
${contributorsTable.id},
24-
${contributorsTable[`name_${lang}`]}
24+
${contributorsTable[`name_${lang}`]} as name
2525
FROM
2626
${contributorsTable}
2727
WHERE
@@ -39,11 +39,11 @@ export class ContributorRepository {
3939
return camelCased;
4040
}
4141

42-
public async findForProject(projectId: string, lang: Language) {
42+
public async findForProject(projectId: string, lang: LanguageCode) {
4343
const statement = sql`
4444
SELECT
4545
${contributorsTable.id},
46-
${contributorsTable[`name_${lang}`]},
46+
${contributorsTable[`name_${lang}`]} as name,
4747
${contributorsTable.avatarUrl},
4848
sum(${contributorRepositoryRelationTable.score}) as ranking
4949
FROM
@@ -67,11 +67,11 @@ export class ContributorRepository {
6767
return camelCased;
6868
}
6969

70-
public async findForList(lang: Language) {
70+
public async findForList(lang: LanguageCode) {
7171
const statement = sql`
7272
SELECT
7373
${contributorsTable.id},
74-
${contributorsTable[`name_${lang}`]},
74+
${contributorsTable[`name_${lang}`]} as name,
7575
${contributorsTable.avatarUrl},
7676
sum(${contributorRepositoryRelationTable.score}) as total_contribution_score,
7777
count(DISTINCT ${contributorRepositoryRelationTable.repositoryId}) as total_repository_count,
@@ -152,11 +152,11 @@ export class ContributorRepository {
152152
.where(ne(contributorsTable.runId, runId));
153153
}
154154

155-
public async findWithStats(contributorId: string, lang: Language) {
155+
public async findWithStats(contributorId: string, lang: LanguageCode) {
156156
const statement = sql`
157157
SELECT
158158
${contributorsTable.id},
159-
${contributorsTable[`name_${lang}`]},
159+
${contributorsTable[`name_${lang}`]} as name,
160160
${contributorsTable.avatarUrl},
161161
${contributorsTable.username},
162162
${contributorsTable.url},

api/src/project/controller.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, NotFoundError, Param } from "routing-controllers";
1+
import { Controller, Get, NotFoundError, Param, QueryParams } from "routing-controllers";
22
import { Service } from "typedi";
33

44
import { ProjectRepository } from "./repository";
@@ -11,7 +11,7 @@ import {
1111
import { RepositoryRepository } from "src/repository/repository";
1212
import { ContributorRepository } from "src/contributor/repository";
1313
import { ContributionRepository } from "src/contribution/repository";
14-
import { Language } from "@dzcode.io/utils/dist/language";
14+
import { LanguageQuery } from "src/_utils/language";
1515

1616
@Service()
1717
@Controller("/projects")
@@ -42,10 +42,10 @@ export class ProjectController {
4242
}
4343

4444
@Get("/:id")
45-
public async getProject(@Param("id") id: string): Promise<GetProjectResponse> {
46-
// todo: lang query param
47-
const lang: Language = "en";
48-
45+
public async getProject(
46+
@Param("id") id: string,
47+
@QueryParams() { lang }: LanguageQuery,
48+
): Promise<GetProjectResponse> {
4949
const [project, repositories, contributors, contributions] = await Promise.all([
5050
this.projectRepository.findWithStats(id),
5151
this.repositoryRepository.findForProject(id),
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { BaseEntity } from "src/_base";
22
import { StripLanguage } from "@dzcode.io/utils/dist/ts";
3-
import { Language } from "@dzcode.io/utils/dist/language";
43

54
export type ContributorEntity = BaseEntity & {
65
name_ar: string;
@@ -10,4 +9,4 @@ export type ContributorEntity = BaseEntity & {
109
avatarUrl: string;
1110
};
1211

13-
export type ContributorNoLang = StripLanguage<Language, ContributorEntity>;
12+
export type ContributorNoLang = StripLanguage<ContributorEntity>;
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
export const allLanguages = [
1+
import { LanguageCode } from "@dzcode.io/utils/dist/language";
2+
3+
export interface Language {
4+
code: LanguageCode;
5+
shortLabel: string;
6+
label: string;
7+
direction: "ltr" | "rtl";
8+
baseUrl: string;
9+
}
10+
11+
// todo: renamed to LANGUAGES
12+
export const Languages: Language[] = [
213
{ code: "en", shortLabel: "EN", label: "English", direction: "ltr", baseUrl: "" },
314
{ code: "ar", shortLabel: "ع", label: "العربية", direction: "rtl", baseUrl: "/ar" },
4-
] as const;
15+
];
516

6-
export interface LanguageEntity {
7-
code: (typeof allLanguages)[number]["code"];
8-
shortLabel: (typeof allLanguages)[number]["shortLabel"];
9-
label: (typeof allLanguages)[number]["label"];
10-
direction: (typeof allLanguages)[number]["direction"];
11-
}
17+
export const DefaultLanguage: Language = Languages[0];

packages/utils/src/fetch/factory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FullstackConfig } from "../config";
2+
import { LanguageCode } from "../language";
23

34
interface Endpoint {
45
params?: Record<string, string>;
@@ -7,14 +8,15 @@ interface Endpoint {
78
}
89

910
export const fetchV2Factory =
10-
<Es>(fullstackConfig: FullstackConfig) =>
11+
<Es>(fullstackConfig: FullstackConfig, lang: LanguageCode) =>
1112
async <T extends Es, E extends keyof T, C extends T[E], D extends keyof C>(
1213
endpoint: E,
1314
config: Pick<C, Exclude<D, "response">>,
1415
): Promise<C[D & "response"]> => {
1516
const { body, params, query } = config as Endpoint;
1617

17-
const queryString = query ? "?" + query.map(([key, value]) => `${key}=${value}`).join("&") : "";
18+
const queryWithLang = [...(query || []), ["lang", lang]];
19+
const queryString = "?" + queryWithLang.map(([key, value]) => `${key}=${value}`).join("&");
1820

1921
const domain = (endpoint as string).slice(0, (endpoint as string).indexOf(":"));
2022
let url = (endpoint as string).slice(domain.length + 1);
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export const LANGUAGES = ["ar", "en"] as const;
2-
3-
export type Language = (typeof LANGUAGES)[number];
1+
export const LANGUAGE_CODES = ["ar", "en"] as const;
2+
export type LanguageCode = (typeof LANGUAGE_CODES)[number];

0 commit comments

Comments
 (0)