-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from KinanaDB/database
database endpoints
- Loading branch information
Showing
4 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |