diff --git a/src/frontend/publicInfoPage.eta.html b/src/frontend/publicInfoPage.eta.html index fd7ba12..56dad3f 100644 --- a/src/frontend/publicInfoPage.eta.html +++ b/src/frontend/publicInfoPage.eta.html @@ -2,11 +2,11 @@

<%= it.name %>

-

Category: <%= it.category%>

-

Amount: <%= it.Amount %>

-

SKU: <%= it.SKU %>

Comment: <%= it.comment %>

+

Category: <%= it.category.name %>

+

Amount: <%= it.amount %>

+

SKU: <%= it.SKU %>

-<%~ E.includeFile("partials/foot.eta.html") %> \ No newline at end of file +<%~ E.includeFile("partials/foot.eta.html") %> diff --git a/src/index.ts b/src/index.ts index 7208003..fe008d1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,7 +44,7 @@ export const prisma = new PrismaClient({ export const app = express(); app.set('x-powered-by', false); -app.set('strict routing', true); +//app.set('strict routing', true); app.engine('html', eta.renderFile); // app.use(cors()); @@ -58,10 +58,6 @@ app.use(fileUpload()); // app.use('/static', express.static('public')); app.use(express.static(__path + '/static')); -/* app.use((req, res, next) => { - res.status(404).send("Sorry can't find that!"); -}); */ -//routes(app); app.use(routes); app.listen(config.global.http_port, config.global.http_listen_address, () => { diff --git a/src/routes/api/v1/index.ts b/src/routes/api/v1/index.ts index fa9e93c..cae09c1 100644 --- a/src/routes/api/v1/index.ts +++ b/src/routes/api/v1/index.ts @@ -6,6 +6,7 @@ import testRoute from './test.js'; // Router base is '/api/v1' const Router = express.Router({ strict: false }); -Router.use('/test', testRoute); +Router.route('/test').get(testRoute.get); + export default Router; diff --git a/src/routes/api/v1/test.ts b/src/routes/api/v1/test.ts index 03da78d..c5f6d65 100644 --- a/src/routes/api/v1/test.ts +++ b/src/routes/api/v1/test.ts @@ -1,5 +1,7 @@ import express, { Request, Response } from 'express'; -export default (req: Request, res: Response) => { +function get(req: Request, res: Response) { res.status(200).send('API v1 Test Successful!'); }; + +export default { get }; diff --git a/src/routes/dev/index.ts b/src/routes/dev/index.ts deleted file mode 100644 index 8acd6ad..0000000 --- a/src/routes/dev/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -import express from 'express'; - -// Route imports -// import setDemoData from './setDemoData.js'; - -// Router base is '/dev' -const Router = express.Router(); - -// Router.use("/setDemoData", setDemoData) - -export default Router; diff --git a/src/routes/frontend/:id.ts b/src/routes/frontend/:id.ts index b676b51..31f3326 100644 --- a/src/routes/frontend/:id.ts +++ b/src/routes/frontend/:id.ts @@ -2,15 +2,28 @@ import { Request, Response } from 'express'; import { prisma, __path } from '../../index.js'; import * as Eta from 'eta'; -export default (req: Request, res: Response) => { - // retrieve data from database using id from url +function get(req: Request, res: Response) { + // Get data from database using sku from url. prisma.item .findFirst({ where: { SKU: req.params.id + }, + select: { + SKU: true, + name: true, + comment: true, + amount: true, + // Get category name from relation. + category: { + select: { + name: true + } + } } }) .then((item) => { + console.log(JSON.stringify(item)); if (item) { Eta.renderFile(__path + '/src/frontend/publicInfoPage.eta.html', item).then((html) => { res.send(html); @@ -19,4 +32,6 @@ export default (req: Request, res: Response) => { res.send('Item not found'); } }); -}; +} + +export default { get }; diff --git a/src/routes/frontend/dashboard.ts b/src/routes/frontend/dashboard.ts index dc85499..f1a24e7 100644 --- a/src/routes/frontend/dashboard.ts +++ b/src/routes/frontend/dashboard.ts @@ -1,29 +1,25 @@ import express, { Request, Response } from 'express'; import { prisma, __path } from '../../index.js'; -export default (req: Request, res: Response) => { - // TODO: Fix it? Express behaves like fucking shit with routers and /. Do not ask about it or touch it. EVER! (But if you can fix it a PR is welcome!) - if (req.originalUrl !== '/') { - // TODO: Respond based on content-type (with req.is('application/json')) - res.status(404).render(__path + '/src/frontend/errors/404.eta.html', { url: req.originalUrl }); - } else { - prisma.item - .findMany({ - orderBy: { - updatedAt: 'desc' - }, - // Limit to 10 items - take: 10 - }) - .then((items) => { - // Count amount of total items - prisma.item.count().then((count) => { - res.render(__path + '/src/frontend/dashboard.eta.html', { recents: items, stats: { total: count } }); - }); - }) - .catch((err) => { - console.error(err); - res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); +function get(req: Request, res: Response) { + prisma.item + .findMany({ + orderBy: { + updatedAt: 'desc' + }, + // Limit to 10 items + take: 10 + }) + .then((items) => { + // Count amount of total items + prisma.item.count().then((count) => { + res.render(__path + '/src/frontend/dashboard.eta.html', { recents: items, stats: { total: count } }); }); - } -}; + }) + .catch((err) => { + console.error(err); + res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); + }); +} + +export default { get }; diff --git a/src/routes/frontend/index.ts b/src/routes/frontend/index.ts index afef15c..fef2f6c 100644 --- a/src/routes/frontend/index.ts +++ b/src/routes/frontend/index.ts @@ -5,15 +5,18 @@ import skuRoute from './:id.js'; import testRoute from './test.js'; import dashboardRoute from './dashboard.js'; import itemsRoute from './items.js'; -import manage_routes from "./manage/index.js"; +import manage_routes from './manage/index.js'; // Router base is '/' const Router = express.Router({ strict: false }); -Router.use('/test', testRoute); -Router.use('/items', itemsRoute) -Router.use('/:id(\\w{8})', skuRoute); +Router.route('/test').get(testRoute.get); +Router.route('/items').get(itemsRoute.get); + +Router.route('/:id(\\w{8})').get(skuRoute.get); + Router.use('/manage', manage_routes); -Router.use('/', dashboardRoute); + +Router.route('/').get(dashboardRoute.get); export default Router; diff --git a/src/routes/frontend/items.ts b/src/routes/frontend/items.ts index 714ea07..ef56fda 100644 --- a/src/routes/frontend/items.ts +++ b/src/routes/frontend/items.ts @@ -1,7 +1,7 @@ -import express, { Request, Response } from 'express'; +import { Request, Response } from 'express'; import { prisma, __path } from '../../index.js'; -export default (req: Request, res: Response) => { +function get(req: Request, res: Response) { prisma.item .findMany({}) .then((items) => { @@ -12,4 +12,6 @@ export default (req: Request, res: Response) => { console.error(err); res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); }); -}; +} + +export default { get }; diff --git a/src/routes/frontend/manage/categoryManager.ts b/src/routes/frontend/manage/categoryManager.ts index be9559d..a5f62d8 100644 --- a/src/routes/frontend/manage/categoryManager.ts +++ b/src/routes/frontend/manage/categoryManager.ts @@ -33,7 +33,7 @@ export default (req: Request, res: Response) => { }, }) .then(() => { - res.redirect('/allCategories'); + res.redirect('categories'); }) .catch((err) => { // TODO Catch if is a duplicate error and show a message to the user @@ -52,7 +52,7 @@ export default (req: Request, res: Response) => { }, }) .then(() => { - res.redirect('/allCategories'); + res.redirect('categories'); }) .catch((err) => { // TODO Catch if is a duplicate error and show a message to the user diff --git a/src/routes/frontend/manage/import/csvImport.ts b/src/routes/frontend/manage/import/csvImport.ts index 3d375a2..2631316 100644 --- a/src/routes/frontend/manage/import/csvImport.ts +++ b/src/routes/frontend/manage/import/csvImport.ts @@ -1,39 +1,39 @@ import express, { Request, Response } from 'express'; import { prisma, __path, log } from '../../../../index.js'; import { UploadedFile } from 'express-fileupload'; -import { parse, transform } from 'csv'; +import { parse } from 'csv'; import { itemStatus, itemCategory, PrismaPromise } from '@prisma/client'; -export default (req: Request, res: Response) => { - // Decide wether its post or get - if (req.method === 'POST') { - // Handle file upload and import - console.log(req.files) - if (!req.files || Object.keys(req.files).length === 0) { - return res.status(400).send('No files were uploaded.'); - } +function post(req: Request, res: Response) { + // Handle file upload and import + console.log(req.files); + if (!req.files || Object.keys(req.files).length === 0) { + return res.status(400).send('No files were uploaded.'); + } - const file: UploadedFile = req.files.formFile as UploadedFile; - const csv = file.data.toString(); - parse(csv, { columns: true }, function (err, records) { - if (err) { - res.send(err); - return; - } - // Find all categories and save them into a set - const categories = new Set(); - records.forEach((record: any) => { - categories.add(record.category); - }); - log.db.debug(categories); - // Remove categories that already exists in the database - prisma.itemCategory.findMany({ + const file: UploadedFile = req.files.formFile as UploadedFile; + const csv = file.data.toString(); + parse(csv, { columns: true }, function (err, records) { + if (err) { + res.send(err); + return; + } + // Find all categories and save them into a set + const categories = new Set(); + records.forEach((record: any) => { + categories.add(record.category); + }); + log.db.debug(categories); + // Remove categories that already exists in the database + prisma.itemCategory + .findMany({ where: { name: { in: Array.from(categories) } } - }).then((values) => { + }) + .then((values) => { values.forEach((value) => { categories.delete(value.name); }); @@ -44,60 +44,61 @@ export default (req: Request, res: Response) => { data: { name: category } - }) + }); categoryPromises.push(promise); }); - Promise.all(categoryPromises).then((values) => { - // Create items - const listOfPromises = []; + Promise.all(categoryPromises) + .then((values) => { + // Create items + const listOfPromises = []; - for (let i = 0; i < records.length; i++) { - const record = records[i]; - const promise = prisma.item.create({ - data: { - name: record.name, - amount: parseInt(record.amount), - comment: record.comment, - category: { - connect: { - name: record.category - } - }, - SKU: record.sku, - manufacturer: record.manufacturer, - status: itemStatus.normal, - importedBy: "CSV_IMPORT" - } - }); - listOfPromises.push(promise); - } - Promise.all(listOfPromises).then((values) => { - console.log(values); - res.send('ok'); - }).catch((err) => { - res.send('failed to create items'); + for (let i = 0; i < records.length; i++) { + const record = records[i]; + const promise = prisma.item.create({ + data: { + name: record.name, + amount: parseInt(record.amount), + comment: record.comment, + category: { + connect: { + name: record.category + } + }, + SKU: record.sku, + manufacturer: record.manufacturer, + status: itemStatus.normal, + importedBy: 'CSV_IMPORT' + } + }); + listOfPromises.push(promise); + } + Promise.all(listOfPromises) + .then((values) => { + console.log(values); + res.send('ok'); + }) + .catch((err) => { + res.send('failed to create items'); + log.db.error(err); + return; + }); + }) + .catch((err) => { + // res.send('failed to create categories'); log.db.error(err); - return; }); - }).catch((err) => { - // res.send('failed to create categories'); - log.db.error(err); - - }); - - - }).catch((err) => { + }) + .catch((err) => { res.send('failed to find categories'); log.db.error(err); return; }); + }); +} - }); +function get(req: Request, res: Response) { + // Render page + res.render(__path + '/src/frontend/manage/imports/csvImport.eta.html'); +} - - } else { - // Render page - res.render(__path + '/src/frontend/manage/imports/csvImport.eta.html'); - } - -}; \ No newline at end of file +export default { get, post }; diff --git a/src/routes/frontend/manage/index.ts b/src/routes/frontend/manage/index.ts index 2beb804..ebc74bf 100644 --- a/src/routes/frontend/manage/index.ts +++ b/src/routes/frontend/manage/index.ts @@ -10,11 +10,11 @@ import startpageRoute from './startpage.js'; // Router base is '/manage' const Router = express.Router({ strict: false }); +Router.route('/test').get(testRoute.get); -Router.use('/test', testRoute); -Router.use('/categories', categoryManager) -Router.use('/storages', storageManager) -Router.use('/import/csv', csvImportRoute); -Router.use('/', startpageRoute); +Router.use('/categories', categoryManager); // TODO: Needs refactoring to new route format. +Router.route('/storages').get(storageManager.get); +Router.route('/import/csv').get(csvImportRoute.get).post(csvImportRoute.post); +Router.route('/').get(startpageRoute.get); export default Router; diff --git a/src/routes/frontend/manage/startpage.ts b/src/routes/frontend/manage/startpage.ts index 9ef8c50..f13ed36 100644 --- a/src/routes/frontend/manage/startpage.ts +++ b/src/routes/frontend/manage/startpage.ts @@ -1,7 +1,8 @@ import express, { Request, Response } from 'express'; import { prisma, __path } from '../../../index.js'; -export default (req: Request, res: Response) => { +function get(req: Request, res: Response) { res.render(__path + '/src/frontend/manage/startpage.eta.html'); //, { items: items }); +} -}; +export default { get }; diff --git a/src/routes/frontend/manage/storageManager.ts b/src/routes/frontend/manage/storageManager.ts index 7e55fca..675f469 100644 --- a/src/routes/frontend/manage/storageManager.ts +++ b/src/routes/frontend/manage/storageManager.ts @@ -1,15 +1,8 @@ import express, { Request, Response } from 'express'; import { prisma, __path } from '../../../index.js'; -export default (req: Request, res: Response) => { - // prisma.item - // .findMany({}) - // .then((items) => { - // Count amount of total items +function get(req: Request, res: Response) { res.render(__path + '/src/frontend/manage/storageManager.eta.html'); //, { items: items }); - // }) - // .catch((err) => { - // console.error(err); - // res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); - // }); -}; +} + +export default { get }; diff --git a/src/routes/frontend/manage/test.ts b/src/routes/frontend/manage/test.ts index 4e19628..8272d8d 100644 --- a/src/routes/frontend/manage/test.ts +++ b/src/routes/frontend/manage/test.ts @@ -1,4 +1,7 @@ -import express, { Request, Response } from 'express'; -export default (req: Request, res: Response) => { +import { Request, Response } from 'express'; + +function get(req: Request, res: Response) { res.status(200).send('Manage Test Successful!'); -}; +} + +export default { get }; diff --git a/src/routes/frontend/test.ts b/src/routes/frontend/test.ts index 868dda5..dd037b6 100644 --- a/src/routes/frontend/test.ts +++ b/src/routes/frontend/test.ts @@ -1,4 +1,7 @@ -import express, { Request, Response } from 'express'; -export default (req: Request, res: Response) => { - res.status(200).send("Frontend Test Successful!"); -}; \ No newline at end of file +import { Request, Response } from 'express'; + +function get(req: Request, res: Response) { + res.status(200).send('Frontend Test Successful!'); +} + +export default { get }; diff --git a/src/routes/index.ts b/src/routes/index.ts index 1772a54..11ddf8b 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,16 +1,21 @@ import express, { Express } from 'express'; +import { __path } from '../index.js'; // Route imports import frontend_routes from './frontend/index.js'; import static_routes from './static/index.js'; import api_routes from './api/index.js'; -import dev_routes from './dev/index.js'; const Router = express.Router({ strict: false }); Router.use('/static', static_routes); Router.use('/api', api_routes); -Router.use('/dev', dev_routes); // This is just for development. TODO: Add check if we are in devmode. Router.use('/', frontend_routes); -export default Router; \ No newline at end of file +// Default route. +Router.get('*', function (req, res) { + // TODO: Respond based on content-type (with req.is('application/json')) + res.status(404).render(__path + '/src/frontend/errors/404.eta.html', { url: req.originalUrl }); +}); + +export default Router; diff --git a/src/routes/static/index.ts b/src/routes/static/index.ts index 772721b..ac96e7e 100644 --- a/src/routes/static/index.ts +++ b/src/routes/static/index.ts @@ -11,7 +11,7 @@ const allowedURLs: Array = JSON.parse(fs.readFileSync('allowedStaticPath const recordedURLs: Array = []; const debugMode: boolean = JSON.parse(fs.readFileSync('allowedStaticPaths.json', 'utf8')).debugMode; -Router.use('*', (req: Request, res: Response) => { +Router.get('*', (req: Request, res: Response) => { if (debugMode) { res.sendFile(Path.join(__path, 'node_modules', req.params[0])); recordedURLs.push(req.params[0]);