diff --git a/package-lock.json b/package-lock.json index f08e2bf..6ea85f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "bcrypt": "^5.1.1", "body-parser": "^1.20.2", "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "dotenv": "^16.4.1", "express": "^4.18.2", "express-async-handler": "^1.2.0", @@ -380,9 +381,9 @@ } }, "node_modules/cookie": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", - "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", "engines": { "node": ">= 0.6" } @@ -412,6 +413,18 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -514,16 +527,16 @@ } }, "node_modules/express": { - "version": "4.18.2", - "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", - "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "version": "4.19.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.19.2.tgz", + "integrity": "sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", - "body-parser": "1.20.1", + "body-parser": "1.20.2", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.5.0", + "cookie": "0.6.0", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", @@ -559,43 +572,6 @@ "resolved": "https://registry.npmjs.org/express-async-handler/-/express-async-handler-1.2.0.tgz", "integrity": "sha512-rCSVtPXRmQSW8rmik/AIb2P0op6l7r1fMW538yyvTMltCO4xQEWMmobfrIxN2V1/mVrgxB8Az3reYF6yUZw37w==" }, - "node_modules/express/node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", - "dependencies": { - "bytes": "3.1.2", - "content-type": "~1.0.4", - "debug": "2.6.9", - "depd": "2.0.0", - "destroy": "1.2.0", - "http-errors": "2.0.0", - "iconv-lite": "0.4.24", - "on-finished": "2.4.1", - "qs": "6.11.0", - "raw-body": "2.5.1", - "type-is": "~1.6.18", - "unpipe": "1.0.0" - }, - "engines": { - "node": ">= 0.8", - "npm": "1.2.8000 || >= 1.4.16" - } - }, - "node_modules/express/node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", - "dependencies": { - "bytes": "3.1.2", - "http-errors": "2.0.0", - "iconv-lite": "0.4.24", - "unpipe": "1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, "node_modules/fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -1634,9 +1610,9 @@ } }, "node_modules/tar": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/tar/-/tar-6.2.0.tgz", - "integrity": "sha512-/Wo7DcT0u5HUV486xg675HtjNd3BXZ6xDbzsCUZPt5iw8bTQ63bP0Raut3mvro9u+CUyq7YQd8Cx55fsZXxqLQ==", + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/tar/-/tar-6.2.1.tgz", + "integrity": "sha512-DZ4yORTwrbTj/7MZYq2w+/ZFdI6OZ/f9SFHR+71gIVUZhOQPHzVCLpvRnPgyaMpfWxxk/4ONva3GQSyNIKRv6A==", "dependencies": { "chownr": "^2.0.0", "fs-minipass": "^2.0.0", diff --git a/package.json b/package.json index 1f65ba7..c1e7cd8 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "bcrypt": "^5.1.1", "body-parser": "^1.20.2", "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "dotenv": "^16.4.1", "express": "^4.18.2", "express-async-handler": "^1.2.0", diff --git a/requests.rest b/requests.rest index a461380..5642f14 100644 --- a/requests.rest +++ b/requests.rest @@ -36,7 +36,7 @@ POST http://localhost:3000/project/create Content-Type: application/json { - "teamId": 1, + "teamId": 3, "projectName": "newProject" } @@ -49,16 +49,16 @@ Content-Type: application/json { "uuid": "d742cdcd-4802-41e3-9f95-f5bb25a7c774" } - ### -// List Projects of team - -GET http://localhost:3000/project/ -Content-Type: application/json +GET http://localhost:3000/project?teamId=3 +### +GET http://localhost:3000/project/5 +### +// List members of team -{ - "teamId": 1 -} +GET http://localhost:3000/team/1/member +### +post http://localhost:3000/project/delete/5 //______________________________________________________ @@ -143,12 +143,9 @@ Content-Type: application/json ### // Get all users of the team -GET http://localhost:3000/team/member +GET http://localhost:3000/team/1/member Content-Type: application/json -{ - "teamId": 7 -} ### // List all teams of the user @@ -156,8 +153,10 @@ Content-Type: application/json GET http://localhost:3000/team Content-Type: application/json +### +DELETE http://localhost:3000/database/c7abcf80-a9e6-40c6-ba40-2e4231a05693?projectId=5 ### // Delete member from team @@ -178,4 +177,18 @@ Content-Type: application/json { "teamId": 8 -} \ No newline at end of file +} + +### +// Delete member from team + + +POST http://localhost:3000/project/create +Content-Type: application/json + +{ + "teamId": "2", + "projectName": "project2" +} + +### diff --git a/src/app.js b/src/app.js index 4732f30..6a05611 100644 --- a/src/app.js +++ b/src/app.js @@ -1,4 +1,5 @@ import express from "express"; +import cors from "cors" import userRoute from "./routes/userRoute.js"; import cookieParser from "cookie-parser"; import { tokenAuth } from "./middlewares/authMiddleware.js"; @@ -10,6 +11,7 @@ const app = express(); const port = 3000; app.use(express.json()); +app.use(cors()); app.use(express.urlencoded({ extended: true })); app.use(cookieParser()); app.use("/", userRoute); diff --git a/src/controllers/databaseController.js b/src/controllers/databaseController.js index 2b2b4c9..6c865da 100644 --- a/src/controllers/databaseController.js +++ b/src/controllers/databaseController.js @@ -34,13 +34,16 @@ export const updateDatabase = async (req, res) => { }; export const deleteDatabase = async (req, res) => { - const { projectId, databaseId } = req.body; - const database = await dataModel.getDatabase(databaseId); + const databaseID = req.params.databaseID; + const projectId = +(req.query.projectId) + 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); + await dataModel.deleteDatabase(databaseID); + res + .status(204) + .json({ message: "database deleted successfully" }); }; diff --git a/src/controllers/projectController.js b/src/controllers/projectController.js index f9ebca4..54c56f3 100644 --- a/src/controllers/projectController.js +++ b/src/controllers/projectController.js @@ -25,7 +25,7 @@ export const createProject = async (req, res) => { export const updateProject = async (req, res) => { const { projectID, newName } = req.body; - res.send(await updateProject(projectID, newName)); + res.send(await projectModel.updateProject(+projectID, newName)); }; export const joinProject = async (req, res) => { const uuid = req.body.uuid; @@ -52,12 +52,21 @@ export const joinProject = async (req, res) => { }; export const deleteProject = async (req, res) => { - const { projectID } = req.body; + const projectID = +(req.params.projectID) await deleteProject(projectID); res.send('project deleted'); }; export const listProjects = async (req, res) => { - const activeTeam = req.body.teamId; + const activeTeam = +(req.query.teamId); res.json({ projects: await projectModel.allProjects(activeTeam) }); }; + +export const getProject = async (req, res) => { + const projectID = +(req.params.projectID) + const project = await projectModel.getProject(projectID) + if (project){ + return res.status(200).json(project) + } + return res.status(404).json({message: "project doesn't exist"}) +} diff --git a/src/controllers/teamController.js b/src/controllers/teamController.js index 5b6eb6e..7788ebc 100644 --- a/src/controllers/teamController.js +++ b/src/controllers/teamController.js @@ -70,9 +70,13 @@ export const deleteTeam = asyncHandler(async (req, res) => { }); export const getMembers = asyncHandler(async (req, res) => { - const { teamId } = req.body; - const team = await teamModel.getTeam(teamId); - res.status(200).json(team[0].members.map((member) => member.user.email)); + const { teamId } = req.params; + const team = await teamModel.getTeam(+teamId); + res.status(200).json(team.members.map((member) => ({ + email: member.user.email, + name: member.user.name, + role: member.role + }))); }); export const allTeamsOfUser = asyncHandler(async (req, res) => { diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 84e0a0c..f10225d 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -3,7 +3,7 @@ import bcrypt from "bcrypt"; const secretKey = process.env.SECRET_KEY; import * as userModel from "../models/userModel.js"; import * as teamModel from "../models/teamModel.js"; -import { memberExist, updateMember } from "../models/teamModel"; +import { memberExist, updateMember } from "../models/teamModel.js"; export const loginController = async (req, res) => { const { email, password } = req.body; diff --git a/src/models/teamModel.js b/src/models/teamModel.js index d71647a..e7d8e3e 100644 --- a/src/models/teamModel.js +++ b/src/models/teamModel.js @@ -62,11 +62,13 @@ export async function deleteMember(memberEmail, teamId) { } export async function getTeam(teamId) { - const team = await prisma.team.findMany({ - where: { id: teamId }, - select: { members: { select: { user: true } } }, - }); - return team; + const team = await prisma.team.findUnique({ + where: { + id: teamId, + }, + include: { members: { include: { user: true } } }, + }) + return team } export async function updateTeam(newName, teamId) { diff --git a/src/routes/databaseRoute.js b/src/routes/databaseRoute.js index 58d846b..631f7e5 100644 --- a/src/routes/databaseRoute.js +++ b/src/routes/databaseRoute.js @@ -8,7 +8,10 @@ import { const route = express.Router(); -route.get("/", tokenAuth, databaseAccess, control.getDatabase); +route.get("/", +tokenAuth, +databaseAccess, +control.getDatabase); route.post( "/", tokenAuth, @@ -24,7 +27,7 @@ route.put( control.updateDatabase ); route.delete( - "/", + "/:databaseID", tokenAuth, databaseAccess, authorizedUser, diff --git a/src/routes/projectRoute.js b/src/routes/projectRoute.js index 1c0f038..eb3155c 100644 --- a/src/routes/projectRoute.js +++ b/src/routes/projectRoute.js @@ -5,6 +5,7 @@ import { deleteProject, joinProject, listProjects, + getProject } from "../controllers/projectController.js"; import { tokenAuth } from "../middlewares/authMiddleware.js"; @@ -12,8 +13,9 @@ const route = express.Router(); route.post("/create", tokenAuth, createProject); route.post("/update", tokenAuth, updateProject); -route.post("/delete", tokenAuth, deleteProject); +route.post("/delete/:projectID", tokenAuth, deleteProject); route.post("/join", tokenAuth, joinProject); +route.get("/:projectID", tokenAuth, getProject) route.get("/", tokenAuth, listProjects); export default route; diff --git a/src/routes/teamRoute.js b/src/routes/teamRoute.js index 3a0aba3..2263d02 100644 --- a/src/routes/teamRoute.js +++ b/src/routes/teamRoute.js @@ -11,7 +11,7 @@ router .delete(tokenAuth, teamController.deleteTeam) .put(tokenAuth, teamController.updateTeam); router - .route("/member") + .route("/:teamId/member") .get(tokenAuth, teamController.getMembers) .put(tokenAuth, teamController.addMember) .delete(tokenAuth, teamController.deleteMember); diff --git a/src/routes/userRoute.js b/src/routes/userRoute.js index 3f2f80b..01209c4 100644 --- a/src/routes/userRoute.js +++ b/src/routes/userRoute.js @@ -3,6 +3,7 @@ import { loginController, registerController, logoutController, + grantController } from "../controllers/userController.js"; const route = express.Router();