From 5ab6342077d3611fd368d223f769bad01fe628de Mon Sep 17 00:00:00 2001 From: Ahd-3hd Date: Sun, 21 Jan 2024 15:19:48 +0300 Subject: [PATCH] feat: create projects and datasets --- src/controllers/dataset.controller.ts | 25 +++++++++++++++++++ src/controllers/project.controller.ts | 24 ++++++++++++++++++ src/controllers/workspace.controller.ts | 2 -- .../schemas/createDataset.schema.ts | 7 ++++++ .../schemas/createProject.schema.ts | 6 +++++ src/routers/dataset.router.ts | 10 ++++++++ src/routers/index.ts | 7 ++++++ src/routers/project.router.ts | 10 ++++++++ 8 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/controllers/dataset.controller.ts create mode 100644 src/controllers/project.controller.ts create mode 100644 src/middlewares/schemas/createDataset.schema.ts create mode 100644 src/middlewares/schemas/createProject.schema.ts create mode 100644 src/routers/dataset.router.ts create mode 100644 src/routers/project.router.ts diff --git a/src/controllers/dataset.controller.ts b/src/controllers/dataset.controller.ts new file mode 100644 index 0000000..3d68e75 --- /dev/null +++ b/src/controllers/dataset.controller.ts @@ -0,0 +1,25 @@ +import { NextFunction, Request, Response } from "express"; +import { DatasetModel } from "../models/Dataset.model"; + +export const createDataset = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + const { name, workspace, project } = req.body; + + const dataset = await DatasetModel.create({ + name, + workspace, + project, + }); + + res.status(201).json({ + dataset, + message: "Dataset created", + }); + } catch (error) { + next(error); + } +}; diff --git a/src/controllers/project.controller.ts b/src/controllers/project.controller.ts new file mode 100644 index 0000000..3cac56a --- /dev/null +++ b/src/controllers/project.controller.ts @@ -0,0 +1,24 @@ +import { NextFunction, Request, Response } from "express"; +import { ProjectModel } from "../models/Project.model"; + +export const createProject = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + const { name, workspace } = req.body; + + const project = await ProjectModel.create({ + name, + workspace, + }); + + res.status(201).json({ + project, + message: "Project created", + }); + } catch (error) { + next(error); + } +}; diff --git a/src/controllers/workspace.controller.ts b/src/controllers/workspace.controller.ts index 394f485..24b7f59 100644 --- a/src/controllers/workspace.controller.ts +++ b/src/controllers/workspace.controller.ts @@ -10,8 +10,6 @@ export const createWorkspace = async ( const { userid } = req.headers; const { name } = req.body; - console.log(userid); - const workspace = await WorkspaceModel.create({ name, owner: parseInt(userid as string), diff --git a/src/middlewares/schemas/createDataset.schema.ts b/src/middlewares/schemas/createDataset.schema.ts new file mode 100644 index 0000000..b8904e5 --- /dev/null +++ b/src/middlewares/schemas/createDataset.schema.ts @@ -0,0 +1,7 @@ +import { z } from "zod"; + +export const createDatasetSchema = z.object({ + name: z.string(), + workspace: z.number(), + project: z.number(), +}); diff --git a/src/middlewares/schemas/createProject.schema.ts b/src/middlewares/schemas/createProject.schema.ts new file mode 100644 index 0000000..2f23eba --- /dev/null +++ b/src/middlewares/schemas/createProject.schema.ts @@ -0,0 +1,6 @@ +import { z } from "zod"; + +export const createProjectSchema = z.object({ + name: z.string(), + workspace: z.number(), +}); diff --git a/src/routers/dataset.router.ts b/src/routers/dataset.router.ts new file mode 100644 index 0000000..c69b0c7 --- /dev/null +++ b/src/routers/dataset.router.ts @@ -0,0 +1,10 @@ +import { Router } from "express"; +import { validate } from "../middlewares/validate"; +import { createDataset } from "../controllers/dataset.controller"; +import { createDatasetSchema } from "../middlewares/schemas/createDataset.schema"; + +const router = Router(); + +router.post("", validate(createDatasetSchema), createDataset); + +export default router; diff --git a/src/routers/index.ts b/src/routers/index.ts index 4055b8c..b7b2df1 100644 --- a/src/routers/index.ts +++ b/src/routers/index.ts @@ -1,9 +1,16 @@ import { Router } from "express"; import workspaceRouter from "./workspace.router"; +import projectRouter from "./project.router"; +import datasetRouter from "./dataset.router"; const router = Router(); // /api/v1/workspace router.use("/workspace", workspaceRouter); +// /api/v1/project +router.use("/project", projectRouter); + +// /api/v1/dataset +router.use("/dataset", datasetRouter); export default router; diff --git a/src/routers/project.router.ts b/src/routers/project.router.ts new file mode 100644 index 0000000..bc1fc8a --- /dev/null +++ b/src/routers/project.router.ts @@ -0,0 +1,10 @@ +import { Router } from "express"; +import { createProject } from "../controllers/project.controller"; +import { validate } from "../middlewares/validate"; +import { createProjectSchema } from "../middlewares/schemas/createProject.schema"; + +const router = Router(); + +router.post("", validate(createProjectSchema), createProject); + +export default router;