import { PageConfig } from 'next';
import an48 from '@mmstudio/an000048';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an49 from '@mmstudio/an000049';
import getuser from '../../../../atoms/getuser';
const logger = anylogger('pages/api/xxx/query');
export type Result = {
count: number;
data: IData[];
};
export type Query = {
q: string;
page: string;
pagesize: string;
}
export type IData = Pick<ITbxxx, 'id'|'xxx'>;
const handler = an48<Result>();
handler.get(async (req, res) => {
try {
logger.debug('msg body:', req.body);
const { q, ...rest } = req.query as Query;
const page = Number(rest.page || '1');
const pagesize = Number(rest.pagesize || '10');
const user = await getuser(req);
if (user.role !== 'admin') {
return { data: [], count: 0 };
}
const db = an49();
const dt1 = db<ITbxxx>('xxx');
if (q && typeof q === 'string') {
dt1.where('xxx', 'like', `%${q}%`);
}
const [{ size }] = await dt1.count('*', { as: 'size' });
const count = Number(size);
const dt2 = db<ITbxxx>('xxx');
if (q && typeof q === 'string') {
dt2.where('xxx', 'like', `%${q}%`);
}
const offset = (page - 1) * pagesize;
const d = await dt2.select('id', 'xxx')
.limit(pagesize)
.offset(offset)
.orderBy('xxx', 'asc');
const data = d.map((it) => {
return {
...it
} as IData;
});
res.status(200).json({ data, count });
} catch (error) {
logger.error(error);
res.status(200).json({
count: 0,
data: []
});
}
});
export const config = {} as PageConfig;
export default handler;