Implemented api validation and item route patch

- Implemented api field validation in item api routes
- Implemented PATCH api route for item endpoint
This commit is contained in:
2023-06-27 23:45:58 +02:00
parent 87e1c55553
commit 4c0be6d87b
2 changed files with 71 additions and 38 deletions

View File

@ -70,9 +70,21 @@ export function parseIntRelation(data: string, relation_name: string = 'id') {
// This function is perfect. If data is not a valid number, return `undefined`
// If it is a valid number return `{connect: {relation_name: yourNumber}}}`
// This can be used by prisma to connect relations
// If the incoming data is null or empty, return a prisma disconnect object instead of a connect one
if (data === null || data === '') {
return JSON.parse(`{
"disconnect": true
}`);
}
return isNaN(parseInt(data)) ? undefined : JSON.parse(`{
"connect": {
"${relation_name}": ${parseInt(data)}
}
}`);
}
export function parseIntOrUndefined(data: string) {
return isNaN(parseInt(data)) ? undefined : parseInt(data);
}