ssr_degrade.js 1.75 KB

const Router = require('koa-router');

const SsrDegradeModel = require('../models/ssrDegradeModel');

let r = new Router();

const ssrDegrade = {
  async index(ctx, next) {
    const ssrDegradeModel = new SsrDegradeModel();
    const apps = await ssrDegradeModel.getApps();

    return ctx.render('action/ssr_degrade', {
      list: apps || []
    });
  },
  async editPage(ctx, next) {
    const ssrDegradeModel = new SsrDegradeModel();
    const app = ctx.request.query.app;

    if (!app) {
      return ctx.render('action/ssr_degrade_edit', {
        data: {}
      });
    }
    const appData = await ssrDegradeModel.getApp(app);

    return ctx.render('action/ssr_degrade_edit', {
      data: appData
    });
  },
  async edit(ctx, next) {
    const ssrDegradeModel = new SsrDegradeModel();
    const data = ctx.request.body;

    if (!data.app || !Array.isArray(data.degrades)) {
      return ctx.body = {
        code: 400,
        message: '参数错误'
      };
    }
    const result = ssrDegradeModel.editApp(data);

    if (result) {
      return ctx.body = {
        code: 200,
      };
    }
    return ctx.body = {
      code: 500,
      message: '失败'
    };
  },
  async delete(ctx, next) {
    const ssrDegradeModel = new SsrDegradeModel();
    const app = ctx.request.query.app;

    if (!app) {
      return ctx.body = {
        code: 400,
        message: '参数错误'
      };
    }
    const result = ssrDegradeModel.deleteApp({app});

    if (result) {
      return ctx.body = {
        code: 200,
      };
    }
    return ctx.body = {
      code: 500,
      message: '失败'
    };
  }
};

r.get('/index', ssrDegrade.index);
r.get('/edit', ssrDegrade.editPage);
r.post('/edit', ssrDegrade.edit);
r.get('/delete', ssrDegrade.delete);

module.exports = r;