index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // 添加账本
  2. import express from "express";
  3. const router = express.Router();
  4. import {
  5. getBookById,
  6. books_insert,
  7. delBookById,
  8. ishaveBookById,
  9. updateBookName,
  10. } from "#db";
  11. import {
  12. shanghaiTime,
  13. shanghaiTimeFormat
  14. } from '#utils'
  15. // middleware that is specific to this router
  16. router.use(function timeLog(req, res, next) {
  17. console.log("Time: ", Date.now());
  18. next();
  19. });
  20. // 获取账本详情
  21. router.get("/:book_id", async function (req, res) {
  22. // getBookById
  23. const book_id = req.params.book_id; // 获取 fileId 参数
  24. const bookInfo = await getBookById(book_id);
  25. // console.log(29, bookInfo)
  26. shanghaiTimeFormat
  27. bookInfo.create_time = shanghaiTimeFormat(bookInfo.create_time)
  28. bookInfo.update_time = shanghaiTimeFormat(bookInfo.update_time)
  29. res.json({
  30. code: 200,
  31. data: bookInfo,
  32. });
  33. // res.send("Books home page");
  34. });
  35. // 添加账本数据
  36. router.post("/", async function (req, res) {
  37. const { book_name = "", userInfo = {} } = req.body;
  38. // type 是否存在重复项,有就返回id,没有就新增 再返回id
  39. const isAddType = await ishaveBookById({
  40. book_name,
  41. author_id: userInfo.user_id,
  42. });
  43. if (isAddType) {
  44. res.status(500).json({
  45. code: 500,
  46. msg: "已存在重复数据",
  47. });
  48. return;
  49. }
  50. const insertId = await books_insert({
  51. book_name,
  52. author_id: userInfo.user_id,
  53. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  54. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  55. });
  56. res.json({
  57. code: 200,
  58. data: {
  59. book_id: insertId,
  60. },
  61. });
  62. });
  63. // 更新bookName
  64. router.put("/:book_id", async function (req, res) {
  65. const book_id = req.params.book_id; // 获取 fileId 参数
  66. const { book_name = ""} = req.body;
  67. await updateBookName({
  68. book_name,
  69. book_id,
  70. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  71. })
  72. res.json({
  73. code: 200,
  74. msg: "更新成功!",
  75. });
  76. });
  77. // 删除数据
  78. router.delete("/:book_id", async function (req, res) {
  79. delBookById;
  80. // getBookById
  81. const book_id = req.params.book_id; // 获取 fileId 参数
  82. await delBookById(book_id);
  83. res.json({
  84. code: 200,
  85. msg: "删除成功!",
  86. });
  87. });
  88. export default router;