diff --git a/backend/controller/event.controller.js b/backend/controller/event.controller.js index 57bb59eb..12b9b3a6 100644 --- a/backend/controller/event.controller.js +++ b/backend/controller/event.controller.js @@ -1,32 +1,51 @@ +const logger = require("../config/logger"); const Event = require("../models/events.model"); // Create a new event const createEvent = async (req, res) => { try { - const event = req.body; + const { title, description, date, time, age, image } = req.body; + + // Input validation + if (!title || !description || !date || !time) { + return res.status(400).json({ message: "Missing required fields" }); + } const newEvent = new Event({ - title: event.title, - description: event.description, - date: event.date, - time: event.time, - age: event.age, - image: event.image, + title, + description, + date, + time, + age, + image, }); - await newEvent.save(); - res.status(201).json(newEvent); + const savedEvent = await newEvent.save(); + res.status(201).json(savedEvent); } catch (error) { - res.status(400).json({ message: error.message }); + logger.error("Error creating event:", error); + if (error.name === "ValidationError") { + res + .status(400) + .json({ message: "Invalid input data", details: error.errors }); + } else { + res.status(500).json({ message: "Internal server error" }); + } } }; const getEvents = async (req, res) => { try { const events = await Event.find(); + + if (events.length === 0) { + return res.status(204).send(); // No Content + } + res.status(200).json(events); } catch (error) { - res.status(404).json({ message: error.message }); + console.error("Error retrieving events:", error); + res.status(500).json({ message: "Internal server error" }); } }; diff --git a/backend/models/events.model.js b/backend/models/events.model.js index 7899f88e..a4a66de0 100644 --- a/backend/models/events.model.js +++ b/backend/models/events.model.js @@ -7,29 +7,38 @@ const eventSchema = new mongoose.Schema({ title: { type: String, required: true, + trim: true, }, description: { type: String, required: true, }, date: { - type: String, + type: Date, required: true, }, time: { - type: String, + type: Date, required: true, }, - age: { + ageRange: { type: String, required: true, + enum: ["0-12", "13-17", "18+", "All Ages"], }, image: { type: String, required: true, + validate: { + validator: function (v) { + return /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test( + v + ); + }, + message: (props) => `${props.value} is not a valid URL!`, + }, }, }); - // Create the Event model const Event = mongoose.model("Event", eventSchema); diff --git a/backend/routes/eventRouter.js b/backend/routes/eventRouter.js index 89a5a1a0..d85c21b2 100644 --- a/backend/routes/eventRouter.js +++ b/backend/routes/eventRouter.js @@ -4,7 +4,23 @@ const { createEvent, getEvents } = require("../controller/event.controller"); const router = express.Router(); +router.get("/", async (req, res) => { + try { + res.json({ + message: "Welcome to the event API!", + version: "1.0.0", + endpoints: { + CreateEvent: "/event/create", + GetEvents: "/event/all", + }, + documentation: "https://api-docs-url.com", + }); + } catch (error) { + logger.error("Error in /event route:", error); // Log the error + res.status(500).json({ error: "Internal server error" }); + } +}); router.post("/create", createEvent); -router.get("/", getEvents); +router.get("/all", getEvents); module.exports = router; diff --git a/backend/routes/index.js b/backend/routes/index.js index a257bfda..dedff8e7 100644 --- a/backend/routes/index.js +++ b/backend/routes/index.js @@ -20,7 +20,7 @@ try { eventRouter = require("./eventRouter"); } catch (error) { logger.error("Error loading eventRouter:", error); // Log the error with Winston - feedbackRouter = (req, res) => { + eventRouter = (req, res) => { res .status(500) .json({ error: "Event functionality is currently unavailable" }); diff --git a/frontend/src/components/Pages/Event.jsx b/frontend/src/components/Pages/Event.jsx index 0bc0b145..25b674d7 100644 --- a/frontend/src/components/Pages/Event.jsx +++ b/frontend/src/components/Pages/Event.jsx @@ -30,22 +30,27 @@ const months = [ ]; export default function Event() { const [events, setEvents] = useState([]); - + const [error, setError] = useState(null); useEffect(() => { const fetchData = async () => { try { - const response = await fetch("http://localhost:3000/api/event", { - method: "GET", - headers: { - "Content-Type": "application/json", - }, - }); + const response = await fetch( + `${import.meta.env.VITE_BACKEND_URL}/api/event/all`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + } + ); const data = await response.json(); console.log(data); setEvents(data); + console.log(events); // You can update the state with the fetched data here } catch (error) { + setError(error); console.error("Error fetching events:", error); } }; @@ -84,7 +89,11 @@ export default function Event() { ease: "power1.inOut", delay: 1, }); - }); + return () => { + tl.kill(); + splitText.revert(); + }; + }, []); useEffect(() => { new Splide(".splide", { @@ -141,7 +150,7 @@ export default function Event() { {Array(firstDayOfMonth) .fill(null) .map((_, i) => ( -
+ ))} {dates.map((day) => ({error}
} {events.map((event) => (