12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- // 添加账本
- import express from "express";
- const router = express.Router();
- import {
- getBookById,
- books_insert,
- delBookById,
- ishaveBookById,
- updateBookName,
- } 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("/:book_id", async function (req, res) {
- // getBookById
- const book_id = req.params.book_id; // 获取 fileId 参数
- const bookInfo = await getBookById(book_id);
- // console.log(29, bookInfo)
- shanghaiTimeFormat
- bookInfo.create_time = shanghaiTimeFormat(bookInfo.create_time)
- bookInfo.update_time = shanghaiTimeFormat(bookInfo.update_time)
- res.json({
- code: 200,
- data: bookInfo,
- });
- // res.send("Books home page");
- });
- // 添加账本数据
- 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.status(500).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;
|