Compare commits

..

2 Commits

5 changed files with 39 additions and 34 deletions

View File

@ -32,12 +32,13 @@ enum itemStatus {
lost
}
// comments and descriptions -> @db.VarChar(2048)
model Item {
id Int @id @unique @default(autoincrement())
SKU String? @unique
amount Int @default(1)
name String
comment String?
comment String? @db.VarChar(2048)
status itemStatus @default(normal) /// TODO: Would it be better to create a separate model for this as well instead of providing several static statuses to choose from(enum)?
contactInfo contactInfo? @relation(fields: [contactInfoId], references: [id])
@ -81,7 +82,7 @@ model StorageUnit {
model itemCategory {
id Int @id @default(autoincrement())
name String @unique
description String?
description String? @db.VarChar(2048)
Item Item[]
}

View File

@ -11,12 +11,12 @@
<div class="modal-body">
<div class="mb-3">
<label for="itemModifyModalName" class="form-label">Name</label>
<input type="text" class="form-control" id="itemModifyModalName" name="name" required />
<input type="text" class="form-control" id="itemModifyModalName" name="name" maxlength="128" required />
<div id="itemModifyModalNameText" class="form-text">This name should be unqiue.</div>
</div>
<div class="mb-3">
<label for="itemModifyModalComment" class="form-label">Comment</label>
<input type="text" class="form-control" id="itemModifyModalComment" name="comment" />
<input type="text" class="form-control" id="itemModifyModalComment" maxlength="2048" name="comment" />
<div id="itemModifyModalDescText" class="form-text">Optional</div>
</div>
<div class="mb-3">
@ -36,12 +36,12 @@
</div>
<div class="mb-3">
<label for="itemModifyModalSKU" class="form-label">SKU</label>
<input type="text" class="form-control" id="itemModifyModalSKU" name="sku" />
<input type="text" class="form-control" id="itemModifyModalSKU" maxlength="64" name="sku" />
<div id="itemModifyModalSKUText" class="form-text">Optional</div>
</div>
<div class="mb-3">
<label for="itemModifyModalManuf" class="form-label">Manufacturer</label>
<input type="text" class="form-control" id="itemModifyModalManuf" name="manufacturer" />
<input type="text" class="form-control" id="itemModifyModalManuf" maxlength="190" name="manufacturer" />
<div id="itemModifyModalSKUText" class="form-text">Optional</div>
</div>
<div class="mb-3">
@ -98,7 +98,7 @@
<tr>
<th scope="col" data-field="SKU" class="sku" data-sortable="true">SKU</th>
<th scope="col" data-field="name" data-sortable="true">Name</th>
<th scope="col" data-field="comment" data-sortable="true">Comment</th>
<th scope="col" data-field="comment" data-sortable="true" data-width="80">Comment</th>
<th scope="col" data-field="status" data-sortable="true">Status</th>
<th scope="col" data-field="actions" data-searchable="false">Actions</th>
</tr>

View File

@ -20,12 +20,12 @@
<div class="modal-body">
<div class="mb-3">
<label for="editCategoryModalName" class="form-label">Name</label>
<input type="text" class="form-control" id="editCategoryModalName" name="name" required />
<input type="text" class="form-control" id="editCategoryModalName" maxlength="128" name="name" required />
<div id="editCategoryModalNameText" class="form-text">This name should be unqiue.</div>
</div>
<div class="mb-3">
<label for="editCategoryModalDescription" class="form-label">Description</label>
<input type="text" class="form-control" id="editCategoryModalDescription" name="description" />
<input type="text" class="form-control" id="editCategoryModalDescription" maxlength="2048" name="description" />
<div id="editCategoryModalDescText" class="form-text">Optional</div>
</div>
<input type="text" id="editCategoryModalId" name="id" hidden />

View File

@ -13,7 +13,7 @@
<div class="modal-body">
<div class="mb-3">
<label for="storageLocationModalName" class="form-label">Name</label>
<input type="text" class="form-control" id="storageLocationModalName" name="name" required />
<input type="text" class="form-control" id="storageLocationModalName" name="name" maxlength="128" required />
<div id="storageLocationModalNameText" class="form-text">This name should be unqiue.</div>
</div>
<div class="mb-3">

View File

@ -1,12 +1,13 @@
import { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js';
import { parseIntOrUndefined } from '../../../assets/helper.js';
// Get storageLocation.
function get(req: Request, res: Response) {
if (req.query.getAll === undefined) {
// Check if required fields are present.
if (!req.query.id) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return;
}
prisma.storageLocation
@ -23,12 +24,12 @@ function get(req: Request, res: Response) {
if (items) {
res.status(200).json(items);
} else {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' });
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageLocation does not exist' });
}
})
.catch((err) => {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
});
} else {
prisma.storageLocation
@ -42,12 +43,12 @@ function get(req: Request, res: Response) {
if (items) {
res.status(200).json(items);
} else {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' });
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageLocation does not exist' });
}
})
.catch((err) => {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
});
}
}
@ -56,7 +57,7 @@ function get(req: Request, res: Response) {
function post(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.name) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return;
}
// Create storageLocation with existing storageUnit.
@ -71,22 +72,22 @@ function post(req: Request, res: Response) {
}
})
.then((data) => {
res.status(201).json({ status: 'created', id: data.id });
res.status(201).json({ status: 'CREATED', message: 'Successfully created storageLocation', id: data.id });
})
.catch((err) => {
// Check if an entry already exists.
if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' });
res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageLocation already exists' });
} else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
// FIXME: Is this errormessage right?
res.status(404).json({ error: 'specified storageUnitId does not exist' });
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageUnitId does not exist' });
} else {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}
});
}
@ -95,7 +96,7 @@ function post(req: Request, res: Response) {
async function patch(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.id || !req.body.name || !req.body.storageUnitId) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return;
}
@ -108,12 +109,12 @@ async function patch(req: Request, res: Response) {
});
if (result === null) {
res.status(404).json({ error: 'storageLocation does not exist.' });
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageLocation does not exist' });
return;
}
} catch (err) {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}
prisma.storageLocation
@ -123,26 +124,29 @@ async function patch(req: Request, res: Response) {
},
data: {
name: req.body.name,
storageUnitId: parseInt(req.body.storageUnitId) || undefined
storageUnitId: parseIntOrUndefined(req.body.storageUnitId)
},
select: {
id: true
}
})
.then(() => {
res.status(201).json({ status: 'updated' });
.then((data) => {
res.status(201).json({ status: 'UPDATED', message: 'Successfully updated storageLocation', id: data.id });
})
.catch((err) => {
// Check if an entry already exists.
if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' });
res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'storageLocation already exists' });
} else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
// FIXME: Is this errormessage right?
res.status(404).json({ error: 'specified storageUnitId does not exist' });
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageUnitId does not exist' });
} else {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}
});
}
@ -151,7 +155,7 @@ async function patch(req: Request, res: Response) {
async function del(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.id) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return;
}
@ -164,12 +168,12 @@ async function del(req: Request, res: Response) {
});
if (result === null) {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' });
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'storageLocation does not exist' });
return;
}
} catch (err) {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}
prisma.storageLocation
@ -179,11 +183,11 @@ async function del(req: Request, res: Response) {
}
})
.then(() => {
res.status(200).json({ errorcode: 'DELETED', error: 'Sucessfully deleted entry' });
res.status(200).json({ status: 'DELETED', message: 'Successfully deleted storageLocation' });
})
.catch((err) => {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
});
}