Browse Source

rename search to all

main
parent
commit
33c6381a7c
3 changed files with 41 additions and 16 deletions
  1. +37
    -12
      src/api/all.ts
  2. +3
    -3
      src/api/index.ts
  3. +1
    -1
      src/index.ts

src/api/search.ts → src/api/all.ts View File

@ -3,14 +3,15 @@ import { FastifyPluginAsync } from 'fastify'
import S from 'jsonschema-definer'
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({
q: S.string().minLength(1),
q: S.string(),
offset: S.integer().optional(),
limit: S.integer().optional(),
distinct: S.string().enum('id').optional(),
})
const sResponse = S.shape({
@ -29,7 +30,9 @@ export const searchRouter: FastifyPluginAsync = async (f) => {
default(v) {
if (v.includes('*')) {
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}` },
reading: { ':': (v) => sql`"reading" &@ ${v}` },
kunyomi: {
':': (v) => sql`"reading" ${pgOp} ${`paths @ ".kunyomi" && string @ ${JSON.stringify(v)}`}`,
':': (v) =>
sql`"reading" ${pgOp} ${`paths @ ".kunyomi" && string @ ${JSON.stringify(
v
)}`}`,
},
onyomi: {
':': (v) => sql`"reading" ${pgOp} ${`paths @ ".onyomi" && string @ ${JSON.stringify(v)}`}`,
':': (v) =>
sql`"reading" ${pgOp} ${`paths @ ".onyomi" && string @ ${JSON.stringify(
v
)}`}`,
},
nanori: {
':': (v) => sql`"reading" ${pgOp} ${`paths @ ".nanori" && string @ ${JSON.stringify(v)}`}`,
':': (v) =>
sql`"reading" ${pgOp} ${`paths @ ".nanori" && string @ ${JSON.stringify(
v
)}`}`,
},
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}` },
type: {
@ -89,7 +104,7 @@ export const searchRouter: FastifyPluginAsync = async (f) => {
f.get<{
Querystring: typeof sQuery.type
}>(
'/',
'/q',
{
schema: {
querystring: sQuery.valueOf(),
@ -101,6 +116,15 @@ export const searchRouter: FastifyPluginAsync = async (f) => {
async (req): Promise<typeof sResponse.type> => {
const { q, offset = 0, limit = 5 } = req.query
if (!q.trim()) {
return {
result: [],
offset,
limit,
count: 0,
}
}
const entryCond = makeEntry.parse(q)
const levelCond = makeLevel.parse(q)
@ -134,10 +158,11 @@ export const searchRouter: FastifyPluginAsync = async (f) => {
)) FROM (
SELECT * FROM cte
ORDER BY "priority" DESC, "frequency" DESC
LIMIT ${limit} OFFSET ${offset}
${limit > 0 ? sql`LIMIT ${limit}` : sql``}
OFFSET ${offset}
) t1)||'{}'::jsonb[] "result",
${offset} "offset",
${limit} "limit",
${offset}::int "offset",
${limit}::int "limit",
(SELECT count(*) FROM cte) "count"
`)

+ 3
- 3
src/api/index.ts View File

@ -10,7 +10,7 @@ import fSession from 'fastify-session'
import fastifySwagger from 'fastify-swagger'
import { db, isDev } from '../shared'
import { searchRouter } from './search'
import { allRouter } from './all'
import { userRouter } from './user'
const apiRouter: FastifyPluginAsync = async (f) => {
@ -127,8 +127,8 @@ const apiRouter: FastifyPluginAsync = async (f) => {
req.session['userId'] = userId
})
f.register(searchRouter, {
prefix: '/search',
f.register(allRouter, {
prefix: '/all',
})
f.register(userRouter, {

+ 1
- 1
src/index.ts View File

@ -39,7 +39,7 @@ async function main() {
redirect: true,
})
app.setNotFoundHandler((_, reply) => {
app.get('/app', (_, reply) => {
reply.redirect(200, '/')
})

Loading…
Cancel
Save