Skip to content

Commit e80417a

Browse files
authored
Merge pull request #12 from vitabletech/develop
Develop
2 parents 4d2a7e8 + d9a954c commit e80417a

File tree

11 files changed

+207
-9
lines changed

11 files changed

+207
-9
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Joi from 'joi';
2+
3+
const addEventValidation = (req, res, next) => {
4+
// Define validation schema
5+
const schema = Joi.object({
6+
eventName: Joi.string().min(3).max(30).required(),
7+
eventDate: Joi.date().iso().required(),
8+
eventTime: Joi.string()
9+
.regex(/^\d{2}:\d{2}$/)
10+
.required(),
11+
eventLocation: Joi.string().required()
12+
// You can add more validation rules as needed
13+
});
14+
15+
// Validate data against schema
16+
const { error } = schema.validate(req.body);
17+
18+
if (error) {
19+
return res.status(400).send({ message: error.details[0].message });
20+
}
21+
return next();
22+
23+
};
24+
25+
const validate = {
26+
addEventValidation
27+
};
28+
29+
export default validate;

app/controllers/eventController.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
};

app/models/Event.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export default (sequelize, Sequelize) => {
2+
const Event = sequelize.define(
3+
'events',
4+
{
5+
eventId: {
6+
type: Sequelize.INTEGER,
7+
autoIncrement: true,
8+
primaryKey: true
9+
},
10+
userId: {
11+
type: Sequelize.INTEGER,
12+
allowNull: false
13+
},
14+
eventName: {
15+
type: Sequelize.STRING,
16+
allowNull: false
17+
},
18+
eventDate: {
19+
type: Sequelize.STRING,
20+
allowNull: false
21+
},
22+
eventTime: {
23+
type: Sequelize.STRING,
24+
allowNull: false
25+
},
26+
eventLocation: {
27+
type: Sequelize.STRING,
28+
allowNull: false
29+
},
30+
eventStatus: {
31+
type: Sequelize.STRING,
32+
allowNull: false,
33+
Comment: '11 => Pending, 22 => On-going, 33 => Completed, 44=> Cancelled'
34+
}
35+
},
36+
{
37+
timestamps: true
38+
}
39+
);
40+
41+
return Event;
42+
};

app/models/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import Sequelize from 'sequelize';
2-
import config from '../../config/db.config.js';
2+
import DB_CONFIG from '../../config/db.config.js';
33
import createUserModel from './User.js';
44
import createRoleModel from './Role.js';
55
import createGuestModel from './Guest.js';
6+
import createEventModel from './Event.js';
67

7-
const sequelize = new Sequelize({...config});
8+
const sequelize = new Sequelize({...DB_CONFIG});
89

910
const db = {
1011
Sequelize,
1112
sequelize,
1213
user: createUserModel(sequelize, Sequelize),
1314
role: createRoleModel(sequelize, Sequelize),
1415
guests: createGuestModel(sequelize, Sequelize),
16+
events: createEventModel(sequelize, Sequelize),
1517
ROLES: ['user', 'admin']
1618
};
1719

app/routes/Router.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import authRoutes from './auth.routes.js';
22
import userRoutes from './user.routes.js';
33
import guestRoutes from './guest.routes.js';
4+
import eventRoutes from './event.route.js';
45

56
const applyRoutes = (app) => {
67
// simple route
@@ -12,6 +13,7 @@ const applyRoutes = (app) => {
1213
authRoutes(app);
1314
userRoutes(app);
1415
guestRoutes(app);
16+
eventRoutes(app);
1517
};
1618

1719
export default applyRoutes;

app/routes/event.route.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import express from 'express';
2+
import {
3+
addEvent,
4+
getEvent,
5+
getEventByid,
6+
deleteById,
7+
updateStatus
8+
} from '../controllers/eventController.js';
9+
import authJwt from '../middleware/authJwt.js';
10+
import validate from '../Validations/addEventsValidation.js';
11+
12+
export default function guestRoutes(app) {
13+
app.use((req, res, next) => {
14+
res.header('Access-Control-Allow-Headers', 'x-access-token, Origin, Content-Type, Accept');
15+
next();
16+
});
17+
app.use(express.json());
18+
app.post('/add-event', [authJwt.verifyToken, validate.addEventValidation], addEvent);
19+
app.get('/get-event', [authJwt.verifyToken], getEvent);
20+
app.get('/get-event-by-id', [authJwt.verifyToken], getEventByid);
21+
app.get('/delete-event-by-id', [authJwt.verifyToken], deleteById);
22+
app.post('/update-status', [authJwt.verifyToken], updateStatus);
23+
}

app/routes/guest.routes.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import express from 'express'; // Import express
1+
import express from 'express';
22
import { addGuest, deleteGuest, updateGuest } from '../controllers/guestController.js'; // Import addGuest function
33

44
export default function guestRoutes(app) {
@@ -13,5 +13,4 @@ export default function guestRoutes(app) {
1313
app.post('/addguest', addGuest);
1414
app.delete('/deleteguest', deleteGuest);
1515
app.put('/updateguest',updateGuest);
16-
1716
}

app/server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import express from 'express';
2+
import { config } from 'dotenv';
23
import applyRouter from './routes/index.js';
34

5+
config();
6+
47
const app = express();
58

69
// Set up the router

config/auth.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { config } from 'dotenv';
2+
3+
config();
4+
15
export default {
26
secret: process.env.JWT_SECRET,
37
jwtOptions: {

config/db.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const config = {
1+
import { config } from 'dotenv';
2+
3+
config();
4+
5+
const DB_CONFIG = {
26
host: process.env.DB_HOST,
37
username: process.env.DB_USER,
48
password: process.env.DB_PASSWORD,
@@ -11,4 +15,4 @@ const config = {
1115
idle: parseInt(process.env.DB_POOL_IDLE, 10)
1216
}
1317
};
14-
export default config;
18+
export default DB_CONFIG;

0 commit comments

Comments
 (0)