The BIG frontend update
This commit is contained in:
@ -80,7 +80,7 @@ async function patch(req: Request, res: Response) {
|
||||
});
|
||||
|
||||
if (result === null) {
|
||||
res.status(410).json({ error: 'category does not exist.' });
|
||||
res.status(410).json({ error: 'Category does not exist.' });
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
|
@ -4,12 +4,17 @@ import express from 'express';
|
||||
import testRoute from './test.js';
|
||||
import categoryRoute from './categories.js';
|
||||
import storageUnitRoute from './storageUnits.js';
|
||||
import storageLocationRoute from './storageLocations.js';
|
||||
|
||||
|
||||
// Router base is '/api/v1'
|
||||
const Router = express.Router({ strict: false });
|
||||
|
||||
Router.route('/categories').get(categoryRoute.get).post(categoryRoute.post).patch(categoryRoute.patch).delete(categoryRoute.del);
|
||||
// TODO: Migrate routes to lowercase.
|
||||
Router.route('/storageUnits').get(storageUnitRoute.get).post(storageUnitRoute.post).patch(storageUnitRoute.patch).delete(storageUnitRoute.del);
|
||||
Router.route('/storageLocations').get(storageLocationRoute.get).post(storageLocationRoute.post).patch(storageLocationRoute.patch).delete(storageLocationRoute.del);
|
||||
|
||||
Router.route('/test').get(testRoute.get);
|
||||
|
||||
export default Router;
|
||||
|
@ -1,12 +1,32 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { prisma, __path } from '../../index.js';
|
||||
|
||||
function get(req: Request, res: Response) {
|
||||
async function get(req: Request, res: Response) {
|
||||
// If no page is provided redirect to first
|
||||
if (req.query.page === undefined) {
|
||||
res.redirect('?page=1');
|
||||
return;
|
||||
}
|
||||
|
||||
let page = parseInt(req.query.page.toString());
|
||||
const itemCount = await prisma.item.count({}); // Count all items in the DB
|
||||
|
||||
const takeSize = 25; // Amount of times per page
|
||||
const pageSize = Math.ceil(itemCount / takeSize); // Amount of pages, always round up
|
||||
|
||||
// If page is less then 1 or more then the max page size redirect to first or last page
|
||||
if (page < 1) {
|
||||
res.redirect('?page=1');
|
||||
return;
|
||||
} else if (page > pageSize) {
|
||||
res.redirect('?page=' + pageSize);
|
||||
return;
|
||||
}
|
||||
|
||||
prisma.item
|
||||
.findMany({})
|
||||
.findMany({ skip: (page - 1) * takeSize, take: takeSize }) // Skip the amount of items per page times the page number minus 1; skip has to be (page-1)*takeSize because skip is 0 indexed
|
||||
.then((items) => {
|
||||
// Count amount of total items
|
||||
res.render(__path + '/src/frontend/items.eta.html', { items: items });
|
||||
res.render(__path + '/src/frontend/items.eta.html', { items: items, currentPage: page, maxPages: pageSize });
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
|
8
src/routes/frontend/manage/demo.ts
Normal file
8
src/routes/frontend/manage/demo.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import express, { Request, Response } from 'express';
|
||||
import { prisma, __path } from '../../../index.js';
|
||||
|
||||
function get(req: Request, res: Response) {
|
||||
res.render(__path + '/src/frontend/auth/login.eta.html'); //, { items: items });
|
||||
}
|
||||
|
||||
export default { get };
|
@ -7,6 +7,7 @@ import jsonImportRoute from './import/jsonImport.js';
|
||||
import categoryManager from './categoryManager.js';
|
||||
import storageManager from './storageManager.js';
|
||||
import startpageRoute from './startpage.js';
|
||||
import demoLoginRoute from './demo.js'
|
||||
|
||||
// Router base is '/manage'
|
||||
const Router = express.Router({ strict: false });
|
||||
@ -16,6 +17,7 @@ Router.route('/categories').get(categoryManager.get);
|
||||
Router.route('/storages').get(storageManager.get);
|
||||
Router.route('/import/csv').get(csvImportRoute.get).post(csvImportRoute.post);
|
||||
Router.route('/import/json').get(jsonImportRoute.get).post(jsonImportRoute.post);
|
||||
Router.route('/demo/login').get(demoLoginRoute.get);
|
||||
Router.route('/').get(startpageRoute.get);
|
||||
|
||||
export default Router;
|
||||
|
@ -5,4 +5,5 @@ function get(req: Request, res: Response) {
|
||||
res.render(__path + '/src/frontend/manage/startpage.eta.html'); //, { items: items });
|
||||
}
|
||||
|
||||
|
||||
export default { get };
|
||||
|
@ -2,12 +2,18 @@ import express, { Request, Response } from 'express';
|
||||
import { prisma, __path } from '../../../index.js';
|
||||
|
||||
function get(req: Request, res: Response) {
|
||||
prisma.storageUnit.findMany({include: {contactInfo: true}}).then((storUnits) => {
|
||||
prisma.storageLocation.findMany().then((storLocs) => {
|
||||
prisma.contactInfo.findMany().then((contactInfos) => {
|
||||
res.render(__path + '/src/frontend/manage/storageManager.eta.html', { storUnits: storUnits, storLocs: storLocs, address: contactInfos });
|
||||
prisma.storageUnit.findMany({ include: { contactInfo: true } }).then((storUnits) => {
|
||||
prisma.storageLocation
|
||||
.findMany({
|
||||
include: {
|
||||
storageUnit: true
|
||||
}
|
||||
})
|
||||
.then((storLocs) => {
|
||||
prisma.contactInfo.findMany().then((contactInfos) => {
|
||||
res.render(__path + '/src/frontend/manage/storageManager.eta.html', { storUnits: storUnits, storLocs: storLocs, address: contactInfos });
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,9 @@ Router.use('/', frontend_routes);
|
||||
Router.all('*', function (req, res) {
|
||||
// TODO: Respond based on content-type (with req.is('application/json'))
|
||||
if (req.is('application/json')) {
|
||||
res.status(404).json({ errorcode: '404' });
|
||||
res.status(418)//.json({ errorcode: '404' });
|
||||
} else {
|
||||
res.status(404).render(__path + '/src/frontend/errors/404.eta.html', { url: req.originalUrl });
|
||||
res.status(418)//.render(__path + '/src/frontend/errors/404.eta.html', { url: req.originalUrl });
|
||||
}
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user