diff --git a/client/src/app/api/apiConstants.js b/client/src/app/api/apiConstants.js index 33a92277f..ba1a35c67 100644 --- a/client/src/app/api/apiConstants.js +++ b/client/src/app/api/apiConstants.js @@ -10,3 +10,4 @@ export const serverURL = calculateServer(); export const eventURL = serverURL + EVENT_TABLE; export const eventsURL = serverURL + EVENTS_TABLE; export const playbackURL = serverURL + 'playback'; +export const ontimeURL = serverURL + 'ontime'; diff --git a/client/src/app/api/eventsApi.js b/client/src/app/api/eventsApi.js index a185a54be..17a2ada40 100644 --- a/client/src/app/api/eventsApi.js +++ b/client/src/app/api/eventsApi.js @@ -1,21 +1,6 @@ import axios from 'axios'; import { eventsURL } from '../api/apiConstants'; -export const downloadEvents = async () => { - await axios({ - url: eventsURL + '/download', - method: 'GET', - responseType: 'blob', // important - }).then((response) => { - const url = window.URL.createObjectURL(new Blob([response.data])); - const link = document.createElement('a'); - link.href = url; - link.setAttribute('download', 'events.json'); - document.body.appendChild(link); - link.click(); - }); -}; - export const fetchAllEvents = async () => { const res = await axios.get(eventsURL); return res.data; diff --git a/client/src/app/api/ontimeApi.js b/client/src/app/api/ontimeApi.js new file mode 100644 index 000000000..188981ddb --- /dev/null +++ b/client/src/app/api/ontimeApi.js @@ -0,0 +1,17 @@ +import axios from 'axios'; +import { ontimeURL } from './apiConstants'; + +export const downloadEvents = async () => { + await axios({ + url: ontimeURL + '/db', + method: 'GET', + responseType: 'blob', // important + }).then((response) => { + const url = window.URL.createObjectURL(new Blob([response.data])); + const link = document.createElement('a'); + link.href = url; + link.setAttribute('download', 'events.json'); + document.body.appendChild(link); + link.click(); + }); +}; diff --git a/server/app.js b/server/app.js index 034e11e90..c8e3bace4 100644 --- a/server/app.js +++ b/server/app.js @@ -11,6 +11,7 @@ export const db = new Low(adapter); import express from 'express'; import http from 'http'; import cors from 'cors'; +import multer from 'multer'; import { dbModel } from './data/dataModel.js'; // Read data from JSON file, this will set db.data content @@ -30,9 +31,7 @@ export const data = db.data; // Import Routes import { router as eventsRouter } from './routes/eventsRouter.js'; import { router as eventRouter } from './routes/eventRouter.js'; - -// No settings yet -// const settingsRouter = require('./routes/settingsRouter.js'); +import { router as ontimeRouter } from './routes/ontimeRouter.js'; // Setup default port const port = process.env.PORT || config.server.port; @@ -50,12 +49,16 @@ app.use(cors()); app.options('*', cors()); // Implement middleware +app.use(express.static('./public')); +app.use('/uploads', express.static('uploads')); + app.use(express.urlencoded({ extended: true })); -app.use(express.json()); +app.use(express.json({ limit: '1mb' })); // Implement route endpoints app.use('/events', eventsRouter); app.use('/event', eventRouter); +app.use('/ontime', ontimeRouter); // implement general router app.get('/', (req, res) => { diff --git a/server/controllers/ontimeController.js b/server/controllers/ontimeController.js index c7f3a55d2..4778e7e21 100644 --- a/server/controllers/ontimeController.js +++ b/server/controllers/ontimeController.js @@ -1,7 +1,12 @@ // Create controller for GET request to '/ontime/download' +import { db, data } from '../app.js'; +function getEventTitle() { + return data.event.title; +} // Returns - -export const eventsDownload = async (req, res) => { - res.download('db.json', 'ontime events.json', (err) => { +export const dbDownload = async (req, res) => { + const fileTitle = getEventTitle() || 'ontime events'; + res.download('db.json', `${fileTitle}.json`, (err) => { if (err) { res.status(500).send({ message: 'Could not download the file. ' + err, diff --git a/server/routes/ontimeRouter.js b/server/routes/ontimeRouter.js index c45d1b3d7..d07d9867f 100644 --- a/server/routes/ontimeRouter.js +++ b/server/routes/ontimeRouter.js @@ -1,5 +1,7 @@ import express from 'express'; export const router = express.Router(); -// create route between controller and '/ontime/download' endpoint -router.get('/download', eventsDownload); +import { dbDownload, dbUpload } from '../controllers/ontimeController.js'; + +// create route between controller and '/ontime/db' endpoint +router.get('/db', dbDownload);