Skip to content

Commit

Permalink
Merge pull request #15 from KinanaDB/database
Browse files Browse the repository at this point in the history
database endpoints
  • Loading branch information
Mohamed-khattab authored Feb 23, 2024
2 parents 34c79a7 + 993103a commit 6c5be4b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import userRoute from "./routes/userRoute.js";
import cookieParser from "cookie-parser";
import { tokenAuth } from "./middlewares/authMiddleware.js";
import projectRoute from "./routes/projectRoute.js";
import DatabaseRoute from "./routes/databaseRoute.js";
import teamRoute from "./routes/teamRoute.js";

const app = express();
Expand All @@ -17,8 +18,9 @@ app.get("/", tokenAuth, (req, res) => {
res.send(`Logged in as: ${req.user.email}`);
});

app.use("/project", projectRoute);
app.use("/team", teamRoute);
app.use("/project", projectRoute);
app.use("/database", DatabaseRoute);

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
Expand Down
46 changes: 46 additions & 0 deletions src/controllers/databaseController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as dataModel from "../models/databaseModel.js";

export const getDatabase = async (req, res) => {
const { projectId } = req.query;
const databases = await dataModel.allDatabases(+projectId);
res.status(200).json({ databases: databases });
};

export const createDatabase = async (req, res) => {
const { projectId, databaseName } = req.body;
const database = await dataModel.createDatabase(projectId, databaseName);
res.status(201).json({
message: "database created successfully",
database: database,
});
};

export const updateDatabase = async (req, res) => {
const { projectId, databaseId, databaseName } = req.body;
const database = await dataModel.getDatabase(databaseId);
if (!database || database.projectId != projectId) {
return res
.status(404)
.json({ message: "database doesn't exist in the project" });
}
const updated_database = await dataModel.updateDatabase(
databaseId,
databaseName
);
res.status(200).json({
message: "database updated successfully",
updatedDatabase: updated_database,
});
};

export const deleteDatabase = async (req, res) => {
const { projectId, databaseId } = req.body;
const database = await dataModel.getDatabase(databaseId);
if (!database || database.projectId != projectId) {
return res
.status(404)
.json({ message: "database doesn't exist in the project" });
}
await dataModel.deleteDatabase(databaseId);
res.status(204);
};
31 changes: 31 additions & 0 deletions src/middlewares/databaseMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getProject } from "../models/projectModel.js";
import { memberExist } from "../models/teamModel.js";

export const databaseAccess = async (req, res, next) => {
let { projectId } = req.body;
if (!projectId) {
projectId = req.query.projectId;
}
const project = await getProject(+projectId);
if (!project) {
return res.status(404).json({ message: "project doesn't exist" });
}
const user_id = req.user.id;
const teamMember = await memberExist(project.teamId, user_id);
if (!teamMember) {
return res
.status(403)
.json({ message: "user doesn't exist in the project" });
}
req.role = teamMember.role;
next();
};

export const authorizedUser = async (req, res, next) => {
if (req.role != "LEADER") {
return res
.status(403)
.json({ message: "user doesn't have permissions to create a database" });
}
next();
};
34 changes: 34 additions & 0 deletions src/routes/databaseRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import express from "express";
import * as control from "../controllers/databaseController.js";
import { tokenAuth } from "../middlewares/authMiddleware.js";
import {
authorizedUser,
databaseAccess,
} from "../middlewares/databaseMiddleware.js";

const route = express.Router();

route.get("/", tokenAuth, databaseAccess, control.getDatabase);
route.post(
"/",
tokenAuth,
databaseAccess,
authorizedUser,
control.createDatabase
);
route.put(
"/",
tokenAuth,
databaseAccess,
authorizedUser,
control.updateDatabase
);
route.delete(
"/",
tokenAuth,
databaseAccess,
authorizedUser,
control.deleteDatabase
);

export default route;

0 comments on commit 6c5be4b

Please sign in to comment.