Skip to content

Commit

Permalink
update the endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
reem2010 committed May 5, 2024
1 parent 7768499 commit a0b4ef3
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 82 deletions.
72 changes: 24 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 27 additions & 14 deletions requests.rest
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ POST http://localhost:3000/project/create
Content-Type: application/json

{
"teamId": 1,
"teamId": 3,
"projectName": "newProject"
}

Expand All @@ -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


//______________________________________________________
Expand Down Expand Up @@ -143,21 +143,20 @@ 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

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

Expand All @@ -178,4 +177,18 @@ Content-Type: application/json

{
"teamId": 8
}
}

###
// Delete member from team


POST http://localhost:3000/project/create
Content-Type: application/json

{
"teamId": "2",
"projectName": "project2"
}

###
2 changes: 2 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions src/controllers/databaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
};
15 changes: 12 additions & 3 deletions src/controllers/projectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"})
}
10 changes: 7 additions & 3 deletions src/controllers/teamController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions src/models/teamModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions src/routes/databaseRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {

const route = express.Router();

route.get("/", tokenAuth, databaseAccess, control.getDatabase);
route.get("/",
tokenAuth,
databaseAccess,
control.getDatabase);
route.post(
"/",
tokenAuth,
Expand All @@ -24,7 +27,7 @@ route.put(
control.updateDatabase
);
route.delete(
"/",
"/:databaseID",
tokenAuth,
databaseAccess,
authorizedUser,
Expand Down
4 changes: 3 additions & 1 deletion src/routes/projectRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import {
deleteProject,
joinProject,
listProjects,
getProject
} from "../controllers/projectController.js";
import { tokenAuth } from "../middlewares/authMiddleware.js";

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;
2 changes: 1 addition & 1 deletion src/routes/teamRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/routes/userRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
loginController,
registerController,
logoutController,
grantController
} from "../controllers/userController.js";
const route = express.Router();

Expand Down

0 comments on commit a0b4ef3

Please sign in to comment.