Skip to content
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
16 changes: 16 additions & 0 deletions backend/models/Task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require('mongoose');

const TaskSchema = new mongoose.Schema(
{
title: { type: String, required: true, trim: true },
description: { type: String, default: '' },
status: { type: String, enum: ['pending', 'completed'], default: 'pending' },
deadline: { type: Date },
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
},
{ timestamps: true }
);

module.exports = mongoose.model('Task', TaskSchema);


87 changes: 87 additions & 0 deletions backend/routes/tasks.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const express = require('express');
const router = express.Router();
const auth = require('../middleware/auth');
const Task = require('../models/Task');

// POST /api/tasks - Create a new task
router.post('/', auth, async (req, res) => {
try {
const { title, description, status, deadline } = req.body;
if (!title || !title.trim()) {
return res.status(400).json({ errors: [{ msg: 'Title is required' }] });
}
const task = new Task({
title: title.trim(),
description: description || '',
status: status === 'completed' ? 'completed' : 'pending',
deadline: deadline ? new Date(deadline) : undefined,
userId: req.user.id,
});
const saved = await task.save();
return res.status(201).json(saved);
} catch (err) {
console.error('Create task error:', err);
return res.status(500).json({ errors: [{ msg: 'Server error' }] });
}
});

// GET /api/tasks - Get tasks for the authenticated user
router.get('/', auth, async (req, res) => {
try {
const tasks = await Task.find({ userId: req.user.id }).sort({ createdAt: -1 });
return res.json(tasks);
} catch (err) {
console.error('Fetch tasks error:', err);
return res.status(500).json({ errors: [{ msg: 'Server error' }] });
}
});

// PUT /api/tasks/:id - Update a task (only owner)
router.put('/:id', auth, async (req, res) => {
try {
const { id } = req.params;
const updates = {};
if (typeof req.body.title === 'string') updates.title = req.body.title.trim();
if (typeof req.body.description === 'string') updates.description = req.body.description;
if (typeof req.body.status === 'string' && ['pending','completed'].includes(req.body.status)) updates.status = req.body.status;
if (typeof req.body.deadline !== 'undefined') updates.deadline = req.body.deadline ? new Date(req.body.deadline) : null;
// Never allow changing userId via API

const task = await Task.findOne({ _id: id, userId: req.user.id });
if (!task) {
return res.status(404).json({ errors: [{ msg: 'Task not found' }] });
}

Object.assign(task, updates);
const saved = await task.save();
return res.json(saved);
} catch (err) {
console.error('Update task error:', err);
if (err.name === 'CastError') {
return res.status(400).json({ errors: [{ msg: 'Invalid task id' }] });
}
return res.status(500).json({ errors: [{ msg: 'Server error' }] });
}
});

// DELETE /api/tasks/:id - Delete a task (only owner)
router.delete('/:id', auth, async (req, res) => {
try {
const { id } = req.params;
const task = await Task.findOneAndDelete({ _id: id, userId: req.user.id });
if (!task) {
return res.status(404).json({ errors: [{ msg: 'Task not found' }] });
}
return res.json({ msg: 'Task deleted' });
} catch (err) {
console.error('Delete task error:', err);
if (err.name === 'CastError') {
return res.status(400).json({ errors: [{ msg: 'Invalid task id' }] });
}
return res.status(500).json({ errors: [{ msg: 'Server error' }] });
}
});

module.exports = router;


1 change: 1 addition & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ app.use("/api/github", githubRoute);
app.use("/api/auth", authMiddleware, require("./routes/auth"));
app.use("/api/profile", generalMiddleware, require("./routes/profile"));
app.use("/api/contact", generalMiddleware, contactRouter);
app.use("/api/tasks", require("./routes/tasks.route"));


// Default route
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import ProtectedRoute from "./Components/auth/ProtectedRoute";
import Dashboard from "./Components/Dashboard";
import FAQ from "./Components/FAQ";
import Pomodoro from "./Components/DashBoard/Pomodoro";
import Todo from "./Components/DashBoard/Todo";
import { ArrowUp } from "lucide-react";
import GitHubProfile from "./Components/GitHubProfile";
import LeetCode from "./Components/DashBoard/LeetCode";
Expand Down Expand Up @@ -131,6 +132,7 @@ function App() {
/>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/pomodoro" element={<Pomodoro />} />
<Route path="/todo" element={<Todo />} />
<Route path="/contributors" element={<AllContributors />} />
<Route path="/dashboard/github/:username" element={<GitHubProfile />} />
<Route path="/leetcode/:leetUser" element={<LeetCode />} />
Expand Down
Loading
Loading