Skip to content

Commit

Permalink
get project name from api
Browse files Browse the repository at this point in the history
  • Loading branch information
ZibanPirate committed Sep 23, 2024
1 parent 3af75d3 commit 970f8be
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
9 changes: 8 additions & 1 deletion api/src/project/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Controller, Get, Param } from "routing-controllers";
import { Service } from "typedi";

import { ProjectRepository } from "./repository";
import { GetProjectResponse, GetProjectsResponse } from "./types";
import { GetProjectNameResponse, GetProjectResponse, GetProjectsResponse } from "./types";
import { RepositoryRepository } from "src/repository/repository";
import { ContributorRepository } from "src/contributor/repository";
import { ContributionRepository } from "src/contribution/repository";
Expand Down Expand Up @@ -44,4 +44,11 @@ export class ProjectController {
},
};
}

@Get("/:id/name")
public async getProjectName(@Param("id") id: number): Promise<GetProjectNameResponse> {
const project = await this.projectRepository.findName(id);

return { project };
}
}
17 changes: 17 additions & 0 deletions api/src/project/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,24 @@ import { ProjectRow, projectsTable } from "./table";
export class ProjectRepository {
constructor(private readonly sqliteService: SQLiteService) {}

public async findName(projectId: number) {
// @TODO-ZM: handle 404
const statement = sql`
SELECT
name
FROM
${projectsTable}
WHERE
id = ${projectId}
`;
const raw = this.sqliteService.db.get(statement);
const unStringifiedRaw = unStringifyDeep(raw);
const camelCased = camelCaseObject(unStringifiedRaw);
return camelCased;
}

public async findWithStats(projectId: number) {
// @TODO-ZM: handle 404
const statement = sql`
SELECT
p.id as id,
Expand Down
4 changes: 4 additions & 0 deletions api/src/project/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ export interface GetProjectResponse extends GeneralResponse {
stars: number;
};
}

export interface GetProjectNameResponse extends GeneralResponse {
project: Pick<ProjectEntity, "name">;
}
15 changes: 13 additions & 2 deletions web/cloudflare/functions/projects/[slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ declare const data: string;
// @ts-expect-error cloudflare converts this to a string using esbuild
import data from "../../public/index.html";

export const onRequest: PagesFunction = (context) => {
export const onRequest: PagesFunction = async (context) => {
// @TODO-ZM: import @dzcode.oi/utils
// const apiUrl = "https://api-stage.dzcode.io";
const apiUrl = "http://localhost:7070";

const pathName = new URL(context.request.url).pathname;
const slug = pathName.split("/").pop();
const newData = data.replace(/<title>.*<\/title>/, `<title>Project ${slug} at DzCode</title>`);
const projectId = slug.split("-").pop();
// @TODO-ZM: use fetchV2
const projectResponse = await fetch(`${apiUrl}/Projects/${projectId}/name`);
const projectData = await projectResponse.json();
// @ts-expect-error @TODO-ZM: import @dzcode.oi/api
const pageTitle = `See the details of ${projectData.project.name} project | DzCode i/o`;

const newData = data.replace(/<title>.*<\/title>/, `<title>${pageTitle}</title>`);

return new Response(newData, {
headers: {
Expand Down

0 comments on commit 970f8be

Please sign in to comment.