Skip to content

Commit

Permalink
add route to get latest project
Browse files Browse the repository at this point in the history
  • Loading branch information
Tch1b0 committed Jun 28, 2024
1 parent 395ed7e commit 779015d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Router, {
import { readBody, H3Event } from "h3";
import { User } from "./classes/user";
import ProjectCollection from "./classes/projectCollection";
import { Project } from "./classes/project";

const app = new Router();
const admin = new User(
Expand All @@ -19,6 +20,8 @@ export const github = new GitHub(process.env["GH_USERNAME"] || "Tch1b0");

export const projectCollection = new ProjectCollection();

let latestProject: Project | null;

/**
* validate that the user is authenticated
* @param req the request object of the request
Expand Down Expand Up @@ -50,6 +53,15 @@ app.get("/project", (e) => {
sendJson(e.res, project.toJSON());
});

app.get("/project-latest", (e) => {
const project = latestProject ?? projectCollection.getRandomProject();
if (!project) {
sendError(e.res, "No projects found", 404);
return;
}
sendJson(e.res, project.toJSON());
});

app.get("/project-ids", (e) => {
sendJson(
e.res,
Expand Down Expand Up @@ -95,6 +107,7 @@ app.post("/article", async (e) => {
const project = projectCollection.getProjectById(projectId);
project.addArticle(content, images);
projectCollection.save();
latestProject = project;
e.res.end("Ok");
});

Expand Down
9 changes: 9 additions & 0 deletions server/classes/projectCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ export default class ProjectCollection {
return this.projects.find((project) => project.id === id);
}

getRandomProject(): Project {
const projectsWithArticle = this.projects.filter(
(p) => p.article !== null && p.article !== undefined,
);
return projectsWithArticle[
Math.floor(Math.random() * projectsWithArticle.length)
];
}

/**
* checks if there are outdated posts to migrate, and migrates them
*/
Expand Down
9 changes: 9 additions & 0 deletions utility/datafetching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export async function getProject(id: number | string): Promise<Project> {
return project;
}

export async function getLatestProjectMeta(): Promise<Project> {
const project = await getFromApi<Project>(
`project-latest`,
`project-latest`,
);

return project;
}

export async function getProjects(): Promise<Project[]> {
const projects = await getFromApi<Project[]>("projects", "projects");

Expand Down

0 comments on commit 779015d

Please sign in to comment.