diff --git a/config/api.yaml b/config/api.yaml index 5b7ad4a..842380b 100644 --- a/config/api.yaml +++ b/config/api.yaml @@ -397,3 +397,26 @@ paths: description: Event created default: description: Internal Server Error + + /event: + get: + tags: + - Get Data API + security : + - ApiTokens: [] + - ApiKey: [] + summary: Returns event details or all events + parameters: + - in: query + name: slug + required: false + schema: + type: string + description : Optional. If provided, fetches a specific event by its slug. + responses: + '200': + description: Success response with event details or all events. + '404': + description: Event Not Found. + default: + description: Internal Server Error. \ No newline at end of file diff --git a/routes/events/Event.js b/routes/events/Event.js index 9506dfb..f03c5eb 100644 --- a/routes/events/Event.js +++ b/routes/events/Event.js @@ -4,6 +4,28 @@ const Event = require("../../schema/events/EventSchema"); const { STATUSCODE, STATUSMESSAGE } = require("../../utils/Status"); const { setTime } = require("../../utils/SetTime"); +router.get("/", async (req, res) => { + try { + if (req.query.slug) { + const eventDetails = await Event.findOne({ slug: req.query.slug }); + + if (eventDetails) { + return res.status(STATUSCODE.OK).json(eventDetails); + } + return res + .status(STATUSCODE.NOT_FOUND) + .json({ message: STATUSMESSAGE.NOT_FOUND }); + } + + const allEvents = await Event.find({}); + res.status(STATUSCODE.OK).json(allEvents); + } catch (error) { + return res + .status(STATUSCODE.INTERNAL_SERVER_ERROR) + .json({ message: STATUSMESSAGE.INTERNAL_SERVER_ERROR }); + } +}); + router.post("/create", async (req, res) => { try { const { slug, ...data } = req.body; diff --git a/utils/Status.js b/utils/Status.js index fc2d29a..98d091d 100644 --- a/utils/Status.js +++ b/utils/Status.js @@ -39,7 +39,7 @@ const STATUSMESSAGE = { UNAUTHORIZED: "Unauthorized !", FORBIDDEN: "Forbidden !", NOT_FOUND: "Not found !", - EVENT_SLUG_ALREADY_EXISTS: "Event slug already exists", + EVENT_NOT_FOUND: "Event not found !", CREATE_EVENT_FAILED: "Failed to create event", PRODUCT_SLUG_ALREADY_EXISTS: "productSlug already exists", PRODUCT_ADD_FAILED: "Failed to add product",