Created the GET /projects/list & GET /projects/:id route
This commit is contained in:
parent
332e618d52
commit
d668b26b51
@ -1,10 +1,28 @@
|
|||||||
import { Request, Response, Router } from "express";
|
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 { validateSchema } from "@utils/error";
|
||||||
import { patchProjectValidation, projectCreationValidation } from "./validations/project";
|
import { patchProjectValidation, projectCreationValidation } from "./validations/project";
|
||||||
|
|
||||||
const app: Router = Router();
|
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) => {
|
app.put("/", async (req: Request, res: Response) => {
|
||||||
if (validateSchema(res, projectCreationValidation, req.body)) return;
|
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) => {
|
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);
|
if (deletionError) return res.json(deletionError);
|
||||||
|
|
||||||
res.json({ message: "The project has been successfully deleted" });
|
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) => {
|
app.patch("/:id", async (req: Request, res: Response) => {
|
||||||
if (validateSchema(res, patchProjectValidation, req.body)) return;
|
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);
|
if (patchError) return res.json(patchError);
|
||||||
|
|
||||||
res.json({ message: "The project has been successfully updated" });
|
res.json({ message: "The project has been successfully updated" });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/:id/regenerate", async (req: Request, res: Response) => {
|
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);
|
if (tokenError) return res.json(tokenError);
|
||||||
|
|
||||||
res.json({ message: "The validation key has been regenerated" });
|
res.json({ message: "The validation key has been regenerated" });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user