diff --git a/src/routes/api/v1/alertContacts.ts b/src/routes/api/v1/alertContacts.ts index cf164b6..de8c470 100644 --- a/src/routes/api/v1/alertContacts.ts +++ b/src/routes/api/v1/alertContacts.ts @@ -14,36 +14,55 @@ async function get(req: Request, res: Response) { log.api?.debug('alertContact GET Success:', req.query, value); // Query with FullTextSearch - const query_fts = { - where: { - OR: [{ id: parseIntOrUndefined(value.id) }, { name: { search: value.search } }, { phone: { search: value.search } }, { comment: { search: value.search } }] - }, - orderBy: parseDynamicSortBy(value.sort.toString(), value.order.toString()) - }; - if (value.search !== undefined || value.id !== undefined) { // with FullTextSearch if (!value.count) { // get all entrys log.api?.trace('get all entrys - with FullTextSearch'); - await db.alertContacts.findMany(query_fts).then((result) => { - res.status(200).json(result); - }); + await db.alertContacts + .findMany({ + where: { + OR: [{ id: parseIntOrUndefined(value.id) }, { name: { search: value.search } }, { phone: { search: value.search } }, { comment: { search: value.search } }] + }, + orderBy: parseDynamicSortBy(value.sort.toString(), value.order.toString()), + skip: parseIntOrUndefined(value.skip), + take: parseIntOrUndefined(value.take) + }) + .then((result) => { + if (result.length !== 0) { + res.status(200).json(result); + } else { + res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' }); + } + }); } else { // count all entrys log.api?.trace('count all entrys - with FullTextSearch'); - await db.alertContacts.count(query_fts).then((result) => { - res.status(200).json(result); - }); + await db.alertContacts + .count({ + where: { + OR: [{ id: parseIntOrUndefined(value.id) }, { name: { search: value.search } }, { phone: { search: value.search } }, { comment: { search: value.search } }] + }, + orderBy: parseDynamicSortBy(value.sort.toString(), value.order.toString()) + }) + .then((result) => { + res.status(200).json(result); + }); } } else { // without FullTextSearch if (!value.count) { // get all entrys log.api?.trace('get all entrys - without FullTextSearch'); - await db.alertContacts.findMany({ orderBy: parseDynamicSortBy(value.sort.toString(), value.order.toString()) }).then((result) => { - res.status(200).json(result); - }); + await db.alertContacts + .findMany({ orderBy: parseDynamicSortBy(value.sort.toString(), value.order.toString()), take: parseIntOrUndefined(value.take), skip: parseIntOrUndefined(value.skip) }) + .then((result) => { + if (result.length !== 0) { + res.status(200).json(result); + } else { + res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' }); + } + }); } else { // count all entrys without FullTextSearch log.api?.trace('count all entrys - without FullTextSearch'); diff --git a/src/routes/api/v1/alertContacts_schema.ts b/src/routes/api/v1/alertContacts_schema.ts index 5ed3c91..76def15 100644 --- a/src/routes/api/v1/alertContacts_schema.ts +++ b/src/routes/api/v1/alertContacts_schema.ts @@ -5,6 +5,10 @@ import validator from 'joi'; // DOCS: https://joi.dev/api const schema_get = validator.object({ sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'), order: validator.string().valid('asc', 'desc').default('asc'), + take: validator.number().min(1).max(512), + skip: validator.number().min(0), + + search: validator.string().min(3).max(20), // TODO: Check if * or ** or *** -> Due to crashes.. id: validator.number().positive().precision(0), count: validator.boolean()