controller.util.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * 控制器辅助函数。
  3. *
  4. * author: Sand
  5. * since: 2016/11/22
  6. */
  7. "use strict";
  8. const MODEL_EVENTS = require("../include/commons").MODEL_EVENTS;
  9. /**
  10. * 根据模型事件注册响应返回的代码。
  11. */
  12. class ControllerUtil {
  13. constructor(){}
  14. static regModelEventHandler(model, response) {
  15. model.on(MODEL_EVENTS.OK, function (data) {
  16. response.status(200).json(data);
  17. });
  18. model.on(MODEL_EVENTS.DataNotFound, function (data) {
  19. response.status(404).json(data);
  20. });
  21. model.on(MODEL_EVENTS.Error, function (data) {
  22. response.status(500).json(data);
  23. });
  24. }
  25. static checkRequestQueryParams(req, params){
  26. let message = "Missing field(s): ";
  27. let missingField = false;
  28. params.forEach(function (param) {
  29. if(!req.query.hasOwnProperty(param)){
  30. message += param + ",";
  31. missingField = true;
  32. }
  33. });
  34. if(missingField) throw {message: message};
  35. }
  36. }
  37. module.exports = ControllerUtil;