Files
score-backend/routes/media.ts
ethan.chen b33ac37720 修复 page 接口返回的 date 字段格式
- 格式化 date 字段,只返回日期部分(YYYY-MM-DD),移除时间信息
2026-01-08 18:00:19 +08:00

204 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Hono } from "hono";
import { db } from "../db/index.ts";
import type { JwtVariables } from "hono/jwt";
import { authMiddleware } from "../auth.ts";
const media = new Hono<{ Variables: JwtVariables }>();
// 查询满分10分作品不需要JWT验证
media.get("/perfect", async (c) => {
try {
const perfectList = await db
.prepare("SELECT * FROM media WHERE rating = 10")
.all();
return c.json({
code: 0,
data: perfectList,
message: "Success",
});
} catch (error: any) {
return c.json({ code: 1, data: {}, message: error.message }, 500);
}
});
media.use("/*", authMiddleware);
// 获取所有媒体记录
media.get("/list", async (c) => {
try {
const mediaList = await db.prepare("SELECT * FROM media").all();
return c.json({
code: 0,
data: mediaList,
message: "Success",
});
} catch (error: any) {
return c.json({ code: 1, data: {}, message: error.message }, 500);
}
});
// 创建新的媒体记录
media.post("/create", async (c) => {
try {
const data = await c.req.json();
const { title, type, rating, notes, platform, date } = data;
const result = await db
.prepare(
`
INSERT INTO media (title, type, rating, notes, platform, date, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW())
RETURNING id
`
)
.run(title, type, rating, notes, platform, date);
const newMedia = await db
.prepare("SELECT * FROM media WHERE id = ?")
.get(result.lastInsertRowid);
return c.json(
{
code: 0,
data: newMedia,
message: "Created successfully",
},
201
);
} catch (error: any) {
return c.json({ code: 2, data: {}, message: error.message }, 500);
}
});
// 更新媒体记录
media.put("/updateById/:id", async (c) => {
try {
const id = c.req.param("id");
const data = await c.req.json();
const { title, type, rating, notes, platform, date } = data;
await db
.prepare(
`
UPDATE media
SET title = ?, type = ?, rating = ?, notes = ?, platform = ?, date = ?, updated_at = NOW()
WHERE id = ?
`
)
.run(title, type, rating, notes, platform, date, id);
const updatedMedia = await db
.prepare("SELECT * FROM media WHERE id = ?")
.get(id);
if (!updatedMedia) {
return c.json({ code: 1, data: {}, message: "Media not found" }, 404);
}
return c.json({
code: 0,
data: updatedMedia,
message: "Updated successfully",
});
} catch (error: any) {
return c.json({ code: 2, data: {}, message: error.message }, 500);
}
});
// 删除媒体记录
media.delete("/deleteById/:id", async (c) => {
try {
const id = c.req.param("id");
await db.prepare("DELETE FROM media WHERE id = ?").run(id);
return c.json({ code: 0, data: {}, message: "Deleted successfully" });
} catch (error: any) {
return c.json({ code: 2, data: {}, message: error.message }, 500);
}
});
// 分页查询媒体记录
media.get("/page", async (c) => {
try {
const type = c.req.query("type");
const currentPage = parseInt(c.req.query("currentPage") || "1");
const pageSize = parseInt(c.req.query("pageSize") || "10");
const title = c.req.query("title") || "";
const startDate = c.req.query("startDate");
const endDate = c.req.query("endDate");
const sortBy = c.req.query("sortBy") || "date";
const sortType = c.req.query("sortType") || "desc";
if (!type) {
return c.json({ code: 1, data: {}, message: "Type is required" }, 400);
}
let query = "SELECT * FROM media WHERE type = ?";
const params = [type];
if (title) {
query += " AND title LIKE ?";
params.push(`%${title}%`);
}
if (startDate) {
query += " AND date >= ?";
params.push(startDate);
}
if (endDate) {
query += " AND date <= ?";
params.push(endDate);
}
// 添加排序
if (sortBy === "date") {
query += ` ORDER BY date ${sortType.toUpperCase()}`;
} else if (sortBy === "score") {
query += ` ORDER BY rating ${sortType.toUpperCase()}`;
}
// 添加分页
const offset = (currentPage - 1) * pageSize;
query += " LIMIT ? OFFSET ?";
params.push(pageSize.toString(), offset.toString());
// 获取总数
const countQuery = query
.replace("SELECT *", "SELECT COUNT(*) as total")
.replace(/ORDER BY.*$/, "") // 移除 ORDER BY 子句
.replace(/LIMIT.*$/, ""); // 移除 LIMIT 和 OFFSET 子句
const totalResult = await db
.prepare(countQuery)
.get(...params.slice(0, -2));
const total = totalResult?.total || 0;
// 获取分页数据
const mediaList = await db.prepare(query).all(...params);
// 格式化 date 字段只返回日期部分YYYY-MM-DD
const formattedList = mediaList.map((item: any) => {
if (item.date) {
// 如果是 Date 对象或字符串,格式化为 YYYY-MM-DD
const date = new Date(item.date);
if (!isNaN(date.getTime())) {
item.date = date.toISOString().split("T")[0];
}
}
return item;
});
return c.json({
code: 0,
data: {
list: formattedList,
total,
currentPage,
pageSize,
},
message: "Success",
});
} catch (error: any) {
return c.json({ code: 1, data: {}, message: error.message }, 500);
}
});
export default media;