Skip to content

Commit

Permalink
Merge pull request #81 from Blueprint-Boulder/58-create-frontend-even…
Browse files Browse the repository at this point in the history
…tservice

events integration complete
  • Loading branch information
nh602 authored Feb 7, 2024
2 parents 5eb0e93 + 8772133 commit b53b921
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 96 deletions.
7 changes: 4 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
"source.fixAll.eslint": true
},
"editor.formatOnSave":false,
"css.lint.unknownAtRules":"ignore",
"eslint.validate": ["javascript, typescript, vue, html, css, scss, json, markdown"],
"editor.formatOnSave": true,
"css.lint.unknownAtRules": "ignore"
}
39 changes: 28 additions & 11 deletions backend/controllers/EventController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ const getEventById = async (req, res, next) => {
};

const createEvent = async (req, res, next) => {
const {name, location, datetime, description, capacity} = req.body;
const {name, location, start_time, end_time, description, capacity} = req.body;
try {
const event = await db.one(`
INSERT INTO Events (name, location, datetime, description, vendor_capacity)
VALUES ($1, $2, $3, $4, $5)
INSERT INTO Events (name, location, start_time, end_time, description, vendor_capacity)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;
`, [name, location, datetime, description, capacity]);
`, [name, location, start_time, end_time, description, capacity]);

// Returns the created event
res.locals.data = event;
Expand All @@ -55,19 +55,20 @@ const createEvent = async (req, res, next) => {

const updateEvent = async (req, res, next) => {
const {event_id} = req.params;
const {name, location, datetime, description, capacity} = req.body;
const {name, location, start_time, end_time, description, capacity} = req.body;
try {
const result = await db.one(`
UPDATE Events
SET
name = $1,
location = $2,
datetime = $3,
description = $4,
vendor_capacity = $5
WHERE event_id = $6
start_time = $3,
end_time = $4,
description = $5,
vendor_capacity = $6
WHERE event_id = $7
RETURNING *;
`, [name, location, datetime, description, capacity, event_id]);
`, [name, location, start_time, end_time, description, capacity, event_id]);

if (!result) {
return res.status(404).json({error: 'Event not found'});
Expand All @@ -81,4 +82,20 @@ const updateEvent = async (req, res, next) => {
}
};

module.exports = {getAllEvents, getEventById, createEvent, updateEvent};
const deleteEvent = async (req, res, next) => {
const {event_id} = req.params;
try {
const result = await db.oneOrNone('DELETE FROM Events WHERE event_id = $1 RETURNING *;', [event_id]);
if (!result) {
return res.status(404).json({error: 'Event not found'});
} else {
res.locals.data = result;
next();
}
} catch (error) {
console.error('Error deleting event:', error);
return res.status(500).json({error: 'Internal Server Error'});
}
}

module.exports = {getAllEvents, getEventById, createEvent, updateEvent, deleteEvent};
9 changes: 5 additions & 4 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const app = express();
app.use(express.json());

const errorHandler = require('errorhandler');
app.use(errorHandler({dumbExceptions: true, showStack: true}));
app.use(errorHandler({ dumbExceptions: true, showStack: true }));

// Allows cross origin requests
const cors = require('cors');
Expand All @@ -29,9 +29,10 @@ const VendorRouter = require('./routes/VendorRouter');
const AdminRouter = require('./routes/AdminRouter');
const EventRouter = require('./routes/EventRouter');

app.use('/vendors', VendorRouter);
app.use('/events', EventRouter);
app.use('/admins', AdminRouter);
// Routes overlapped with the frontend
app.use('/api/vendors', VendorRouter);
app.use('/api/events', EventRouter);
app.use('/api/admins', AdminRouter);

app.get('/', (req, res) => {
res.status(202).send('Hello World!');
Expand Down
5 changes: 5 additions & 0 deletions backend/routes/AdminRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ router.post('/login', getAdminByEmail, signAdminToken, (req, res) => {
// res.status(200).json({status: 'success', admin: res.locals.data});
// });

// DELETE ROUTE IN PRODUCTION -- CREATE AN ADMIN ACCOUNT FOR TESTING PURPOSES
router.post('/', createAdminMiddleware, (req, res) => {
res.status(200).json({status: 'success'});
});

module.exports = router;
3 changes: 3 additions & 0 deletions backend/routes/EventRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
getEventById,
createEvent,
updateEvent,
deleteEvent,
} = require('../controllers/EventController');
const sendSuccessResponse = require('../middleware/successResponse');
const {verify} = require('../controllers/AuthController');
Expand All @@ -21,4 +22,6 @@ router.post('/', verify('admin'), createEvent, sendSuccessResponse);
// Update an existing event
router.put('/:event_id', verify('admin'), updateEvent, sendSuccessResponse);

router.delete('/:event_id', verify('admin'), deleteEvent, sendSuccessResponse);

module.exports = router;
1 change: 1 addition & 0 deletions frontend/src/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const config = {
environment: 'dev',
baseUrl: 'http://localhost:3001/api',
};

export default config;
56 changes: 36 additions & 20 deletions frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,91 @@ import reportWebVitals from './reportWebVitals';
import Vendors from './routes/vendors.jsx';
import ErrorPage from './components/error.jsx';
import './App.css';
import {createBrowserRouter, Navigate, RouterProvider} from 'react-router-dom';
import { createBrowserRouter, Navigate, RouterProvider } from 'react-router-dom';
import Register from './routes/register';
import MockEventService from './services/MockServices/MockEventService.js';
import MockVendorService from './services/MockServices/MockVendorService.js';
import ResetPassword from './routes/reset_password';
import {handleLoginVendor} from './services/handleLogin.js';
import {handleRegister} from './services/handleRegister.js';
import { handleLoginVendor } from './services/handleLogin.js';
import { handleRegister } from './services/handleRegister.js';
import EventsService from './services/Events/EventsService.js';

import config from './config.js';
import {MessageProvider} from './context.jsx';
import { MessageProvider } from './context.jsx';

const isadmin = true;
const session = true;

let eventService;
let vendorService;

// Setup the mock vendor service
if (config.environment === 'dev') {
if (config.environment == 'dev') {
MockVendorService.init();
MockEventService.init();

eventService = MockEventService;
vendorService = MockVendorService;
} else if (config.environment == 'prod') {
// Load base url for the backend
const baseUrl = config.baseUrl;

// Initilize Services
const eventsService = new EventsService(baseUrl);

eventService = eventsService;
vendorService = vendorService;
}

const router = createBrowserRouter([
{
path: '/',
// loader: loaderfunc(),
element: <Root admin = {isadmin}/>,
element: <Root admin={isadmin} />,
children: [
{
path: '/vendors/:vendorId',
element: session? <Vendor VendorService={MockVendorService}/> : <Navigate to="/login" />,
element: session ? <Vendor VendorService={vendorService} /> : <Navigate to="/login" />,
},
{
path: '/login',
element: <Login loginService = {handleLoginVendor} admin={isadmin}/>,
element: <Login loginService={handleLoginVendor} admin={isadmin} />,
},
{
path: '/register',
element: <Register registerService = {handleRegister}/>,
element: <Register registerService={handleRegister} />,
},
{
path: '/reset_password',
element: <ResetPassword/>,
element: <ResetPassword />,
},
{
path: '/profile',
element: session ? <Profile VendorService={MockVendorService}/> : <Navigate to="/login" />,
element: session ? <Profile VendorService={vendorService} /> : <Navigate to="/login" />,
},
{
path: '/events/:eventId',
element: session ? <Event EventService = {MockEventService} /> : <Navigate to="/login" />,
element: session ? <Event eventsService={eventService} /> : <Navigate to="/login" />,
},
{
path: '/events',
element: session ? <Events EventService={MockEventService}/> : <Navigate to="/login" />,
element: session ? <Events eventsService={eventService} /> : <Navigate to="/login" />,
},
{
path: '/vendors',
element: session && isadmin? <Vendors VendorService={MockVendorService}/>: <Navigate to="/login" />,
element: session && isadmin ? <Vendors VendorService={vendorService} /> : <Navigate to="/login" />,
}],
errorElement: <ErrorPage admin = {isadmin} />,
errorElement: <ErrorPage admin={isadmin} />,
},
]);

const root = ReactDOM.createRoot(document.getElementById('root'));
export default root.render(
<React.StrictMode>
<MessageProvider>
<RouterProvider router={router} />
</MessageProvider>
</React.StrictMode>,
<React.StrictMode>
<MessageProvider>
<RouterProvider router={router} />
</MessageProvider>
</React.StrictMode>,
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
Expand Down
10 changes: 3 additions & 7 deletions frontend/src/objects/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
Storage mechanism for a vendor object
*/
class Event {
constructor(eventId, name, location, datetime, description, vendorCapacity, durationHours = 2) {
constructor(eventId, name, location, startTime, endTime, description, vendorCapacity) {
this.eventId = eventId;
this.name = name;
this.location = location;
this.date = datetime.toLocaleDateString();
this.startTime = datetime.toLocaleTimeString();

// Create a new Date object for the end time
const endTime = new Date(datetime.getTime() + durationHours * 60 * 60 * 1000);
this.date = startTime.toLocaleDateString();
this.startTime = startTime.toLocaleTimeString();
this.endTime = endTime.toLocaleTimeString();

this.description = description;
this.vendorCapacity = vendorCapacity;
}
Expand Down
Loading

0 comments on commit b53b921

Please sign in to comment.