Skip to content

Commit

Permalink
feat: projects a contributor helped in
Browse files Browse the repository at this point in the history
  • Loading branch information
ZibanPirate committed Oct 27, 2024
1 parent 696a2d8 commit 86f7dbd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
15 changes: 11 additions & 4 deletions api/src/contributor/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { Service } from "typedi";

import { ContributorRepository } from "./repository";
import { GetContributorResponse, GetContributorsResponse } from "./types";
import { ProjectRepository } from "src/project/repository";

@Service()
@Controller("/Contributors")
export class ContributorController {
constructor(private readonly contributorRepository: ContributorRepository) {}
constructor(
private readonly contributorRepository: ContributorRepository,
private readonly projectRepository: ProjectRepository,
) {}

@Get("/")
public async getContributors(): Promise<GetContributorsResponse> {
Expand All @@ -20,15 +24,18 @@ export class ContributorController {

@Get("/:id")
public async getContributor(@Param("id") id: string): Promise<GetContributorResponse> {
const [contributor] = await Promise.all([this.contributorRepository.findWithStats(id)]);
const [contributor, projects] = await Promise.all([
this.contributorRepository.findWithStats(id),
this.projectRepository.findForContributor(id),
]);

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

return {
contributor: {
...contributor,
// @TODO-ZM: Add contributions and projects
// projects,
projects,
// @TODO-ZM: Add contributions
// contributions,
},
};
Expand Down
9 changes: 9 additions & 0 deletions api/src/contributor/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ContributorEntity } from "@dzcode.io/models/dist/contributor";
import { ProjectEntity } from "@dzcode.io/models/dist/project";
import { GeneralResponse } from "src/app/types";

export interface GetContributorsResponse extends GeneralResponse {
Expand All @@ -16,5 +17,13 @@ export interface GetContributorResponse extends GeneralResponse {
ranking: number;
totalContributionScore: number;
totalRepositoryCount: number;
projects: Array<
Pick<ProjectEntity, "id" | "name"> & {
totalRepoContributorCount: number;
totalRepoScore: number;
totalRepoStars: number;
ranking: number;
}
>;
};
}
41 changes: 41 additions & 0 deletions api/src/project/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,47 @@ export class ProjectRepository {
return camelCased;
}

public async findForContributor(id: string) {
const statement = sql`
SELECT
id,
name,
sum(repo_with_stats.contributor_count)::int as total_repo_contributor_count,
sum(repo_with_stats.stars)::int as total_repo_stars,
sum(repo_with_stats.score)::int as total_repo_score,
ROUND( 100 * sum(repo_with_stats.contributor_count) + 100 * sum(repo_with_stats.stars) + max(repo_with_stats.score) - sum(repo_with_stats.score) / sum(repo_with_stats.contributor_count) )::int as ranking
FROM
(
SELECT
repository_id,
project_id,
sum(score) as score,
count(*) as contributor_count,
stars
FROM
${contributorRepositoryRelationTable}
JOIN
${repositoriesTable} ON ${contributorRepositoryRelationTable.repositoryId} = ${repositoriesTable.id}
WHERE
${contributorRepositoryRelationTable.contributorId} = ${id}
GROUP BY
${contributorRepositoryRelationTable.repositoryId}, ${repositoriesTable.projectId}, ${repositoriesTable.stars}
) as repo_with_stats
JOIN
${projectsTable} ON ${projectsTable.id} = repo_with_stats.project_id
GROUP BY
${projectsTable.id}
ORDER BY
ranking DESC
`;

const raw = await this.postgresService.db.execute(statement);
const entries = Array.from(raw);
const unStringifiedRaw = unStringifyDeep(entries);
const camelCased = camelCaseObject(unStringifiedRaw);
return camelCased;
}

public async findForSitemap() {
const statement = sql`
SELECT
Expand Down
5 changes: 4 additions & 1 deletion web/src/components/locale/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ Besides the open tasks on [/Contribute](/Contribute) page, you can also contribu
en: "repositories",
ar: "مستودعات",
},

"contributor-contributed-to-projects": {
en: "Contributed to projects",
ar: "ساهم في المشاريع",
},
"projects-title": {
en: "Browse a growing list of Algerian open-source projects | DzCode i/o",
ar: "تصفح قائمة المشاريع الجزائرية مفتوحة المصدر | DzCode i / o",
Expand Down
19 changes: 19 additions & 0 deletions web/src/pages/team/contributor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TryAgain } from "src/components/try-again";
import { fetchContributorAction } from "src/redux/actions/contributor";
import { useAppDispatch, useAppSelector } from "src/redux/store";
import { getContributorURL } from "src/utils/contributor";
import { getProjectURL } from "src/utils/project";

// ts-prune-ignore-next
export default function Page(): JSX.Element {
Expand Down Expand Up @@ -81,6 +82,24 @@ export default function Page(): JSX.Element {
</div>
</div>
</div>
{contributor.projects.length > 0 ? (
<>
<h2 className="text-lg font-bold">
<Locale contributor-contributed-to-projects />
</h2>
<div className="flex flex-row gap-4 flex-wrap">
{contributor.projects.map((project, projectIndex) => (
<Link
key={projectIndex}
href={getProjectURL(project)}
className="card card-compact bg-base-200 rounded-lg p-4 w-full md:w-auto"
>
{project.name}
</Link>
))}
</div>
</>
) : null}
</div>
)}
</div>
Expand Down

0 comments on commit 86f7dbd

Please sign in to comment.