# Code coverage report for thinkjs/lib/Lib/Extend/Controller/RestController.js

## Statements: 100% (45 / 45) Branches: 85.71% (12 / 14) Functions: 100% (21 / 21) Lines: 100% (45 / 45) Ignored: none

All files » thinkjs/lib/Lib/Extend/Controller/ » RestController.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102

    2

    2

    15

    15

    15

    15

    4
    4
    2
    1

    1

    1

    2
    1

    1

    3
    3
    3
    3
    3
    1

    2

    2

    1
    1

    3
    1

    2
    2
    2

    1

    1

    3
    1

    2
    2
    2
    2
    2
    1

    1

    1

    1
    1

    1

     /**
     * REST Controller
     * @return {[type]} [description]
     */
    module.exports = Controller(function(){
      'use strict';
      return {
        init: function(http){
          this.super('init', http);
          //资源名
          this.resource = this.get('resource');
          //资源id
          this.id = this.get('id') | 0;
          //实例化对应的模型
          this.model = D(this.resource);
        },
        /**
         * 获取
         * @return {[type]} [description]
         */
        getAction: function(){
          var self = this;
          if (this.id) {
            return getPromise(this.model.getPk()).then(function(pk){
              return self.model.where(getObject(pk, self.id)).find();
            }).then(function(data){
              return self.success(data);
            }).catch(function(err){
              return self.error(err.message);
            })
          }
          return this.model.select().then(function(data){
            return self.success(data);
          }).catch(function(err){
            return self.error(err.message);
          });
        },
        /**
         * 新建
         * @return {[type]} [description]
         */
        postAction: function(){
          var self = this;
          return getPromise(this.model.getPk()).then(function(pk){
            var data = self.post();
            delete data[pk];
            if (isEmpty(data)) {
              return self.error('data is empty');
            }
            return self.model.add(data);
          }).then(function(insertId){
            return self.success({id: insertId});
          }).catch(function(err){
            var msg = isError(err) ? err.message : err;
            return self.error(msg);
          });
        },
        /**
         * 删除
         * @return {[type]} [description]
         */
        deleteAction: function(){
          if (!this.id) {
            return this.error('params error');
          }
          var self = this;
          return getPromise(this.model.getPk()).then(function(pk){
            return self.model.where(getObject(pk, self.id)).delete();
          }).then(function(affectedRows){
            return self.success({affectedRows: affectedRows});
          }).catch(function(err){
            return self.error(err.message);
          });
        },
        /**
         * 更新
         * @return {[type]} [description]
         */
        putAction: function(){
          if (!this.id) {
            return this.error('params error');
          }
          var self = this;
          return getPromise(this.model.getPk()).then(function(pk){
            var data = self.post();
            delete data[pk];
            if (isEmpty(data)) {
              return self.error('data is empty');
            }
            return self.model.where(getObject(pk, self.id)).update(data);
          }).then(function(affectedRows){
            return self.success({affectedRows: affectedRows});
          }).catch(function(err){
            var msg = isError(err) ? err.message : err;
            return self.error(msg);
          });
        },
        __call: function(action){
          return this.error('action `' + action + '` is not allowed');
        }
      }
    })