From 6562a1aae30174de458d55a1dcd1393af97e60a7 Mon Sep 17 00:00:00 2001 From: Eric Fithian Date: Mon, 13 Nov 2023 18:40:27 -0700 Subject: [PATCH] get and post event request endpoints added. Cannot test without Events apis --- backend/controllers/VendorController.js | 88 +++++++++++++++++++++---- backend/routes/VendorRouter.js | 8 +++ sql-scripts/init.sql | 7 +- 3 files changed, 87 insertions(+), 16 deletions(-) diff --git a/backend/controllers/VendorController.js b/backend/controllers/VendorController.js index 81b93dc..d383647 100644 --- a/backend/controllers/VendorController.js +++ b/backend/controllers/VendorController.js @@ -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; @@ -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) { @@ -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; } @@ -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, +}; diff --git a/backend/routes/VendorRouter.js b/backend/routes/VendorRouter.js index c694f42..f92cb43 100644 --- a/backend/routes/VendorRouter.js +++ b/backend/routes/VendorRouter.js @@ -6,6 +6,8 @@ const { createVendor, getVendorById, authenticateVendor, + createEventRequest, + getEventRequest, } = require('../controllers/VendorController'); const sendSuccessResponse = require('../middleware/successResponse'); @@ -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); + +// Get Vendor event request +router.get('/events/request', getEventRequest, sendSuccessResponse); + module.exports = router; diff --git a/sql-scripts/init.sql b/sql-scripts/init.sql index 2f04f60..f7d4f0f 100644 --- a/sql-scripts/init.sql +++ b/sql-scripts/init.sql @@ -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 @@ -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 );