From f9d69cb15ad847a7b32bdb7703d1b52c3c8af728 Mon Sep 17 00:00:00 2001 From: Pragati-006 Date: Mon, 28 Jul 2025 14:54:59 +1000 Subject: [PATCH] taskController.js file is added --- backend/controllers/taskController.js | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 backend/controllers/taskController.js diff --git a/backend/controllers/taskController.js b/backend/controllers/taskController.js new file mode 100644 index 0000000..67110ed --- /dev/null +++ b/backend/controllers/taskController.js @@ -0,0 +1,61 @@ +const Task = require('../models/Task'); + +const getTasks = async (req, res) => { + +try { + +const tasks = await Task.find({userld: req.user.id }); + +res.json(tasks); + +} catch (error) { + +res.status(500).json({ message: error.message }); +} +}; + +const addTask = async ( +req, +res) => { +const { title, description, deadline } = req.body; +try { +const task = await Task.create({ userId: req.user.id, title, description, deadline }); +res.status(201).json(task); +} catch (error) { +res.status(500).json({ message: error.message }); +} +}; + + +const updateTask = async ( +req, +res) => { +const { title, description, completed, deadline } = req.body; +try { +const task = await Task.findById(req.params.id); +if (!task) return res.status(404).json({ message: 'Task not found' }); +task.title = title || task.title; +task.description = description || task.description; +task.completed = completed ?? task.completed; +task.deadline = deadline || task.deadline; +const updatedTask = await task.save(); +res.json(updatedTask); +} catch (error) { +res.status(500).json({ message: error.message }); +} +}; + + +const deleteTask = async ( +req, +res) => { +try { +const task = await Task.findById(req.params.id); +if (!task) return res.status(404).json({ message: 'Task not found' }); +await task.remove(); +res.json({ message: 'Task deleted' }); +} catch (error) { +res.status(500).json({ message: error.message }); +} +}; +module.exports = { getTasks, addTask, updateTask, deleteTask }; \ No newline at end of file