add search

This commit is contained in:
2025-10-22 17:26:03 +08:00
parent 833263f344
commit a18b255b36

View File

@@ -1,7 +1,7 @@
import { Hono } from "hono" import { Hono } from "hono"
import { cors } from "hono/cors" import { cors } from "hono/cors"
import db, { schema, initDatabase } from "./database" import db, { schema, initDatabase } from "./database"
import { eq, desc } from "drizzle-orm" import { eq, desc, like } from "drizzle-orm"
import { randomBytes } from "crypto" import { randomBytes } from "crypto"
const app = new Hono() const app = new Hono()
@@ -68,12 +68,26 @@ const handleError = (
// 获取项目列表 // 获取项目列表
app.get("/api/projects", async (c) => { app.get("/api/projects", async (c) => {
try { try {
const projectList = await db const searchQuery = c.req.query("search")
.select()
.from(schema.projects)
.orderBy(desc(schema.projects.id))
return c.json(projectList) // 如果有搜索查询,添加搜索条件
if (searchQuery && searchQuery.trim()) {
const searchTerm = `%${searchQuery.trim()}%`
const projectList = await db
.select()
.from(schema.projects)
.where(like(schema.projects.name, searchTerm))
.orderBy(desc(schema.projects.id))
return c.json(projectList)
} else {
const projectList = await db
.select()
.from(schema.projects)
.orderBy(desc(schema.projects.id))
return c.json(projectList)
}
} catch (error) { } catch (error) {
return handleError(c, error, "获取项目列表失败") return handleError(c, error, "获取项目列表失败")
} }