parent
3be376b214
commit
5aeec6fb28
@ -18,7 +18,7 @@ async function get(req: Request, res: Response) {
|
||||
if (req.query.id) {
|
||||
// Check if number is a valid integer
|
||||
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;
|
||||
}
|
||||
prisma.item
|
||||
@ -45,12 +45,12 @@ async function get(req: Request, res: Response) {
|
||||
if (items) {
|
||||
res.status(200).json(items);
|
||||
} 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) => {
|
||||
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 {
|
||||
// Get all items
|
||||
@ -119,12 +119,12 @@ async function get(req: Request, res: Response) {
|
||||
if (items) {
|
||||
res.status(200).json({ total: itemCountFiltered, totalNotFiltered: itemCountNotFiltered, items: items });
|
||||
} 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) => {
|
||||
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) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Check if status is valid.
|
||||
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;
|
||||
}
|
||||
|
||||
@ -170,22 +170,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 item', 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: 'Item already exists' });
|
||||
res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'Item 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({ errorcode: 'NOT_EXISTING', error: 'Specified item does not exist' });
|
||||
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Item 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' });
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -194,19 +194,19 @@ function post(req: Request, res: Response) {
|
||||
async function patch(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;
|
||||
}
|
||||
|
||||
// Check if number is a valid integer
|
||||
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;
|
||||
}
|
||||
|
||||
// Check if status is valid.
|
||||
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;
|
||||
}
|
||||
|
||||
@ -237,24 +237,27 @@ async function patch(req: Request, res: Response) {
|
||||
// }
|
||||
//},
|
||||
createdBy: req.body.createdBy
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.status(201).json({ status: 'updated' });
|
||||
.then((data) => {
|
||||
res.status(201).json({ status: 'UPDATED', message: 'Successfully updated item', 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: 'Item already exists', err: err });
|
||||
res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'Item 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
|
||||
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 {
|
||||
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) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
@ -276,12 +279,12 @@ async function del(req: Request, res: Response) {
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
} 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.item
|
||||
@ -291,11 +294,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 item' });
|
||||
})
|
||||
.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' });
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user