// 添加账本 import express from "express"; const router = express.Router(); import { getBookById, books_insert, delBookById, ishaveBookById, updateBookName, getAllBook } from "#db"; import { shanghaiTime, shanghaiTimeFormat } from '#utils' // middleware that is specific to this router router.use(function timeLog(req, res, next) { console.log("Time: ", Date.now()); next(); }); // 获取所有账本 router.get("/", async function (req, res) { const { userInfo = {} } = req.body; const bookInfo = await getAllBook(userInfo.user_id); res.json({ code: 200, data: bookInfo.map(elm => { return { book_name: elm.book_name, create_time: shanghaiTimeFormat(elm.create_time), id: elm.id, update_time: shanghaiTimeFormat(elm.update_time), } }), }); }); // 获取账本详情 router.get("/:book_id", async function (req, res) { const book_id = req.params.book_id; // 获取 fileId 参数 const bookInfo = await getBookById(book_id); bookInfo.create_time = shanghaiTimeFormat(bookInfo.create_time) bookInfo.update_time = shanghaiTimeFormat(bookInfo.update_time) delete bookInfo.is_del res.json({ code: 200, data: bookInfo, }); }); // 添加账本数据 router.post("/", async function (req, res) { const { book_name = "", userInfo = {} } = req.body; // type 是否存在重复项,有就返回id,没有就新增 再返回id const isAddType = await ishaveBookById({ book_name, author_id: userInfo.user_id, }); if (isAddType) { res.json({ code: 500, msg: "已存在重复数据", }); return; } const insertId = await books_insert({ book_name, author_id: userInfo.user_id, create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), }); res.json({ code: 200, data: { book_id: insertId, }, }); }); // 更新bookName router.put("/:book_id", async function (req, res) { const book_id = req.params.book_id; // 获取 fileId 参数 const { book_name = ""} = req.body; await updateBookName({ book_name, book_id, update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), }) res.json({ code: 200, msg: "更新成功!", }); }); // 删除数据 router.delete("/:book_id", async function (req, res) { delBookById; // getBookById const book_id = req.params.book_id; // 获取 fileId 参数 await delBookById(book_id); res.json({ code: 200, msg: "删除成功!", }); }); export default router;