-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/samar12-rad/PlayCafe into j…
…sonwebtoken
- Loading branch information
Showing
5 changed files
with
201 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const logger = require("../config/logger"); | ||
const Event = require("../models/events.model"); | ||
|
||
// Create a new event | ||
const createEvent = async (req, res) => { | ||
try { | ||
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, | ||
description, | ||
date, | ||
time, | ||
age, | ||
image, | ||
}); | ||
|
||
const savedEvent = await newEvent.save(); | ||
res.status(201).json(savedEvent); | ||
} catch (error) { | ||
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) { | ||
logger.error("Error retrieving events:", error); | ||
res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}; | ||
|
||
module.exports = { createEvent, getEvents }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// models/Event.js | ||
|
||
const mongoose = require("mongoose"); | ||
|
||
// Define the Event schema | ||
const eventSchema = new mongoose.Schema({ | ||
title: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
}, | ||
description: { | ||
type: String, | ||
required: true, | ||
}, | ||
date: { | ||
type: Date, | ||
required: true, | ||
}, | ||
time: { | ||
type: Date, | ||
required: true, | ||
}, | ||
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); | ||
|
||
module.exports = Event; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const express = require("express"); | ||
const logger = require("../config/logger"); | ||
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("/all", getEvents); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.