Skip to content

Commit

Permalink
get and post event request endpoints added. Cannot test without Event…
Browse files Browse the repository at this point in the history
…s apis
  • Loading branch information
Eric-Fithian committed Nov 14, 2023
1 parent 116931d commit 6562a1a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
88 changes: 75 additions & 13 deletions backend/controllers/VendorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const bcrypt = require('bcryptjs');

const getVendor = async (req, res, next) => {
try {
const data = await db.oneOrNone(
'SELECT * FROM Vendors WHERE email = $1',
[req.body.email],
);
const data = await db.oneOrNone('SELECT * FROM Vendors WHERE email = $1', [
req.body.email,
]);

if (data) {
res.locals.data = data;
Expand Down Expand Up @@ -92,13 +91,7 @@ const getVendorById = async (req, res, next) => {
// Registers the vendor in the database
const createVendor = async (req, res, next) => {
// Get the values from the request body
const {
name,
email,
phone_number,
password,
website,
} = req.body;
const {name, email, phone_number, password, website} = req.body;

// Checks if the required fields are present
if (!password || !email || !name) {
Expand Down Expand Up @@ -132,7 +125,9 @@ const createVendor = async (req, res, next) => {
} catch (err) {
// Duplicate emails are not allowed
if (err.code === '23505') {
res.status(400).json({error: 'A vendor with that email already exists'});
res
.status(400)
.json({error: 'A vendor with that email already exists'});
return;
}

Expand All @@ -145,4 +140,71 @@ const createVendor = async (req, res, next) => {
next();
};

module.exports = {getVendor, getVendors, createVendor, getVendorById, authenticateVendor};
const createEventRequest = async (req, res, next) => {
const {vendorId, eventId} = req.body;

try {
await db.none(
'INSERT INTO EventRequests (vendor_id, event_id) VALUES ($1, $2)',
[vendorId, eventId],
);
next();
} catch (err) {
console.error(err);
res.status(500).json({error: 'Internal Server Error'});
}
};

const getEventRequest = async (req, res, next) => {
const {requestId, vendorId, eventId} = req.body;
if (requestId) {
try {
const eventRequest = db.oneOrNone(
'SELECT * FROM Event_Requests WHERE request_id = $1',
[requestIdId],
);
if (eventRequest) {
// Store the vendor data in res.locals.data for the middleware
res.locals.data = eventRequest;
next(); // Pass control to the next middleware
} else {
res.status(404).json({message: 'Event Request not found'});
}
} catch (err) {
console.error(err);
res.status(500).json({error: 'Internal Server Error'});
}
}
else if (vendorId && eventId) {
try {
const eventRequest = db.oneOrNone(
'SELECT * FROM Event_Requests WHERE vendor_id = $1 AND event_id = $2',
[vendorId, eventId],
);
if (eventRequest) {
// Store the vendor data in res.locals.data for the middleware
res.locals.data = eventRequest;
next(); // Pass control to the next middleware
} else {
res.status(404).json({message: 'Event Request not found'});
}
} catch (err) {
console.error(err);
res.status(500).json({error: 'Internal Server Error'});
}
}
else {
res.status(400).json({error: 'Missing required fields'});
}
};


module.exports = {
getVendor,
getVendors,
createVendor,
getVendorById,
authenticateVendor,
createEventRequest,
getEventRequest,
};
8 changes: 8 additions & 0 deletions backend/routes/VendorRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {
createVendor,
getVendorById,
authenticateVendor,
createEventRequest,
getEventRequest,
} = require('../controllers/VendorController');
const sendSuccessResponse = require('../middleware/successResponse');

Expand All @@ -23,4 +25,10 @@ router.post('/', createVendor, (req, res) => {
res.status(200).json({status: 'success'});
});

// Create Vendor event request
router.post('/events/request', createEventRequest, sendSuccessResponse);

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a database access
, but is not rate-limited.

// Get Vendor event request
router.get('/events/request', getEventRequest, sendSuccessResponse);

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a database access
, but is not rate-limited.
This route handler performs
a database access
, but is not rate-limited.

module.exports = router;
7 changes: 4 additions & 3 deletions sql-scripts/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ CREATE TABLE Events (
name VARCHAR(100) NOT NULL,
location VARCHAR(255),
datetime TIMESTAMP NOT NULL,
description TEXT
description TEXT,
vendor_capacity INT NOT NULL
);

-- Violations table
Expand All @@ -43,11 +44,11 @@ CREATE TABLE VendorViolations (
);

-- Event_Requests table
CREATE TABLE Event_Requests (
CREATE TABLE EventRequests (
request_id SERIAL PRIMARY KEY,
vendor_id INT REFERENCES Vendors(vendor_id),
event_id INT REFERENCES Events(event_id),
approved BOOLEAN DEFAULT FALSE,
approved BOOLEAN DEFAULT NULL,
requested_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
approved_at TIMESTAMP
);
Expand Down

0 comments on commit 6562a1a

Please sign in to comment.