Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var actors = [];    //array mit actors
  2. var nextActorId = 1;
  3.  
  4. function Actor (actorname, status, thumbnail ) {  //erzeugen actoren mit constructor function
  5.     this.id = nextActorId;
  6.     this.actorname = actorname;
  7.     this.status = status;
  8.     this.thumbnail = thumbnail;
  9.     nextActorId++;
  10. }  
  11. var actorTemplate = new Actor(null, null, null);
  12. delete actorTemplate.id;
  13.  
  14.  
  15. /*function generateTestData () {
  16.     var tmp = [];       //temporaeres array
  17.     tmp.push(new Actor('hansi', 'offline', null));   //erzeugen von Actoren (später Aus DB))
  18.     tmp.push(new Actor('franzi', 'offline', null));
  19.     tmp.push(new Actor('susi', 'offline', null));
  20.  
  21.     //actors[actorid] = actor;
  22.     //actors[tmp[0].id] = tmp[0];
  23.  
  24.     for(var idx = 0; idx < tmp.length; idx++){      //einsortieren in array, id 1 ist auf Stelle 1
  25.         actors[tmp[idx].id] = tmp[idx];
  26.     }
  27. }*/
  28.  
  29. function _get() {
  30.     var result = [];
  31.     for(var key in actors) {        //einsortieren, dass es kein null gibt
  32.         result.push(actors[key]);
  33.     }
  34.  
  35.     return result;
  36. }
  37.  
  38. function _getById(id) {
  39.     if(actors[id] == undefined)
  40.         throw new RangeError("actor not found");     //stack trace ist mit dabei //gibt jz neun Typen   //Range Error: weil es in diesem Bereich nicht gibt
  41.    
  42.     return actors[id];
  43. }
  44.  
  45. function _post(body) {
  46.     for (var prop in body) {
  47.         if (typeof(actorTemplate[prop]) === 'undefined') {
  48.             throw new TypeError("Actor data invalid!" + body[prop] + " is missing");
  49.         }
  50.     }
  51.  
  52.     for (var prop in actorTemplate) {
  53.         if (typeof(body[prop]) === 'undefined' && body[prop] != 'id') {
  54.             throw new TypeError("Actor data invalid! " + actorTemplate[prop] + " is missing");
  55.         }
  56.     }
  57.  
  58.     if(actors.some((e) => { return e.actorname == body['actorname']; })) {
  59.         throw new TypeError("Actorname " + body['actorname'] + " Not unique");
  60.     }
  61.  
  62.     actors.push(new Actor(body.actorname, body.status, null));
  63.     return 201;
  64.  
  65. }
  66.  
  67. function _put(id, body) {
  68.    
  69.     if (actors[id] === null || actors[id] === undefined)
  70.         return 404;
  71.    
  72.     for (var prop in body) {
  73.         if (typeof(actorTemplate[prop]) === 'undefined') {
  74.             throw new TypeError("Actor data invalid!");
  75.         }
  76.     }
  77.  
  78.     for (var prop in body) {
  79.         actors[id][prop] = body[prop];
  80.     }
  81.  
  82.     return 202;
  83. }
  84.  
  85. function _delete(id) {
  86.     if(actors[id] == undefined)
  87.         throw new RangeError("actor not found");
  88.    
  89.     delete actors[id];
  90.  
  91.     return 204;
  92. }
  93.  
  94.  
  95. //generateTestData();
  96. module.exports.get = _get;
  97. module.exports.post = _post;
  98. module.exports.put = _put;
  99. module.exports.delete = _delete;
  100. module.exports.getById = _getById;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement