diff --git a/src/frontend/manage/storageManager.eta.html b/src/frontend/manage/storageManager.eta.html
index 6e53d4e..6587c12 100644
--- a/src/frontend/manage/storageManager.eta.html
+++ b/src/frontend/manage/storageManager.eta.html
@@ -1,5 +1,130 @@
<%~ E.includeFile("../partials/head.eta.html", {"title": "Settings - Storage Manager"}) %> <%~ E.includeFile("../partials/controls.eta.html", {"active": "SETT_STORE"}) %>
+<%~ E.includeFile("../partials/deleteModal.eta.html") %>
+
+
+
+
+ A storage location is a place where you can store your items. It can be a room, a shelf or a box.
@@ -47,27 +174,48 @@
-
+ <% it.storLocs.forEach(function(locations){ %>
+
+ <%= locations.name %> |
+ <%= locations.contactInfo %> |
+
+
+
+ |
+
+ <% }) %>
+
+ A storage unit is a physical place, like a warehouse. This contains an address and a name.
- # |
Name |
Address |
Actions |
+ <% it.storUnits.forEach(function(units){ %>
+
+ <%= units.name %> |
+ <%= units.contactInfo.street %> <%= units.contactInfo.houseNumber %>, <%= units.contactInfo.city %> <%= units.contactInfo.country %> |
+
+
+
+ |
+
+ <% }) %>
+
diff --git a/src/frontend/partials/deleteModal.eta.html b/src/frontend/partials/deleteModal.eta.html
new file mode 100644
index 0000000..64af238
--- /dev/null
+++ b/src/frontend/partials/deleteModal.eta.html
@@ -0,0 +1,16 @@
+
+
+
+
+
+
This will permanently delete the category and all its associated data.
Items will be kept but will be unassigned from this category.
+
+
+
+
\ No newline at end of file
diff --git a/src/routes/api/v1/index.ts b/src/routes/api/v1/index.ts
index 58abc96..b98b39a 100644
--- a/src/routes/api/v1/index.ts
+++ b/src/routes/api/v1/index.ts
@@ -3,11 +3,13 @@ import express from 'express';
// Route imports
import testRoute from './test.js';
import categoryRoute from './categories.js';
+import storageUnitRoute from './storageUnits.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);
+Router.route('/storageUnits').get(storageUnitRoute.get).post(storageUnitRoute.post).patch(storageUnitRoute.patch).delete(storageUnitRoute.del);
Router.route('/test').get(testRoute.get);
export default Router;
diff --git a/src/routes/api/v1/storageUnits.ts b/src/routes/api/v1/storageUnits.ts
new file mode 100644
index 0000000..76f5a6a
--- /dev/null
+++ b/src/routes/api/v1/storageUnits.ts
@@ -0,0 +1,148 @@
+import { Request, Response } from 'express';
+import { prisma, __path, log } from '../../../index.js';
+
+// Get storageUnit.
+function get(req: Request, res: Response) {
+ if (req.query.getAll === undefined) {
+ // Check if required fields are present.
+ if (!req.query.name) {
+ res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
+ return;
+ }
+ prisma.storageUnit
+ .findUnique({
+ where: {
+ name: req.query.name.toString()
+ },
+ // Get category name from relation.
+ include: {
+ contactInfo: true
+ }
+ })
+ .then((items) => {
+ if (items) {
+ res.status(200).json(JSON.stringify(items));
+ } else {
+ res.status(410).json({ error: 'it seems that there is no storageUnit present.' });
+ }
+ })
+ .catch((err) => {
+ console.error(err);
+ res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
+ });
+ } else {
+ prisma.storageUnit
+ .findMany({
+ // Get category name from relation.
+ include: {
+ contactInfo: true
+ }
+ })
+ .then((items) => {
+ if (items) {
+ res.status(200).json(JSON.stringify(items));
+ } else {
+ res.status(410).json({ error: 'storageUnit does not exist.' });
+ }
+ })
+ .catch((err) => {
+ console.error(err);
+ res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
+ });
+ }
+}
+
+// Create storageUnit.
+function post(req: Request, res: Response) {
+ log.web.debug(JSON.stringify(req.body));
+/* // Check if required fields are present.
+ if (!req.body.name) {
+ res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
+ return;
+ }
+
+ // Save data.
+ prisma.storageUnit
+ .create({
+ data: {
+ name: req.body.name
+ }
+ })
+ .then(() => {
+ res.status(201).json({ status: 'created' });
+ })
+ .catch((err) => {
+ // TODO Catch if is a duplicate error and show a message to the user
+ log.db.error(err);
+ res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
+ }); */
+}
+
+// Update category.
+function patch(req: Request, res: Response) {
+ // Check if required fields are present.
+ if (!req.body.id || !req.body.name) {
+ res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
+ return;
+ }
+
+ prisma.storageUnit
+ .update({
+ where: {
+ id: parseInt(req.body.id)
+ },
+ data: {
+ name: req.body.name
+ }
+ })
+ .then(() => {
+ res.status(201).json({ status: 'updated' });
+ })
+ .catch((err) => {
+ // TODO Catch if is a duplicate error and show a message to the user
+ log.db.error(err);
+ res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
+ });
+}
+
+// Delete category.
+async function del(req: Request, res: Response) {
+ // Check if required fields are present.
+ if (!req.body.id) {
+ res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
+ return;
+ }
+
+ // Does the id exist? If not return 410 Gone.
+ try {
+ const result = await prisma.storageUnit.findUnique({
+ where: {
+ id: parseInt(req.body.id)
+ }
+ });
+
+ if (result === null) {
+ res.status(410).json({ error: 'Storage Unit does not exist.' });
+ return;
+ }
+ } catch (err) {
+ log.db.error(err);
+ res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
+ }
+
+ prisma.storageUnit
+ .delete({
+ where: {
+ id: parseInt(req.body.id)
+ }
+ })
+ .then(() => {
+ res.status(201).json({ status: 'deleted' });
+ })
+ .catch((err) => {
+ log.db.error(err);
+ res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
+ });
+}
+
+export default { get, post, patch, del };
diff --git a/src/routes/frontend/manage/storageManager.ts b/src/routes/frontend/manage/storageManager.ts
index 675f469..936d74f 100644
--- a/src/routes/frontend/manage/storageManager.ts
+++ b/src/routes/frontend/manage/storageManager.ts
@@ -2,7 +2,13 @@ import express, { Request, Response } from 'express';
import { prisma, __path } from '../../../index.js';
function get(req: Request, res: Response) {
- res.render(__path + '/src/frontend/manage/storageManager.eta.html'); //, { items: items });
+ 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 });
+ });
+ });
+ });
}
export default { get };
diff --git a/src/routes/index.ts b/src/routes/index.ts
index 046f6c2..3bec316 100644
--- a/src/routes/index.ts
+++ b/src/routes/index.ts
@@ -1,5 +1,5 @@
import express, { Express } from 'express';
-import { __path } from '../index.js';
+import { __path, prisma } from '../index.js';
// Route imports
import frontend_routes from './frontend/index.js';
@@ -12,9 +12,6 @@ Router.use('/static', static_routes);
Router.use('/api', api_routes);
Router.use('/', frontend_routes);
-Router.get('/debug-sentry', function mainHandler(req, res) {
- throw new Error('My first Sentry error!');
-});
// Default route.
Router.all('*', function (req, res) {
// TODO: Respond based on content-type (with req.is('application/json'))
diff --git a/static/js/editStorages.js b/static/js/editStorages.js
index 9f7ce20..701a583 100644
--- a/static/js/editStorages.js
+++ b/static/js/editStorages.js
@@ -3,21 +3,53 @@
// Taken from https://stackoverflow.com/a/9393768/11317151 (and edited, like a lot)
// Also update on location change
-window.addEventListener("hashchange", function() {
-
-
-var hash = location.hash.replace(/^#/, ''); // ^ means starting, meaning only match the first hash
+window.addEventListener(
+ 'hashchange',
+ function () {
+ var hash = location.hash.replace(/^#/, ''); // ^ means starting, meaning only match the first hash
+ if (hash) {
+ bootstrap.Tab.getOrCreateInstance(document.querySelector('#' + hash)).show();
+ }
+ },
+ false
+);
+var hash = location.hash.replace(/^#/, ''); // ^ means starting, meaning only match the first hash
if (hash) {
- bootstrap.Tab.getOrCreateInstance(document.querySelector('#' + hash)).show()
-}
-
-}, false);
-var hash = location.hash.replace(/^#/, ''); // ^ means starting, meaning only match the first hash
-if (hash) {
- bootstrap.Tab.getOrCreateInstance(document.querySelector('#' + hash)).show()
-}
+ bootstrap.Tab.getOrCreateInstance(document.querySelector('#' + hash)).show();
+}
// Change hash for page-reload
$('.nav-link').on('click', function (e) {
window.location.hash = e.target.id;
-})
\ No newline at end of file
+});
+
+function primeCreateNew() {
+ const form = document.getElementById('storageUnitModalForm');
+ const form2 = document.getElementById('storageLocationModal');
+ form.setAttribute('method', 'POST');
+ form2.setAttribute('method', 'POST');
+ return true;
+}
+
+function primeEdit() {
+ const form = document.getElementById('storageUnitModalForm');
+ const form2 = document.getElementById('storageLocationModal');
+ form.setAttribute('method', 'PATCH');
+ form2.setAttribute('method', 'PATCH');
+ return true;
+}
+
+function handleSelector(){
+ const selector = document.getElementById('storageUnitModalLocationSelect')
+ const value = selector.options[selector.selectedIndex].value;
+ if(value == "META_CREATENEW") {
+ $('#storageUnitModalContactInfoCreator').removeClass('d-none')
+ $('.requireOnCreate').attr('required', true)
+ } else {
+ $('#storageUnitModalContactInfoCreator').addClass('d-none')
+ $('.requireOnCreate').attr('required', false)
+ }
+ console.log(value);
+}
+
+handleSelector()
\ No newline at end of file