|
| 1 | +import db from '../models/index.js'; |
| 2 | + |
| 3 | +const { events: Event } = db; |
| 4 | + |
| 5 | +export const addEvent = (req, res) => { |
| 6 | + try { |
| 7 | + const event = new Event({ ...req.body, userId: req.userId, eventStatus: 11 }); |
| 8 | + event.save(); |
| 9 | + res.status(200).send({ message: 'Event added successfully' }); |
| 10 | + } catch (err) { |
| 11 | + res.status(500).send({ message: err.message }); |
| 12 | + } |
| 13 | +}; |
| 14 | + |
| 15 | +export const getEvent = async (req, res) => { |
| 16 | + try { |
| 17 | + const {userId} = req; |
| 18 | + const events = await Event.findAll({ userId }); |
| 19 | + res.status(200).send({ message: 'Request completed', event: events }); |
| 20 | + } catch (err) { |
| 21 | + res.status(500).send({ message: err.message }); |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +export const getEventByid = async (req, res) => { |
| 26 | + try { |
| 27 | + const {userId} = req; |
| 28 | + const {eventId} = req.query; |
| 29 | + // Check if userId and eventId are provided |
| 30 | + if (!eventId) { |
| 31 | + throw new Error('eventId is required'); |
| 32 | + } |
| 33 | + |
| 34 | + const events = await Event.findOne({ where: { userId, eventId } }); |
| 35 | + |
| 36 | + if (!events || userId !== events.userId) { |
| 37 | + throw new Error('Data not found'); |
| 38 | + } |
| 39 | + |
| 40 | + res.status(200).send({ message: 'Request completed', event: events }); |
| 41 | + } catch (err) { |
| 42 | + res.status(500).send({ message: err.message }); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +export const updateStatus = async (req, res) => { |
| 47 | + try { |
| 48 | + const {userId} = req; |
| 49 | + const {eventId} = req.body; |
| 50 | + const {status} = req.body; |
| 51 | + // Check if userId and eventId are provided |
| 52 | + if (!eventId) { |
| 53 | + throw new Error('eventId is required'); |
| 54 | + } |
| 55 | + const events = await Event.update( |
| 56 | + { eventStatus: status }, |
| 57 | + { where: { userId, eventId } } |
| 58 | + ); |
| 59 | + |
| 60 | + if (!events || userId !== events.userId) { |
| 61 | + throw new Error('Data not found'); |
| 62 | + } |
| 63 | + |
| 64 | + res.status(200).send({ message: 'Request completed' }); |
| 65 | + } catch (err) { |
| 66 | + res.status(500).send({ message: err.message }); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +export const deleteById = async (req, res) => { |
| 71 | + try { |
| 72 | + const {userId} = req; |
| 73 | + const {eventId} = req.query; |
| 74 | + // Check if userId and eventId are provided |
| 75 | + if (!eventId) { |
| 76 | + throw new Error('eventId is required'); |
| 77 | + } |
| 78 | + const events = await Event.destroy({ where: { userId, eventId } }); |
| 79 | + |
| 80 | + if (!events || userId !== events.userId) { |
| 81 | + throw new Error('Data not found'); |
| 82 | + } |
| 83 | + |
| 84 | + res.status(200).send({ message: 'Request completed' }); |
| 85 | + } catch (err) { |
| 86 | + res.status(500).send({ message: err.message }); |
| 87 | + } |
| 88 | +}; |
0 commit comments