|
|
|
|
@@ -41,124 +41,141 @@ taskSchema.index({ dateCreated: 1 })
|
|
|
|
|
|
|
|
|
|
const Task = mongoose.model("Task", taskSchema);
|
|
|
|
|
|
|
|
|
|
// this api route gets all the tasks from our database.
|
|
|
|
|
// if the request included a query string, use that for our sorting.
|
|
|
|
|
app.get('/api/tasks', async (req, res) => {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const { sortBy } = req.query; // ?sortBy=dueDate or ?sortBy=dateCreated
|
|
|
|
|
let sortOption = {};
|
|
|
|
|
const { sortBy } = req.query; // destructs the sortBy from the query string. e.g. ?sortBy=dueDate or ?sortBy=dateCreated
|
|
|
|
|
let sortOption = {}; // create an empty sort object for the database query
|
|
|
|
|
|
|
|
|
|
if (sortBy === 'dueDate') {
|
|
|
|
|
sortOption = { dueDate: 1 }; // Ascending
|
|
|
|
|
sortOption = { dueDate: 1 }; // if the sortBy query string is dueDate, set the sortOption to dueDate and set the order to ascending (1)
|
|
|
|
|
} else if (sortBy === 'dateCreated') {
|
|
|
|
|
sortOption = { dateCreated: 1 };
|
|
|
|
|
sortOption = { dateCreated: 1 }; // if the sortBy query string is dateCreated, set the sortOption to dateCreated and set the order to ascending (1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tasks = await Task.find({}).sort(sortOption);
|
|
|
|
|
const tasks = await Task.find({}).sort(sortOption); // fetch all task documents and sort them using sortOption
|
|
|
|
|
|
|
|
|
|
if (!tasks) {
|
|
|
|
|
// return the following if the query failed to return a value
|
|
|
|
|
return res.status(404).json({ message: "Tasks not found!" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json(tasks);
|
|
|
|
|
res.json(tasks); // send a json response with the task objects
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// if there are any errors, do the following
|
|
|
|
|
console.error("Error:", error);
|
|
|
|
|
res.status(500).json({ message: "Error grabbing tasks!" });
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this api route takes post requests to save a new MongoDB document, aka, our task
|
|
|
|
|
app.post('/api/tasks/todo', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { title, description, dueDate } = req.body;
|
|
|
|
|
const taskData = { title, description, dueDate };
|
|
|
|
|
const createTask = new Task(taskData);
|
|
|
|
|
const newTask = await createTask.save();
|
|
|
|
|
const { title, description, dueDate } = req.body; //destructures the request body
|
|
|
|
|
const taskData = { title, description, dueDate }; // creating the object to be used for creating the task
|
|
|
|
|
const createTask = new Task(taskData); // instantiates a new Task document/model instance using the taskData object values
|
|
|
|
|
const newTask = await createTask.save(); // this saves the new document to MongoDB using the Task model/schema.
|
|
|
|
|
|
|
|
|
|
res.json({ task: newTask, message: "New task created successfully!" });
|
|
|
|
|
res.json({ task: newTask, message: "New task created successfully!" }); // if there were no error above, send a response where the body is an JSON object.
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// if there are any errors, do the following
|
|
|
|
|
console.error("Error:", error);
|
|
|
|
|
res.status(500).json({ message: "Error creating the task!" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// To 'complete' the task and move columns ↓
|
|
|
|
|
// this api route marks a task as complete using its id.
|
|
|
|
|
|
|
|
|
|
app.patch('/api/tasks/complete/:id', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { completed } = req.body;
|
|
|
|
|
const taskId = req.params.id;
|
|
|
|
|
const completedTask = await Task.findByIdAndUpdate(taskId, { completed }, { new: true });
|
|
|
|
|
const { completed } = req.body; // expected boolean value coming from the frontend
|
|
|
|
|
const taskId = req.params.id; // grab the task id from the URL params
|
|
|
|
|
const completedTask = await Task.findByIdAndUpdate(taskId, { completed }, { new: true }); // update completed status and return updated document
|
|
|
|
|
|
|
|
|
|
if (!completedTask) {
|
|
|
|
|
// return 404 if no task matches the provided id
|
|
|
|
|
return res.status(404).json({ message: "Task not found!" });
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// return updated task document and confirmation message
|
|
|
|
|
res.json({ task: completedTask, message: "Task set to 'Complete'" });
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// return 500 if database update fails
|
|
|
|
|
console.error("Error:", error);
|
|
|
|
|
res.status(500).json({ message: "Error completing the task!" });
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// To make the task 'not complete' and move columns ↓
|
|
|
|
|
// this api route marks a task as not complete using its id.
|
|
|
|
|
|
|
|
|
|
app.patch('/api/tasks/notComplete/:id', async (req, res) => {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const { completed } = req.body;
|
|
|
|
|
const taskId = req.params.id;
|
|
|
|
|
const taskNotComplete = await Task.findByIdAndUpdate(taskId, { completed }, { new: true });
|
|
|
|
|
const { completed } = req.body; // expected boolean value coming from the frontend
|
|
|
|
|
const taskId = req.params.id; // grab the task id from the URL params
|
|
|
|
|
const taskNotComplete = await Task.findByIdAndUpdate(taskId, { completed }, { new: true }); // update completed status and return updated document
|
|
|
|
|
|
|
|
|
|
if (!taskNotComplete) {
|
|
|
|
|
// return 404 if no task matches the provided id
|
|
|
|
|
return res.status(404).json({ message: "Task not found!" });
|
|
|
|
|
}
|
|
|
|
|
// return updated task document and confirmation message
|
|
|
|
|
res.json({ task: taskNotComplete, message: "Task set to 'Not Complete'" });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// return 500 if database update fails
|
|
|
|
|
console.error("Error:", error);
|
|
|
|
|
res.status(500).json({ message: "Error making the task NOT complete!" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// this api route deletes a task by id.
|
|
|
|
|
app.delete('/api/tasks/delete/:id', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const taskId = req.params.id;
|
|
|
|
|
const deletedTask = await Task.findByIdAndDelete(taskId);
|
|
|
|
|
const taskId = req.params.id; // grab the task id from the URL params
|
|
|
|
|
const deletedTask = await Task.findByIdAndDelete(taskId); // delete the matching task document
|
|
|
|
|
|
|
|
|
|
if (!deletedTask) {
|
|
|
|
|
// return 404 if no task matches the provided id
|
|
|
|
|
return res.status(404).json({ message: "Task not found!" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return deleted task document and confirmation message
|
|
|
|
|
res.json({ task: deletedTask, message: "Task deleted successfully!" });
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// return 500 if deletion fails
|
|
|
|
|
console.error("Error:", error);
|
|
|
|
|
res.status(500).json({ message: "Error deleting the task!" });
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// To edit exisiting task and update
|
|
|
|
|
// this api route updates a task's editable fields by id.
|
|
|
|
|
|
|
|
|
|
app.put('/api/tasks/update/:id', async (req, res) => {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const taskId = req.params.id;
|
|
|
|
|
const { title, description, dueDate } = req.body; // Extract data from front end request
|
|
|
|
|
const taskData = { title, description, dueDate };
|
|
|
|
|
const updatedTask = await Task.findByIdAndUpdate(taskId, taskData, { new: true });
|
|
|
|
|
const taskId = req.params.id; // grab the task id from the URL params
|
|
|
|
|
const { title, description, dueDate } = req.body; // extract updated fields from request body
|
|
|
|
|
const taskData = { title, description, dueDate }; // build the update object
|
|
|
|
|
const updatedTask = await Task.findByIdAndUpdate(taskId, taskData, { new: true }); // apply update and return updated document
|
|
|
|
|
|
|
|
|
|
if (!updatedTask) {
|
|
|
|
|
// return 404 if no task matches the provided id
|
|
|
|
|
return res.status(404).json({ message: "Task not found!" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return updated task document and confirmation message
|
|
|
|
|
res.json({ task: updatedTask, message: "Task updated successfully!" });
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// return 500 if update fails
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
res.status(500).json({ message: "Error updating the task!" });
|
|
|
|
|
|
|
|
|
|
|