Implemented skip+take in alertContact api and some other optimizations

This commit is contained in:
Leon Meier 2025-01-31 23:09:53 +01:00
parent fb23f73963
commit e377af7501
2 changed files with 39 additions and 16 deletions

View File

@ -14,25 +14,38 @@ async function get(req: Request, res: Response) {
log.api?.debug('alertContact GET Success:', req.query, value); log.api?.debug('alertContact GET Success:', req.query, value);
// Query with FullTextSearch // 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) { if (value.search !== undefined || value.id !== undefined) {
// with FullTextSearch // with FullTextSearch
if (!value.count) { if (!value.count) {
// get all entrys // get all entrys
log.api?.trace('get all entrys - with FullTextSearch'); log.api?.trace('get all entrys - with FullTextSearch');
await db.alertContacts.findMany(query_fts).then((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); res.status(200).json(result);
} else {
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' });
}
}); });
} else { } else {
// count all entrys // count all entrys
log.api?.trace('count all entrys - with FullTextSearch'); log.api?.trace('count all entrys - with FullTextSearch');
await db.alertContacts.count(query_fts).then((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); res.status(200).json(result);
}); });
} }
@ -41,8 +54,14 @@ async function get(req: Request, res: Response) {
if (!value.count) { if (!value.count) {
// get all entrys // get all entrys
log.api?.trace('get all entrys - without FullTextSearch'); log.api?.trace('get all entrys - without FullTextSearch');
await db.alertContacts.findMany({ orderBy: parseDynamicSortBy(value.sort.toString(), value.order.toString()) }).then((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); res.status(200).json(result);
} else {
res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified object' });
}
}); });
} else { } else {
// count all entrys without FullTextSearch // count all entrys without FullTextSearch

View File

@ -5,6 +5,10 @@ import validator from 'joi'; // DOCS: https://joi.dev/api
const schema_get = validator.object({ const schema_get = validator.object({
sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'), sort: validator.string().valid('id', 'name', 'phone', 'comment').default('id'),
order: validator.string().valid('asc', 'desc').default('asc'), 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.. search: validator.string().min(3).max(20), // TODO: Check if * or ** or *** -> Due to crashes..
id: validator.number().positive().precision(0), id: validator.number().positive().precision(0),
count: validator.boolean() count: validator.boolean()