import { Color } from "@/model/Color"; import { gFail, gSuccess } from "@/util"; import Joi from "joi"; import { auth, method, route, validate } from "@noderun/hapi-router"; export default class { index(request,h){ return h.view('views/color.pug') } @method("POST") @route("/add") @validate({ payload: Joi.object({ color: Joi.string().required(), title: Joi.string(), describe: Joi.string(), }), }) async add(request, h) { let { color, title, describe } = request.payload || {}; try { await Color.create({ color, title, describe }); return gSuccess("success"); } catch (error) { request.log("error", error); return gFail(null, "添加颜色失败"); } } @method("GET") @route("/all_color") async getAll(request, h) { try { let allColors = await Color.findAll(); if (allColors == null) { return gFail(null, "不存在颜色"); } return gSuccess(allColors, "success"); } catch (error) { request.log("error", error); return gFail(null, "获取颜色失败"); } } }