Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
hizaidii authored Feb 13, 2024
1 parent 4200200 commit 4527f76
Show file tree
Hide file tree
Showing 69 changed files with 5,522 additions and 0 deletions.
91 changes: 91 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const express = require("express");
const app = express();
const path = require("path");
const PORT = 4444;
const bodyparser = require("body-parser");
const hbs = require("hbs");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo");
const session = require("express-session");
const passport = require("passport");
const isLoggedIn = require("./middlewares/isLoggedIn");
require("dotenv").config();
const moment = require("moment");

hbs.registerPartials(__dirname + "/views/partials");
app.set("view engine", "hbs");

// Session middleware configuration
app.use(
session({
secret: "hgsadjkhsa",
resave: false,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: process.env.MONGO_URL }),
})
);
// --> Middlewares
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json());
app.use(express.static(path.join(__dirname, "public")));
require("./utilities/passport");
app.use(passport.initialize());
app.use(passport.session());

// --> Routes
app.get("/", require("./routes/generalRoutes"));
app.get("/signup", require("./routes/generalRoutes"));
app.post("/signup", require("./routes/generalRoutes"));

// --> Passport Routes
app.post("/login", passport.authenticate("local", { failureRedirect: "/" }), require("./routes/generalRoutes"));
app.get("/auth/facebook", passport.authenticate("facebook"));
app.get("/auth/facebook/callback", passport.authenticate("facebook", { failureRedirect: "/" }), function (req, res) {
res.redirect("/app");
});
app.get("/auth/google", passport.authenticate("google", { scope: ["profile"] }));
app.get("/auth/google/callback", passport.authenticate("google", { failureRedirect: "/" }), function (req, res) {
res.redirect("/app");
});

// --> All APP routes mounted here
app.use("/app", isLoggedIn, require("./routes/appRoutes"));

// Catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error("Not Found");
err.status = 404;
next(err);
});
// --> Error Handling middleare
app.use((err, req, res, next) => {
// Set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};

// Render the error page
res.status(err.status || 500);
res.render("error", { error: err });
});

// --> HBS helper function
// Register a helper for date formatting
hbs.registerHelper("formatDate", function (dateString) {
const date = moment(dateString);
return date.format("MMM DD, YYYY - hh:mm A"); // Formats date as "Feb 06, 2024 - 11:47 PM"
});
// Defining a helper function to compare values
hbs.registerHelper("isEqual", function (a, b, options) {
if (a === b) {
return options.fn(this); // If values are equal, execute the block
} else {
return options.inverse(this); // If values are not equal, execute the else block
}
});

// --> Database Connection
mongoose.connect(process.env.MONGO_URL).then(() => {
app.listen(PORT, () => {
console.log(`http://localhost:` + PORT);
});
});
278 changes: 278 additions & 0 deletions controllers/appController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
const User = require("../models/users");
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");

// Go to app page
module.exports.getApp = (req, res, next) => {
const fullName = req.user.name;
const firstName = fullName.split(" ")[0];
const totalTasks = req.user.totalTasks;
const deletedTasks = req.user.deletedTasks;
const currentTasks = totalTasks - deletedTasks;
let completionRate = (deletedTasks / totalTasks) * 100;
if (isNaN(completionRate) || completionRate === Infinity || completionRate === -Infinity) {
completionRate = 0;
} else {
completionRate = Math.round(completionRate); // Round to the nearest integer
}

res.render("./app/app", {
user: req.user,
firstName,
currentTasks,
totalTasks,
deletedTasks,
completionRate,
});
};

// Logout
module.exports.getLogout = (req, res, next) => {
req.logout(function (err) {
if (err) {
return next(err);
}
res.redirect("/");
});
};

// Create a new board
module.exports.postNewBoard = async (req, res, next) => {
const { title } = req.body;

try {
let user = await User.findById(req.user._id);
user.boards.push({ title });
user.save();
let newBoard = user.boards[user.boards.length - 1];
res.redirect("/app/board?boardId=" + newBoard._id);
} catch (err) {
res.render("error", { err });
}
};

// Edit a board name
module.exports.postEditBoard = async (req, res, next) => {
const { title, boardId } = req.body;

try {
let user = await User.findById(req.user._id);

let board = user.boards.id(boardId);
board.title = title;
user.save();
res.redirect("/app/board?boardId=" + boardId);
} catch (err) {
res.render("error", { err });
}
};

// Go to any Board page
module.exports.getBoard = async (req, res, next) => {
const { boardId } = req.query;
try {
let user = await User.findById(req.user._id);
let board = user.boards.id(boardId);
if (!board) {
throw new Error("Board not found");
}
res.render("./app/board", {
board,
user: req.user,
});
} catch (err) {
res.render("error", { err });
}
};

// Create a new swimlane
module.exports.postNewSwimlane = async (req, res, next) => {
const { boardId, title, statusColor } = req.body;
try {
let user = await User.findById(req.user._id);
let board = user.boards.id(boardId);
if (!board) {
throw new Error("Board not found");
}
board.swimlanes.push({ title, statusColor });
user.save();
res.redirect("/app/board?boardId=" + boardId);
} catch (err) {
res.render("error", { err });
}
};

// Edit swimlane
module.exports.postEditSwimlane = async (req, res, next) => {
const { swimlaneId, boardId, title, statusColor } = req.body;
try {
let user = await User.findById(req.user._id);
let board = user.boards.id(boardId);
let swimlane = board.swimlanes.id(swimlaneId);
swimlane.title = title;
swimlane.statusColor = statusColor;
user.save();
res.redirect("/app/board?boardId=" + boardId);
} catch (err) {
res.render("error", { err });
}
};

// Create a new task
module.exports.postNewTask = async (req, res, next) => {
const { boardId, title, description, status } = req.body;
try {
let user = await User.findById(req.user._id);
let board = user.boards.id(boardId);

if (!board) {
throw new Error("Board not found");
}
let swimlaneID = board.swimlanes.id(status);
// board.swimlanes.push({ title });
swimlaneID.tasks.push({ title, description, creationDate: Date.now() });
user.totalTasks = Number(user.totalTasks) || 0;
user.totalTasks += 1;

user.save();
res.redirect("/app/board?boardId=" + boardId);
} catch (err) {
res.render("error", { err });
}
};

// Edit a task
module.exports.postEditTask = async (req, res, next) => {
const { taskId, swimlaneId, boardId, title, description } = req.body;
try {
let user = await User.findById(req.user._id);
let board = user.boards.id(boardId);
let swimlane = board.swimlanes.id(swimlaneId);
let task = swimlane.tasks.id(taskId);

task.title = title;
task.description = description;

user.save();
res.redirect("/app/board?boardId=" + boardId);
} catch (err) {
res.render("error", { err });
}
};

// moving tasks around on the board
module.exports.getMoveTask = async (req, res, next) => {
try {
const { boardId, orgSwimlaneId, swimlaneId, taskId, newPosition } = req.query;

const user = await User.findById(req.user.id);
if (!user) {
throw new Error("User not found");
}

// Find the board
const board = user.boards.id(boardId);
if (!board) {
throw new Error("Board not found");
}

// Find the original swimlane
const originalSwimlane = board.swimlanes.id(orgSwimlaneId);
if (!originalSwimlane) {
throw new Error("Original swimlane not found");
}

// Find the new swimlane
const newSwimlane = board.swimlanes.id(swimlaneId);
if (!newSwimlane) {
throw new Error("New swimlane not found");
}

// Find the task in the original swimlane
const task = originalSwimlane.tasks.id(taskId);
if (!task) {
throw new Error("Task not found in original swimlane");
}

// Remove the task from the original swimlane
originalSwimlane.tasks.pull(taskId);

// Add the task to the new swimlane at the specified position
newSwimlane.tasks.splice(newPosition, 0, task);

await user.save();

return { success: true, message: "Task moved successfully" };
} catch (error) {
console.error(error);
return { success: false, message: error.message };
}
};

// deleting a board
module.exports.getDeleteBoard = async (req, res, next) => {
const { boardId } = req.query;
try {
let user = await User.findById(req.user._id);
//counting all tasks in the board
let noOfTasks = 0;
user.boards.forEach((board) => {
if (board._id == boardId) {
board.swimlanes.forEach((swimlane) => {
noOfTasks += swimlane.tasks.length;
});
}
});
//updating the deleted tasks count
user.deletedTasks = Number(user.deletedTasks) || 0;
user.deletedTasks += noOfTasks;

// deleting the board
user.boards.pull({ _id: boardId });
await user.save();
res.redirect("/app");
} catch (err) {
res.render("error", { err });
}
};

// deleting a column
module.exports.getDeleteColumn = async (req, res, next) => {
const { swimlaneId, boardId } = req.query;
try {
let user = await User.findById(req.user._id);

let board = user.boards.id(boardId);

//marking all tasks in this swimlane as deleted
let noOfTasks = board.swimlanes.id(swimlaneId).tasks.length;
user.deletedTasks = Number(user.deletedTasks) || 0;
user.deletedTasks += noOfTasks;

// deleting the swimlane
board.swimlanes.pull({ _id: swimlaneId });

await user.save();
res.redirect("/app/board?boardId=" + boardId);
} catch (err) {
res.render("error", { err });
}
};

// deleting a task
module.exports.getDeleteTask = async (req, res, next) => {
const { taskId, swimlaneId, boardId } = req.query;
try {
let user = await User.findById(req.user._id);
let board = user.boards.id(boardId);
let swimlane = board.swimlanes.id(swimlaneId);
swimlane.tasks.pull({ _id: taskId });
user.deletedTasks = Number(user.deletedTasks) || 0;
user.deletedTasks += 1;

await user.save();
res.redirect("/app/board?boardId=" + boardId);
} catch (err) {
res.render("error", { err });
}
};
Loading

0 comments on commit 4527f76

Please sign in to comment.