feat: Initial commit of backend services and AGENTS.md
This commit is contained in:
66
backend/routes/events.js
Normal file
66
backend/routes/events.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const express = require("express");
|
||||
const Event = require("../models/Event");
|
||||
const auth = require("../middleware/auth");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Get all events
|
||||
router.get("/", async (req, res) => {
|
||||
try {
|
||||
const events = await Event.find();
|
||||
res.json(events);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
|
||||
// Create an event
|
||||
router.post("/", auth, async (req, res) => {
|
||||
const { title, description, date, location } = req.body;
|
||||
|
||||
try {
|
||||
const newEvent = new Event({
|
||||
title,
|
||||
description,
|
||||
date,
|
||||
location,
|
||||
});
|
||||
|
||||
const event = await newEvent.save();
|
||||
res.json(event);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
|
||||
// RSVP to an event
|
||||
router.put("/rsvp/:id", auth, async (req, res) => {
|
||||
try {
|
||||
const event = await Event.findById(req.params.id);
|
||||
if (!event) {
|
||||
return res.status(404).json({ msg: "Event not found" });
|
||||
}
|
||||
|
||||
// Check if the user has already RSVPed
|
||||
if (
|
||||
event.participants.filter(
|
||||
(participant) => participant.toString() === req.user.id,
|
||||
).length > 0
|
||||
) {
|
||||
return res.status(400).json({ msg: "Already RSVPed" });
|
||||
}
|
||||
|
||||
event.participants.unshift(req.user.id);
|
||||
|
||||
await event.save();
|
||||
|
||||
res.json(event.participants);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
res.status(500).send("Server error");
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user