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