parent
3be376b214
commit
5aeec6fb28
@ -11,14 +11,14 @@ async function get(req: Request, res: Response) {
|
|||||||
if (req.query.order === undefined) {
|
if (req.query.order === undefined) {
|
||||||
req.query.order = 'asc';
|
req.query.order = 'asc';
|
||||||
}
|
}
|
||||||
if(req.query.search === undefined) {
|
if (req.query.search === undefined) {
|
||||||
req.query.search = '';
|
req.query.search = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.query.id) {
|
if (req.query.id) {
|
||||||
// Check if number is a valid integer
|
// Check if number is a valid integer
|
||||||
if (!Number.isInteger(parseInt(req.query.id.toString()))) {
|
if (!Number.isInteger(parseInt(req.query.id.toString()))) {
|
||||||
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'The id field must be an integer' });
|
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
prisma.item
|
prisma.item
|
||||||
@ -45,12 +45,12 @@ async function get(req: Request, res: Response) {
|
|||||||
if (items) {
|
if (items) {
|
||||||
res.status(200).json(items);
|
res.status(200).json(items);
|
||||||
} else {
|
} else {
|
||||||
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' });
|
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item does not exist' });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
log.db.error(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 {
|
} else {
|
||||||
// Get all items
|
// Get all items
|
||||||
@ -119,12 +119,12 @@ async function get(req: Request, res: Response) {
|
|||||||
if (items) {
|
if (items) {
|
||||||
res.status(200).json({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items });
|
res.status(200).json({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items });
|
||||||
} else {
|
} else {
|
||||||
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' });
|
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item does not exist' });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
log.db.error(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' });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -133,13 +133,13 @@ async function get(req: Request, res: Response) {
|
|||||||
function post(req: Request, res: Response) {
|
function post(req: Request, res: Response) {
|
||||||
// Check if required fields are present.
|
// Check if required fields are present.
|
||||||
if (!req.body.name) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if status is valid.
|
// Check if status is valid.
|
||||||
if (req.body.status !== undefined && !Object.keys(itemStatus).includes(req.body.status)) {
|
if (req.body.status !== undefined && !Object.keys(itemStatus).includes(req.body.status)) {
|
||||||
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` });
|
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,22 +170,22 @@ function post(req: Request, res: Response) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
res.status(201).json({ status: 'created', id: data.id });
|
res.status(201).json({ status: 'CREATED', message: 'Successfully created item', id: data.id });
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
// Check if an entry already exists.
|
// Check if an entry already exists.
|
||||||
if (err.code === 'P2002') {
|
if (err.code === 'P2002') {
|
||||||
// P2002 -> "Unique constraint failed on the {constraint}"
|
// P2002 -> "Unique constraint failed on the {constraint}"
|
||||||
// https://www.prisma.io/docs/reference/api-reference/error-reference
|
// https://www.prisma.io/docs/reference/api-reference/error-reference
|
||||||
res.status(409).json({ errorcode: 'EXISTING', error: 'Item already exists' });
|
res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'Item already exists' });
|
||||||
} else if (err.code == 'P2003') {
|
} else if (err.code == 'P2003') {
|
||||||
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
|
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
|
||||||
// https://www.prisma.io/docs/reference/api-reference/error-reference
|
// https://www.prisma.io/docs/reference/api-reference/error-reference
|
||||||
// FIXME: Is this errormessage right?
|
// FIXME: Is this errormessage right?
|
||||||
res.status(404).json({ errorcode: 'NOT_EXISTING', error: 'Specified item does not exist' });
|
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item does not exist' });
|
||||||
} else {
|
} else {
|
||||||
log.db.error(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' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -194,19 +194,19 @@ function post(req: Request, res: Response) {
|
|||||||
async function patch(req: Request, res: Response) {
|
async function patch(req: Request, res: Response) {
|
||||||
// Check if required fields are present.
|
// Check if required fields are present.
|
||||||
if (!req.body.id) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if number is a valid integer
|
// Check if number is a valid integer
|
||||||
if (!Number.isInteger(parseInt(req.body.id.toString()))) {
|
if (!Number.isInteger(parseInt(req.body.id.toString()))) {
|
||||||
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'The id field must be an integer' });
|
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'id field must be an integer' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if status is valid.
|
// Check if status is valid.
|
||||||
if (req.body.status !== undefined && !Object.keys(itemStatus).includes(req.body.status)) {
|
if (req.body.status !== undefined && !Object.keys(itemStatus).includes(req.body.status)) {
|
||||||
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` });
|
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: `Status is not valid, valid values are: ${Object.keys(itemStatus).join(', ')}` });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,24 +237,27 @@ async function patch(req: Request, res: Response) {
|
|||||||
// }
|
// }
|
||||||
//},
|
//},
|
||||||
createdBy: req.body.createdBy
|
createdBy: req.body.createdBy
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then((data) => {
|
||||||
res.status(201).json({ status: 'updated' });
|
res.status(201).json({ status: 'UPDATED', message: 'Successfully updated item', id: data.id });
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
// Check if an entry already exists.
|
// Check if an entry already exists.
|
||||||
if (err.code === 'P2002') {
|
if (err.code === 'P2002') {
|
||||||
// P2002 -> "Unique constraint failed on the {constraint}"
|
// P2002 -> "Unique constraint failed on the {constraint}"
|
||||||
// https://www.prisma.io/docs/reference/api-reference/error-reference
|
// https://www.prisma.io/docs/reference/api-reference/error-reference
|
||||||
res.status(409).json({ errorcode: 'EXISTING', error: 'Item already exists', err: err });
|
res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'Item already exists' });
|
||||||
} else if (err.code == 'P2003') {
|
} else if (err.code == 'P2003') {
|
||||||
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
|
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
|
||||||
// https://www.prisma.io/docs/reference/api-reference/error-reference
|
// https://www.prisma.io/docs/reference/api-reference/error-reference
|
||||||
res.status(404).json({ errorcode: 'NOT_EXISTING', error: 'Specified item does not exist' });
|
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item does not exist' });
|
||||||
} else {
|
} else {
|
||||||
log.db.error(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' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -263,7 +266,7 @@ async function patch(req: Request, res: Response) {
|
|||||||
async function del(req: Request, res: Response) {
|
async function del(req: Request, res: Response) {
|
||||||
// Check if required fields are present.
|
// Check if required fields are present.
|
||||||
if (!req.body.id) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,12 +279,12 @@ async function del(req: Request, res: Response) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (result === null) {
|
if (result === null) {
|
||||||
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' });
|
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item does not exist' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.db.error(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.item
|
prisma.item
|
||||||
@ -291,11 +294,11 @@ async function del(req: Request, res: Response) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
res.status(200).json({ errorcode: 'DELETED', error: 'Sucessfully deleted entry' });
|
res.status(200).json({ status: 'DELETED', message: 'Successfully deleted item' });
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
log.db.error(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' });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user