{"size": 1583, "ext": "js", "max_stars_repo_path": "06-web应用架构+应用模型介绍/演示代码/02-前后端分离架构案例演示/server/routers/bwlRouter.js", "max_stars_repo_name": "Like-H5/01---H5--", "max_stars_repo_stars_event_min_datetime": "2021-07-19T03:33:12.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-05T07:56:39.000Z", "max_issues_repo_path": "06-web应用架构+应用模型介绍/演示代码/02-前后端分离架构案例演示/server/routers/bwlRouter.js", "max_issues_repo_name": "Like-H5/01---H5--", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "06-web应用架构+应用模型介绍/演示代码/02-前后端分离架构案例演示/server/routers/bwlRouter.js", "max_forks_repo_name": "Like-H5/01---H5--", "max_forks_repo_forks_event_min_datetime": "2021-10-20T02:20:46.000Z", "max_forks_repo_forks_event_max_datetime": "2021-10-20T02:20:46.000Z"}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 22.2957746479, "max_line_length": 66, "alphanum_fraction": 0.5742261529}
const express = require('express');
const {getCurrentTime} = require("../tool/sz_dateTime")
const data = require("../data/data.json")
const fs = require("fs");

let router = express.Router();




router.post("/add", (req, resp) => {
    const {title, content} = req.body;
    let id = Date.now() + Math.random() + Math.random() + ""
    let currentTime = getCurrentTime()

    let item = {
        id,
        currentTime,
        title,
        content
    }
    data.push(item)
    fs.writeFile("./data/data.json", JSON.stringify(data), () => {
        console.log("追加数据成功")
    })

    resp.send(resp.tool.resultTemp(0, "添加成功", item))

})

router.get("/list", (req, resp)=>{
    resp.send(resp.tool.resultTemp(0, "查询结果成功", data))
})

router.get("/detail/:id", (req, resp) => {
    const {id} = req.params;
    let item = data.find(item=>item.id === id)
    resp.send(resp.tool.resultTemp(0, "查找成功", item))
})

router.get("/del", (req, resp)=>{
    const {id} = req.query;
    let idx = data.findIndex(item=>item.id === id);
    data.splice(idx, 1);

    fs.writeFile("./data/data.json", JSON.stringify(data), () => {
        console.log("追加数据成功")
    })
    resp.send(resp.tool.resultTemp(0, "删除成功", {}))
})

router.post("/edit", (req, resp) => {

    const {id, title, content} = req.body;

    let item = data.find(item=>item.id === id)
    item.title = title;
    item.content = content;

    fs.writeFile("./data/data.json", JSON.stringify(data), () => {
        console.log("追加数据成功")
    })

    resp.send(resp.tool.resultTemp(0, "修改成功", {}))


})



module.exports = router;