update: revision for endpoint get on management aspect
This commit is contained in:
parent
0edd2f40ed
commit
4a679438be
|
|
@ -80,9 +80,19 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const { includeTrashed, page, limit, q } = c.req.valid("query");
|
const { includeTrashed, page, limit, q } = c.req.valid("query");
|
||||||
|
|
||||||
const totalCountQuery = includeTrashed
|
const aspectCountQuery = await db
|
||||||
? sql<number>`(SELECT count(DISTINCT ${aspects.id}) FROM ${aspects})`
|
.select({
|
||||||
: sql<number>`(SELECT count(DISTINCT ${aspects.id}) FROM ${aspects} WHERE ${aspects.deletedAt} IS NULL)`;
|
count: sql<number>`count(*)`,
|
||||||
|
})
|
||||||
|
.from(aspects)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
includeTrashed ? undefined : isNull(aspects.deletedAt),
|
||||||
|
q ? or(ilike(aspects.name, q), eq(aspects.id, q)) : undefined
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
const totalItems = Number(aspectCountQuery[0]?.count) || 0;
|
||||||
|
|
||||||
const aspectIdsQuery = await db
|
const aspectIdsQuery = await db
|
||||||
.select({
|
.select({
|
||||||
|
|
@ -129,7 +139,6 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
FROM ${questions}
|
FROM ${questions}
|
||||||
WHERE ${questions.subAspectId} = ${subAspects.id}
|
WHERE ${questions.subAspectId} = ${subAspects.id}
|
||||||
)`.as('questionCount'),
|
)`.as('questionCount'),
|
||||||
fullCount: totalCountQuery,
|
|
||||||
})
|
})
|
||||||
.from(aspects)
|
.from(aspects)
|
||||||
.leftJoin(subAspects, eq(subAspects.aspectId, aspects.id))
|
.leftJoin(subAspects, eq(subAspects.aspectId, aspects.id))
|
||||||
|
|
@ -178,8 +187,8 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
data: groupedArray,
|
data: groupedArray,
|
||||||
_metadata: {
|
_metadata: {
|
||||||
currentPage: page,
|
currentPage: page,
|
||||||
totalPages: Math.ceil((Number(result[0]?.fullCount) ?? 0) / limit),
|
totalPages: Math.ceil(totalItems / limit),
|
||||||
totalItems: Number(result[0]?.fullCount) ?? 0,
|
totalItems,
|
||||||
perPage: limit,
|
perPage: limit,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user