feat: Initial commit of backend services and AGENTS.md
This commit is contained in:
56
backend/routes/tasks.js
Normal file
56
backend/routes/tasks.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const express = require("express");
|
||||
const Task = require("../models/Task");
|
||||
const auth = require("../middleware/auth");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Get all tasks for user
|
||||
router.get("/", auth, async (req, res) => {
|
||||
try {
|
||||
const tasks = await Task.find({ completedBy: req.user.id });
|
||||
res.json(tasks);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
|
||||
// Create a task
|
||||
router.post("/", auth, async (req, res) => {
|
||||
const { street, description } = req.body;
|
||||
|
||||
try {
|
||||
const newTask = new Task({
|
||||
street,
|
||||
description,
|
||||
});
|
||||
|
||||
const task = await newTask.save();
|
||||
res.json(task);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
|
||||
// Complete a task
|
||||
router.put("/:id", auth, async (req, res) => {
|
||||
try {
|
||||
const task = await Task.findById(req.params.id);
|
||||
if (!task) {
|
||||
return res.status(404).json({ msg: "Task not found" });
|
||||
}
|
||||
|
||||
task.completedBy = req.user.id;
|
||||
task.status = "completed";
|
||||
|
||||
await task.save();
|
||||
|
||||
res.json(task);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user