Implemented POST / PATCH / DELETE for alertcontact api route
This commit is contained in:
parent
94034fa29f
commit
7715672802
@ -2,7 +2,7 @@ import { Request, Response } from 'express';
|
||||
import db, { handlePrismaError } from '../../../handlers/db.js'; // Database
|
||||
import log from '../../../handlers/log.js';
|
||||
import { parseIntOrUndefined, parseDynamicSortBy } from '../../../helpers/prisma_helpers.js';
|
||||
import { schema_get, schema_post } from './alertContacts_schema.js';
|
||||
import { schema_get, schema_post, schema_patch, schema_del } from './alertContacts_schema.js';
|
||||
|
||||
// MARK: GET alertContact
|
||||
async function get(req: Request, res: Response) {
|
||||
@ -63,30 +63,69 @@ async function post(req: Request, res: Response) {
|
||||
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
|
||||
} else {
|
||||
log.api?.debug('alertContact POST Success:', req.body, value);
|
||||
|
||||
// // Check if undefined or null
|
||||
// if (req.body.name != null && req.body.phone != null) {
|
||||
await db.alertContacts
|
||||
.create({
|
||||
data: {
|
||||
name: value.name,
|
||||
phone: value.phone,
|
||||
comment: value.comment
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
})
|
||||
.then((result) => {
|
||||
res.status(201).json({ status: 'CREATED', message: 'Successfully created alertContact', id: result.id });
|
||||
});
|
||||
await db.alertContacts
|
||||
.create({
|
||||
data: {
|
||||
name: value.name,
|
||||
phone: value.phone,
|
||||
comment: value.comment
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
})
|
||||
.then((result) => {
|
||||
res.status(201).json({ status: 'CREATED', message: 'Successfully created alertContact', id: result.id });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: UPDATE alertContact
|
||||
async function patch(req: Request, res: Response) {}
|
||||
async function patch(req: Request, res: Response) {
|
||||
const { error, value } = schema_patch.validate(req.body);
|
||||
if (error) {
|
||||
log.api?.debug('alertContact PATCH Error:', req.body, value, error.details[0].message);
|
||||
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
|
||||
} else {
|
||||
log.api?.debug('alertContact PATCH Success:', req.body, value);
|
||||
await db.alertContacts
|
||||
.update({
|
||||
where: {
|
||||
id: value.id
|
||||
},
|
||||
data: {
|
||||
name: value.name,
|
||||
phone: value.phone,
|
||||
comment: value.comment
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
})
|
||||
.then((result) => {
|
||||
res.status(200).json({ status: 'UPDATED', message: 'Successfully updated alertContact', id: result.id });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: DELETE alertContact
|
||||
async function del(req: Request, res: Response) {}
|
||||
async function del(req: Request, res: Response) {
|
||||
const { error, value } = schema_del.validate(req.body);
|
||||
if (error) {
|
||||
log.api?.debug('alertContact DELETE Error:', req.body, value, error.details[0].message);
|
||||
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
|
||||
} else {
|
||||
log.api?.debug('alertContact DELETE Success:', req.body, value);
|
||||
await db.alertContacts
|
||||
.delete({
|
||||
where: {
|
||||
id: value.id
|
||||
}
|
||||
})
|
||||
.then((result) => {
|
||||
res.status(200).json({ status: 'DELETED', message: 'Successfully deleted alertContact', id: result.id });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default { get, post, patch, del };
|
||||
|
@ -1,8 +1,7 @@
|
||||
import { Request, Response } from 'express';
|
||||
import validator from 'joi'; // DOCS: https://joi.dev/api
|
||||
import log from '../../../handlers/log.js';
|
||||
|
||||
|
||||
// MARK: GET alertContact
|
||||
const schema_get = validator.object({
|
||||
sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'),
|
||||
order: validator.string().valid('asc', 'desc').default('asc'),
|
||||
@ -11,42 +10,40 @@ const schema_get = validator.object({
|
||||
count: validator.boolean()
|
||||
}).nand('id', 'search'); // Allow id or search. not both.
|
||||
|
||||
// MARK: CREATE alertContact
|
||||
const schema_post = validator.object({
|
||||
name: validator.string().min(1).max(32).required(),
|
||||
phone: validator.string().pattern(new RegExp('^\\+(\\d{1,3})\\s*(?:\\(\\s*(\\d{2,5})\\s*\\)|\\s*(\\d{2,5})\\s*)\\s*(\\d{5,15})$')).required(),
|
||||
comment: validator.string().max(64),
|
||||
})
|
||||
|
||||
// MARK: UPDATE alertContact
|
||||
const schema_patch = validator.object({
|
||||
sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'),
|
||||
order: validator.string().valid('asc', 'desc').default('asc'),
|
||||
search: validator.string().min(3).max(20), // TODO: Check if * or ** or *** -> Due to crashes..
|
||||
id: validator.number().positive().precision(0),
|
||||
count: validator.boolean()
|
||||
}).nand('id', 'search'); // Allow id or search. not both.
|
||||
id: validator.number().positive().precision(0).required(),
|
||||
name: validator.string().min(1).max(32),
|
||||
phone: validator.string().pattern(new RegExp('^\\+(\\d{1,3})\\s*(?:\\(\\s*(\\d{2,5})\\s*\\)|\\s*(\\d{2,5})\\s*)\\s*(\\d{5,15})$')),
|
||||
comment: validator.string().max(64),
|
||||
}).or('name', 'phone', 'comment')
|
||||
|
||||
// MARK: DELETE alertContact
|
||||
const schema_del = validator.object({
|
||||
sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'),
|
||||
order: validator.string().valid('asc', 'desc').default('asc'),
|
||||
search: validator.string().min(3).max(20), // TODO: Check if * or ** or *** -> Due to crashes..
|
||||
id: validator.number().positive().precision(0),
|
||||
count: validator.boolean()
|
||||
}).nand('id', 'search'); // Allow id or search. not both.
|
||||
id: validator.number().positive().precision(0).required()
|
||||
})
|
||||
|
||||
// Describe all schemas
|
||||
const schema_get_describe = schema_get.describe();
|
||||
const schema_post_describe = schema_post.describe();
|
||||
const schema_patch_describe = schema_patch.describe();
|
||||
const schema_del_describe = schema_del.describe();
|
||||
const schema_get_desc = schema_get.describe();
|
||||
const schema_post_desc = schema_post.describe();
|
||||
const schema_patch_desc = schema_patch.describe();
|
||||
const schema_del_desc = schema_del.describe();
|
||||
|
||||
|
||||
// GET route
|
||||
export default async function get(req: Request, res: Response) {
|
||||
res.status(200).json({
|
||||
GET: schema_get_describe,
|
||||
POST: schema_post_describe,
|
||||
PATCH: schema_patch_describe,
|
||||
DELETE: schema_del_describe
|
||||
GET: schema_get_desc,
|
||||
POST: schema_post_desc,
|
||||
PATCH: schema_patch_desc,
|
||||
DELETE: schema_del_desc
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user