From 970f8be7db8b60329947fc2c504721d45e13ba80 Mon Sep 17 00:00:00 2001 From: Zakaria Mansouri Date: Mon, 23 Sep 2024 14:20:44 +0200 Subject: [PATCH] get project name from api --- api/src/project/controller.ts | 9 ++++++++- api/src/project/repository.ts | 17 +++++++++++++++++ api/src/project/types.ts | 4 ++++ web/cloudflare/functions/projects/[slug].ts | 15 +++++++++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/api/src/project/controller.ts b/api/src/project/controller.ts index fc8a853de..20a07d011 100644 --- a/api/src/project/controller.ts +++ b/api/src/project/controller.ts @@ -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"; @@ -44,4 +44,11 @@ export class ProjectController { }, }; } + + @Get("/:id/name") + public async getProjectName(@Param("id") id: number): Promise { + const project = await this.projectRepository.findName(id); + + return { project }; + } } diff --git a/api/src/project/repository.ts b/api/src/project/repository.ts index 701fe19a0..45d6136ed 100644 --- a/api/src/project/repository.ts +++ b/api/src/project/repository.ts @@ -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, diff --git a/api/src/project/types.ts b/api/src/project/types.ts index aa266e609..65358b8f6 100644 --- a/api/src/project/types.ts +++ b/api/src/project/types.ts @@ -37,3 +37,7 @@ export interface GetProjectResponse extends GeneralResponse { stars: number; }; } + +export interface GetProjectNameResponse extends GeneralResponse { + project: Pick; +} diff --git a/web/cloudflare/functions/projects/[slug].ts b/web/cloudflare/functions/projects/[slug].ts index 7dfcdba37..dc6778c86 100644 --- a/web/cloudflare/functions/projects/[slug].ts +++ b/web/cloudflare/functions/projects/[slug].ts @@ -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>Project ${slug} at DzCode`); + 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>${pageTitle}`); return new Response(newData, { headers: {