Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend api integration #26

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const AppError = require("./utils/appError");

const hackathonRouter = require("./routes/hackathonRoutes");
const userRouter = require("./routes/userRoutes");
const projectRouter = require("./routes/projectRoute");
const teamRouter = require("./routes/teamRoute");

dotenv.config();

Expand Down Expand Up @@ -54,6 +56,8 @@ app.get("/app/v1", (req, res, next) => {
//mongo routes to check
app.use("/api/v1/users", userRouter);
app.use("/api/v1/hackathon", hackathonRouter);
app.use("/api/v1/project", projectRouter);
app.use("/api/v1/team", teamRouter);

app.use("*", (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
Expand Down
17 changes: 17 additions & 0 deletions backend/controllers/hackathonController.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ exports.getAllProjects = catchAsync(async (req, res, next) => {
return next(new AppError("no such hackathon exists", 400));
}
});
exports.getAllHackathon = catchAsync(async (req, res, next) => {
const hackathons = await hackathonModel.find();
res.status(200).json({
hackathons,
message: "found all hackathons",
});
});
exports.getHackathonByPagination = catchAsync(async (req, res, next) => {
const page = req.params.page * 1 || 1;
const limit = req.params.limit * 1 || 10;
const skip = (page - 1) * limit;
const hackathons = await hackathonModel.find().skip(skip).limit(limit);
res.status(200).json({
hackathons,
message: "found all hackathons",
});
});
exports.getHackathon = catchAsync(async (req, res, next) => {
const hackathonID = req.params.hackathonID;
const hackathon = await hackathonModel.findById(hackathonID);
Expand Down
79 changes: 79 additions & 0 deletions backend/controllers/projectController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const projectModel = require("./../models/projectModel.js");
const catchAsync = require("./../utils/catchAsync.js");

exports.getProject = catchAsync(async (req, res, next) => {
const projectId = req.params.projectID;
const project = await projectModel.findById(projectId);
res.status(200).json({
message: "Project",
project,
});
});

exports.submissionProject = catchAsync(async (req, res, next) => {
const newProject = await projectModel.create(req.body);
res.status(200).json({
message: "Project submitted",
newProject,
});
});

exports.updateProject = catchAsync(async (req, res, next) => {
const projectId = req.params.projectID;
const data = req.body;
const leaderId = req.params.leaderId;

const checkProject = await projectModel.findById(projectId);
if (checkProject.projectLeader != leaderId) {
return res.status(401).json({
message: "You are not authorized to update this project",
});
}

const project = await projectModel.findByIdAndUpdate(projectId, data, {
new: true,
});

res.status(200).json({
message: "Project updated",
project,
});
});

exports.deleteProject = catchAsync(async (req, res, next) => {
const projectId = req.params.projectID;
const leaderId = req.params.leaderId;
const checkProject = await projectModel.findById(projectId);

if (checkProject.projectLeader != leaderId) {
return res.status(401).json({
message: "You are not authorized to delete this project",
});
}

const project = await projectModel.findByIdAndDelete(projectId);
res.status(200).json({
message: "Project deleted",
project,
});
});

exports.getAllProjectsOfaUser = catchAsync(async (req, res, next) => {
const userId = req.params.userID;
const projects = await projectModel
.find({ "team.members": userId })
.populate("team.members");
res.status(200).json({
message: "All projects of a user",
projects,
});
});

exports.getProjectOfaTeam = catchAsync(async (req, res, next) => {
const teamId = req.params.teamID;
const project = await projectModel.find({ team: teamId }).populate("team");
res.status(200).json({
message: "Project of a team",
project,
});
});
174 changes: 174 additions & 0 deletions backend/controllers/teamController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
const teamModel = require("./../models/teamModel.js");
const catchAsync = require("./../utils/catchAsync.js");

exports.getTeam = catchAsync(async (req, res, next) => {
const teamId = req.params.teamID;
const team = await teamModel.findById(teamId);
res.status(200).json({
message: "Team",
team,
});
});

exports.createTeam = catchAsync(async (req, res, next) => {
const newTeam = await teamModel.create(req.body);
res.status(200).json({
message: "Team Created",
newTeam,
});
});

exports.updateTeam = catchAsync(async (req, res, next) => {
const teamId = req.params.teamID;
const data = req.body;
const userId = req.params.userID;
const checkTeam = await teamModel.findById(teamId);

if (checkTeam.teamLeader != userId) {
return res.status(401).json({
message: "You are not authorized to update this team",
});
}

const team = await teamModel.findByIdAndUpdate(teamId, data, {
new: true,
});

res.status(200).json({
message: "Team updated",
team,
});
});

exports.deleteTeam = catchAsync(async (req, res, next) => {
const teamId = req.params.teamID;
const team = await teamModel.findByIdAndDelete(teamId);
const userId = req.params.userID;
const checkTeam = await teamModel.findById(teamId);

if (checkTeam.teamLeader != userId) {
return res.status(401).json({
message: "You are not authorized to delete this team",
});
}

res.status(200).json({
message: "Team deleted",
team,
});
});

exports.getAllTeamsOfhackthodId = catchAsync(async (req, res, next) => {
const hackthodId = req.params.hackthodID;
const teams = await teamModel
.find({ hackthodId: hackthodId })
.populate("team");
res.status(200).json({
message: "All teams of a hackthod",
teams,
});
});

exports.getTeamsOfhackthodIdByPagination = catchAsync(
async (req, res, next) => {
const hackthodId = req.params.hackthodID;
const page = req.query.page * 1 || 1;
const limit = req.query.limit * 1 || 5;
const skip = (page - 1) * limit;
const teams = await teamModel
.find({ hackthodId: hackthodId })
.populate("team")
.skip(skip)
.limit(limit);
res.status(200).json({
message: "All teams of a hackthod",
teams,
});
}
);

exports.getTeamsOfUser = catchAsync(async (req, res, next) => {
const userId = req.params.userID;
const teams = await teamModel.find({ members: userId }).populate("members");
res.status(200).json({
message: "All teams of a user",
teams,
});
});

exports.joinTeamBySecretkey = catchAsync(async (req, res, next) => {
const secretkey = req.params.secretkey;
const team = await teamModel.findOne({ secretkey: secretkey });
if (team) {
const userId = req.params.userID;
const members = [...team.members];
members.push(userId);
const updatedTeam = await teamModel.findByIdAndUpdate(
team._id,
{ members: members },
{ new: true }
);
res.status(200).json({
message: "Team joined",
updatedTeam,
});
} else {
res.status(404).json({
message: "Team not found",
});
}
});

exports.leaveTeam = catchAsync(async (req, res, next) => {
const teamId = req.params.teamID;
const userId = req.params.userID;
const team = await teamModel.findById(teamId);
const members = [...team.members];
const index = members.indexOf(userId);
if (index > -1) {
members.splice(index, 1);
}
const updatedTeam = await teamModel.findByIdAndUpdate(
teamId,
{ members: members },
{ new: true }
);
res.status(200).json({
message: "Team left",
updatedTeam,
});
});

exports.removeMemberFromTeam = catchAsync(async (req, res, next) => {
const userId = req.params.userID;
const teamId = req.params.teamID;
const leaderId = req.params.leaderID;
const team = await teamModel.findById(teamId);

if (!team) {
return res.status(404).json({
message: "Team not found",
});
}

if (team.teamLeader === leaderId) {
return res.status(401).json({
message: "You are not authorized to remove this member",
});
}

const members = [...team.members];
const index = members.indexOf(userId);
if (index > -1) {
members.splice(index, 1);
}
const updatedTeam = await teamModel.findByIdAndUpdate(
teamId,
{ members: members },
{ new: true }
);
res.status(200).json({
message: "Member removed from team",
updatedTeam,
});
});
6 changes: 5 additions & 1 deletion backend/models/projectModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const projectSchema = new mongoose.Schema(
team: {
type: mongoose.Schema.ObjectId,
ref: "Team",
required:true,
required: true,
},
name: {
type: String,
Expand All @@ -30,6 +30,10 @@ const projectSchema = new mongoose.Schema(
tracks: {
type: [String],
},
projectLeader: {
type: String,
required: true,
},
},
{
toJSON: { virtuals: true },
Expand Down
27 changes: 27 additions & 0 deletions backend/models/teamModel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
const mongoose = require("mongoose");

const generateSecretKey = () => {
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+";
let secretkey = "";
const charactersLength = characters.length;
for (let i = 0; i < 12; i++) {
secretkey += characters.charAt(
Math.floor(Math.random() * charactersLength)
);
}
return secretkey;
};

const teamSchema = mongoose.Schema(
{
teamname: {
Expand All @@ -22,6 +36,19 @@ const teamSchema = mongoose.Schema(
return this.teamsize < 5;
},
},
hackthodId: {
type: mongoose.Schema.ObjectId,
ref: "Hackathon",
},
teamLeader: {
type: String,
required: true,
},
secretkey: {
type: String,
required: true,
default: generateSecretKey(),
},
},
{
toJSON: { virtuals: true },
Expand Down
Loading