Skip to content

Commit 4a2d271

Browse files
committed
/getContribution/:id
1 parent 2aed7e7 commit 4a2d271

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

api/src/contribution/controller.ts

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

44
import { ContributionRepository } from "./repository";
5-
import { GetContributionsResponse } from "./types";
5+
import { GetContributionResponse, GetContributionsResponse } from "./types";
66

77
@Service()
88
@Controller("/Contributions")
@@ -17,4 +17,13 @@ export class ContributionController {
1717
contributions,
1818
};
1919
}
20+
21+
@Get("/:id")
22+
public async getContribution(@Param("id") id: string): Promise<GetContributionResponse> {
23+
const contribution = await this.contributionRepository.findByIdWithStats(id);
24+
25+
return {
26+
contribution,
27+
};
28+
}
2029
}

api/src/contribution/repository.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,75 @@ export class ContributionRepository {
148148

149149
return sortedUpdatedAt;
150150
}
151+
152+
public async findByIdWithStats(id: string) {
153+
const statement = sql`
154+
SELECT
155+
p.id as id,
156+
p.name as name,
157+
json_agg(
158+
json_build_object('id', r.id, 'name', r.name, 'owner', r.owner, 'contributions', r.contributions)
159+
) AS repositories
160+
FROM
161+
(SELECT
162+
r.id as id,
163+
r.owner as owner,
164+
r.name as name,
165+
r.project_id as project_id,
166+
json_agg(
167+
json_build_object(
168+
'id',
169+
c.id,
170+
'title',
171+
c.title,
172+
'type',
173+
c.type,
174+
'url',
175+
c.url,
176+
'updated_at',
177+
c.updated_at,
178+
'activity_count',
179+
c.activity_count,
180+
'contributor',
181+
json_build_object(
182+
'id',
183+
cr.id,
184+
'name',
185+
cr.name,
186+
'username',
187+
cr.username,
188+
'avatar_url',
189+
cr.avatar_url
190+
)
191+
)
192+
) AS contributions
193+
FROM
194+
${contributionsTable} c
195+
INNER JOIN
196+
${repositoriesTable} r ON c.repository_id = r.id
197+
INNER JOIN
198+
${contributorsTable} cr ON c.contributor_id = cr.id
199+
WHERE
200+
c.id = ${id}
201+
GROUP BY
202+
r.id) AS r
203+
INNER JOIN
204+
${projectsTable} p ON r.project_id = p.id
205+
GROUP BY
206+
p.id
207+
`;
208+
209+
const raw = await this.postgresService.db.execute(statement);
210+
const entries = Array.from(raw);
211+
const unStringifiedRaw = unStringifyDeep(entries);
212+
213+
const reversed = reverseHierarchy(unStringifiedRaw, [
214+
{ from: "repositories", setParentAs: "project" },
215+
{ from: "contributions", setParentAs: "repository" },
216+
]);
217+
218+
const camelCased = camelCaseObject(reversed);
219+
220+
return camelCased[0];
221+
}
151222
}

api/src/contribution/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ export interface GetContributionsResponse extends GeneralResponse {
1414
}
1515
>;
1616
}
17+
18+
export interface GetContributionResponse extends GeneralResponse {
19+
contribution: Pick<
20+
ContributionEntity,
21+
"id" | "title" | "type" | "url" | "updatedAt" | "activityCount"
22+
> & {
23+
repository: Pick<RepositoryEntity, "id" | "owner" | "name"> & {
24+
project: Pick<ProjectEntity, "id" | "name">;
25+
};
26+
contributor: Pick<ContributorEntity, "id" | "name" | "username" | "avatarUrl">;
27+
};
28+
}

0 commit comments

Comments
 (0)