rename search to all
This commit is contained in:
1 parent
d6142bca01
commit
33c6381a7c
3 files changed
+41
-16
No files matched your search
@@ -3,14 +3,15 @@ import { FastifyPluginAsync } from 'fastify'
|
|||||||
import S from 'jsonschema-definer'
|
import S from 'jsonschema-definer'
|
||||||
|
|
||||||
import { db } from '../shared'
|
import { db } from '../shared'
|
||||||
import { QSplit, qParseNum, pgOp } from '../util/search'
|
import { QSplit, pgOp, qParseNum } from '../util/search'
|
||||||
|
|
||||||
export const searchRouter: FastifyPluginAsync = async (f) => {
|
export const allRouter: FastifyPluginAsync = async (f) => {
|
||||||
{
|
{
|
||||||
const sQuery = S.shape({
|
const sQuery = S.shape({
|
||||||
q: S.string().minLength(1),
|
q: S.string(),
|
||||||
offset: S.integer().optional(),
|
offset: S.integer().optional(),
|
||||||
limit: S.integer().optional(),
|
limit: S.integer().optional(),
|
||||||
|
distinct: S.string().enum('id').optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
const sResponse = S.shape({
|
const sResponse = S.shape({
|
||||||
@@ -29,7 +30,9 @@ export const searchRouter: FastifyPluginAsync = async (f) => {
|
|||||||
default(v) {
|
default(v) {
|
||||||
if (v.includes('*')) {
|
if (v.includes('*')) {
|
||||||
return sql`"entry" && (
|
return sql`"entry" && (
|
||||||
SELECT array_agg("entry") FROM "frequency" WHERE "entry" LIKE ${'%' + v.replace('*', '%') + '%'}
|
SELECT array_agg("entry") FROM "frequency" WHERE "entry" LIKE ${
|
||||||
|
'%' + v.replace('*', '%') + '%'
|
||||||
|
}
|
||||||
)`
|
)`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,16 +64,28 @@ export const searchRouter: FastifyPluginAsync = async (f) => {
|
|||||||
kana: { ':': (v) => sql`"reading" &@ ${v}` },
|
kana: { ':': (v) => sql`"reading" &@ ${v}` },
|
||||||
reading: { ':': (v) => sql`"reading" &@ ${v}` },
|
reading: { ':': (v) => sql`"reading" &@ ${v}` },
|
||||||
kunyomi: {
|
kunyomi: {
|
||||||
':': (v) => sql`"reading" ${pgOp} ${`paths @ ".kunyomi" && string @ ${JSON.stringify(v)}`}`,
|
':': (v) =>
|
||||||
|
sql`"reading" ${pgOp} ${`paths @ ".kunyomi" && string @ ${JSON.stringify(
|
||||||
|
v
|
||||||
|
)}`}`,
|
||||||
},
|
},
|
||||||
onyomi: {
|
onyomi: {
|
||||||
':': (v) => sql`"reading" ${pgOp} ${`paths @ ".onyomi" && string @ ${JSON.stringify(v)}`}`,
|
':': (v) =>
|
||||||
|
sql`"reading" ${pgOp} ${`paths @ ".onyomi" && string @ ${JSON.stringify(
|
||||||
|
v
|
||||||
|
)}`}`,
|
||||||
},
|
},
|
||||||
nanori: {
|
nanori: {
|
||||||
':': (v) => sql`"reading" ${pgOp} ${`paths @ ".nanori" && string @ ${JSON.stringify(v)}`}`,
|
':': (v) =>
|
||||||
|
sql`"reading" ${pgOp} ${`paths @ ".nanori" && string @ ${JSON.stringify(
|
||||||
|
v
|
||||||
|
)}`}`,
|
||||||
},
|
},
|
||||||
english: {
|
english: {
|
||||||
':': (v) => sql`"translation" ${pgOp} ${`paths @ ".en" && string @ ${JSON.stringify(v)}`}`,
|
':': (v) =>
|
||||||
|
sql`"translation" ${pgOp} ${`paths @ ".en" && string @ ${JSON.stringify(
|
||||||
|
v
|
||||||
|
)}`}`,
|
||||||
},
|
},
|
||||||
translation: { ':': (v) => sql`"translation" &@ ${v}` },
|
translation: { ':': (v) => sql`"translation" &@ ${v}` },
|
||||||
type: {
|
type: {
|
||||||
@@ -89,7 +104,7 @@ export const searchRouter: FastifyPluginAsync = async (f) => {
|
|||||||
f.get<{
|
f.get<{
|
||||||
Querystring: typeof sQuery.type
|
Querystring: typeof sQuery.type
|
||||||
}>(
|
}>(
|
||||||
'/',
|
'/q',
|
||||||
{
|
{
|
||||||
schema: {
|
schema: {
|
||||||
querystring: sQuery.valueOf(),
|
querystring: sQuery.valueOf(),
|
||||||
@@ -101,6 +116,15 @@ export const searchRouter: FastifyPluginAsync = async (f) => {
|
|||||||
async (req): Promise<typeof sResponse.type> => {
|
async (req): Promise<typeof sResponse.type> => {
|
||||||
const { q, offset = 0, limit = 5 } = req.query
|
const { q, offset = 0, limit = 5 } = req.query
|
||||||
|
|
||||||
|
if (!q.trim()) {
|
||||||
|
return {
|
||||||
|
result: [],
|
||||||
|
offset,
|
||||||
|
limit,
|
||||||
|
count: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const entryCond = makeEntry.parse(q)
|
const entryCond = makeEntry.parse(q)
|
||||||
const levelCond = makeLevel.parse(q)
|
const levelCond = makeLevel.parse(q)
|
||||||
|
|
||||||
@@ -134,10 +158,11 @@ export const searchRouter: FastifyPluginAsync = async (f) => {
|
|||||||
)) FROM (
|
)) FROM (
|
||||||
SELECT * FROM cte
|
SELECT * FROM cte
|
||||||
ORDER BY "priority" DESC, "frequency" DESC
|
ORDER BY "priority" DESC, "frequency" DESC
|
||||||
LIMIT ${limit} OFFSET ${offset}
|
${limit > 0 ? sql`LIMIT ${limit}` : sql``}
|
||||||
|
OFFSET ${offset}
|
||||||
) t1)||'{}'::jsonb[] "result",
|
) t1)||'{}'::jsonb[] "result",
|
||||||
${offset} "offset",
|
${offset}::int "offset",
|
||||||
${limit} "limit",
|
${limit}::int "limit",
|
||||||
(SELECT count(*) FROM cte) "count"
|
(SELECT count(*) FROM cte) "count"
|
||||||
`)
|
`)
|
||||||
|
|
||||||
+3
-3
@@ -10,7 +10,7 @@ import fSession from 'fastify-session'
|
|||||||
import fastifySwagger from 'fastify-swagger'
|
import fastifySwagger from 'fastify-swagger'
|
||||||
|
|
||||||
import { db, isDev } from '../shared'
|
import { db, isDev } from '../shared'
|
||||||
import { searchRouter } from './search'
|
import { allRouter } from './all'
|
||||||
import { userRouter } from './user'
|
import { userRouter } from './user'
|
||||||
|
|
||||||
const apiRouter: FastifyPluginAsync = async (f) => {
|
const apiRouter: FastifyPluginAsync = async (f) => {
|
||||||
@@ -127,8 +127,8 @@ const apiRouter: FastifyPluginAsync = async (f) => {
|
|||||||
req.session['userId'] = userId
|
req.session['userId'] = userId
|
||||||
})
|
})
|
||||||
|
|
||||||
f.register(searchRouter, {
|
f.register(allRouter, {
|
||||||
prefix: '/search',
|
prefix: '/all',
|
||||||
})
|
})
|
||||||
|
|
||||||
f.register(userRouter, {
|
f.register(userRouter, {
|
||||||
|
|||||||
+1
-1
@@ -39,7 +39,7 @@ async function main() {
|
|||||||
redirect: true,
|
redirect: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
app.setNotFoundHandler((_, reply) => {
|
app.get('/app', (_, reply) => {
|
||||||
reply.redirect(200, '/')
|
reply.redirect(200, '/')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user