Created the GET /projects/list & GET /projects/:id route

This commit is contained in:
Mathias Wagner 2023-01-22 15:15:25 +01:00
parent 332e618d52
commit d668b26b51
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,10 +1,28 @@
import { Request, Response, Router } from "express";
import { createProject, deleteProject, patchProject, regenerateKey } from "@controller/projects";
import {
createProject,
deleteProject,
getProject,
listProjects,
patchProject,
regenerateKey,
} from "@controller/projects";
import { validateSchema } from "@utils/error";
import { patchProjectValidation, projectCreationValidation } from "./validations/project";
const app: Router = Router();
app.get("/list", async (req: Request, res: Response) => {
res.json(await listProjects(String(req.user?._id)));
});
app.get("/:id", async (req: Request, res: Response) => {
const request = await getProject(req.params.id, String(req.user?._id));
if ("code" in request) return res.json(request);
res.json(request);
});
app.put("/", async (req: Request, res: Response) => {
if (validateSchema(res, projectCreationValidation, req.body)) return;
@ -15,7 +33,7 @@ app.put("/", async (req: Request, res: Response) => {
});
app.delete("/:id", async (req: Request, res: Response) => {
const deletionError = await deleteProject(req.params.id);
const deletionError = await deleteProject(req.params.id, String(req.user?._id));
if (deletionError) return res.json(deletionError);
res.json({ message: "The project has been successfully deleted" });
@ -24,14 +42,14 @@ app.delete("/:id", async (req: Request, res: Response) => {
app.patch("/:id", async (req: Request, res: Response) => {
if (validateSchema(res, patchProjectValidation, req.body)) return;
const patchError = await patchProject(req.params.id, req.body);
const patchError = await patchProject(req.params.id, String(req.user?._id), req.body);
if (patchError) return res.json(patchError);
res.json({ message: "The project has been successfully updated" });
});
app.post("/:id/regenerate", async (req: Request, res: Response) => {
const tokenError = await regenerateKey(req.params.id);
const tokenError = await regenerateKey(req.params.id, String(req.user?._id));
if (tokenError) return res.json(tokenError);
res.json({ message: "The validation key has been regenerated" });