{"size": 2111, "ext": "js", "max_stars_repo_path": "api/labels.js", "max_stars_repo_name": "cuo9958/pub-server", "max_stars_repo_stars_event_min_datetime": "2019-08-20T05:34:07.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-01T09:36:10.000Z", "max_issues_repo_path": "api/labels.js", "max_issues_repo_name": "msdgwzhy6/pub-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/labels.js", "max_forks_repo_name": "msdgwzhy6/pub-server", "max_forks_repo_forks_event_min_datetime": "2019-08-20T05:34:13.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-05T02:37:05.000Z"}
{"max_stars_count": 11, "max_issues_count": null, "max_forks_count": 6, "avg_line_length": 23.1978021978, "max_line_length": 80, "alphanum_fraction": 0.4647086689}
const Router = require("koa-router");
const Service = require("pub-service");

const router = new Router();

router.get("/", async function(ctx, next) {
    try {
        const data = await Service.Labels.getAll();
        ctx.body = {
            status: 0,
            data: data
        };
    } catch (error) {
        ctx.body = {
            status: 1,
            msg: error.message
        };
    }
});
router.get("/pages", async function(ctx, next) {
    const { limit = 1 } = ctx.query;
    try {
        const data = await Service.Labels.getCount(limit);
        ctx.body = {
            status: 0,
            data: data
        };
    } catch (error) {
        ctx.body = {
            status: 1,
            msg: error.message
        };
    }
});
router.post("/add", async function(ctx, next) {
    const { title, attr_name, attr_val, attr_type = "text" } = ctx.request.body;
    if (!ctx.cookies.get("token")) {
        return (ctx.body = {
            status: 1,
            msg: "请登录"
        });
    }
    const model = {
        title,
        attr_name,
        attr_val,
        nickname: decodeURIComponent(ctx.cookies.get("nickname")),
        attr_type
    };
    console.log(model)
    try {
        const data = await Service.Labels.insert(model);
        ctx.body = {
            status: 0,
            data: data
        };
    } catch (error) {
        ctx.body = {
            status: 1,
            msg: error.message
        };
    }
});
router.post("/change/:id", async function(ctx, next) {
    const { status } = ctx.request.body;
    if (!ctx.cookies.get("token")) {
        return (ctx.body = {
            status: 1,
            msg: "请登录"
        });
    }
    const id = ctx.params.id;
    try {
        console.log(status);
        if (status == 1) {
            await Service.Labels.use(id);
        } else {
            await Service.Labels.del(id);
        }
        ctx.body = {
            status: 0
        };
    } catch (error) {
        ctx.body = {
            status: 1,
            msg: error.message
        };
    }
});
exports.routers = router.routes();
