From a7722ebd80f7539b513098bd77b5a0662f27a5d1 Mon Sep 17 00:00:00 2001 From: Akalanka Perera Date: Sun, 19 Nov 2023 14:09:14 +0000 Subject: [PATCH 1/2] Feat: added slug and guidelines to event model --- src/models/event/index.js | 2 ++ src/validations/event.js | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/models/event/index.js b/src/models/event/index.js index 538b3ac..c368923 100644 --- a/src/models/event/index.js +++ b/src/models/event/index.js @@ -18,6 +18,8 @@ const EventSchema = new mongoose.Schema( type: Number, required: true }, + slug: String, + guidelines: String, photo_urls: { 'default': String, 'sm': String, diff --git a/src/validations/event.js b/src/validations/event.js index 9dd154b..9ffd6af 100644 --- a/src/validations/event.js +++ b/src/validations/event.js @@ -1,6 +1,7 @@ import { Joi } from 'celebrate'; const optionals = { + slug: Joi.string().optional(), photo_urls: Joi.object({ 'default': Joi.string().optional(), 'sm': Joi.string().optional(), @@ -10,6 +11,7 @@ const optionals = { '2xl': Joi.string().optional() }).optional(), tags: Joi.array().items(Joi.string()).min(2).max(2), + guidelines: Joi.string().optional(), faqs: Joi.array() .items( Joi.object({ From e933c1046d691f814a4ff9a0ddb1111f9c275079 Mon Sep 17 00:00:00 2001 From: Miyuru Gunarathna Date: Mon, 20 Nov 2023 00:35:49 +0530 Subject: [PATCH 2/2] Feat: added event search by slug --- src/controllers/event/index.js | 5 +++++ src/models/event/index.js | 5 ++++- src/repository/event/index.js | 7 +++++++ src/routes/event.routes.js | 8 ++++++++ src/services/event/index.js | 6 ++++++ src/validations/event.js | 4 ++++ 6 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/controllers/event/index.js b/src/controllers/event/index.js index e3ff818..ff01a4e 100644 --- a/src/controllers/event/index.js +++ b/src/controllers/event/index.js @@ -18,6 +18,11 @@ export const getEventById = async (req, res) => { return makeResponse({ res, data: result, message: 'Event retrieved successfully' }); }; +export const getEventBySlug = async (req, res) => { + const result = await eventService.retrieveBySlug(req.params.slug, req.user); + return makeResponse({ res, data: result, message: 'Event retrieved successfully' }); +}; + export const updateEvent = async (req, res) => { const result = await eventService.update(req.params.event_id, req.body, req.user); return makeResponse({ res, data: result, message: 'Event updated successfully' }); diff --git a/src/models/event/index.js b/src/models/event/index.js index c368923..aedf5bc 100644 --- a/src/models/event/index.js +++ b/src/models/event/index.js @@ -18,7 +18,10 @@ const EventSchema = new mongoose.Schema( type: Number, required: true }, - slug: String, + slug: { + type: String, + unique: true + }, guidelines: String, photo_urls: { 'default': String, diff --git a/src/repository/event/index.js b/src/repository/event/index.js index af062f2..2a4a9da 100644 --- a/src/repository/event/index.js +++ b/src/repository/event/index.js @@ -91,6 +91,13 @@ export const findById = (id, user, filterFields = true) => { return query.exec(); }; +export const findBySlug = (slug, user, filterFields = true) => { + const baseFilters = { slug }; + let query = Event.findOne(eventFilters(user, baseFilters)).lean(); + if (filterFields) query = query.select('-creator_lock'); + return query.exec(); +}; + export const findOneAndUpdate = (filters, data) => Event.findOneAndUpdate(filters, dot(data), { new: true }); export const updateById = (id, data) => findOneAndUpdate({ _id: id }, data); diff --git a/src/routes/event.routes.js b/src/routes/event.routes.js index 3237324..0001039 100644 --- a/src/routes/event.routes.js +++ b/src/routes/event.routes.js @@ -7,6 +7,7 @@ import { deleteEvent, getAllEvents, getEventById, + getEventBySlug, getUserEventTicket, requestEventTicket, updateEvent @@ -15,6 +16,7 @@ import { adminProtect, identify, protect } from '@/middleware/auth'; import { addEventSchema, eventIdSchema, + eventSlugSchema, eventTicketIdSchema, requestTicketSchema, updateEventSchema @@ -45,6 +47,12 @@ events.delete( adminProtect, tracedAsyncHandler(deleteEvent) ); +events.get( + '/slugs/:slug', + identify, + celebrate({ [Segments.PARAMS]: eventSlugSchema }), + tracedAsyncHandler(getEventBySlug) +); events.post( '/:event_id/tickets/request', protect, diff --git a/src/services/event/index.js b/src/services/event/index.js index 54774fd..15aa489 100644 --- a/src/services/event/index.js +++ b/src/services/event/index.js @@ -13,6 +13,12 @@ export const retrieve = async (event_id, user) => { return result; }; +export const retrieveBySlug = async (slug, user) => { + const result = await eventRepository.findBySlug(slug, user); + if (!result) throw new createError(404, "Event doesn't exist or you do not have permission to access this event"); + return result; +}; + export const update = async (event_id, data, user) => { const event = await retrieve(event_id, user); if (data.name) { diff --git a/src/validations/event.js b/src/validations/event.js index 9ffd6af..8830b72 100644 --- a/src/validations/event.js +++ b/src/validations/event.js @@ -76,6 +76,10 @@ export const eventIdSchema = { event_id: Joi.string().hex().length(24).required() }; +export const eventSlugSchema = { + slug: Joi.string().required() +}; + export const eventTicketIdSchema = { ...eventIdSchema, ticket_id: Joi.string().hex().length(24).required()