Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var actors = []; //array mit actors
- var nextActorId = 1;
- function Actor (actorname, status, thumbnail ) { //erzeugen actoren mit constructor function
- this.id = nextActorId;
- this.actorname = actorname;
- this.status = status;
- this.thumbnail = thumbnail;
- nextActorId++;
- }
- var actorTemplate = new Actor(null, null, null);
- delete actorTemplate.id;
- /*function generateTestData () {
- var tmp = []; //temporaeres array
- tmp.push(new Actor('hansi', 'offline', null)); //erzeugen von Actoren (später Aus DB))
- tmp.push(new Actor('franzi', 'offline', null));
- tmp.push(new Actor('susi', 'offline', null));
- //actors[actorid] = actor;
- //actors[tmp[0].id] = tmp[0];
- for(var idx = 0; idx < tmp.length; idx++){ //einsortieren in array, id 1 ist auf Stelle 1
- actors[tmp[idx].id] = tmp[idx];
- }
- }*/
- function _get() {
- var result = [];
- for(var key in actors) { //einsortieren, dass es kein null gibt
- result.push(actors[key]);
- }
- return result;
- }
- function _getById(id) {
- if(actors[id] == undefined)
- throw new RangeError("actor not found"); //stack trace ist mit dabei //gibt jz neun Typen //Range Error: weil es in diesem Bereich nicht gibt
- return actors[id];
- }
- function _post(body) {
- for (var prop in body) {
- if (typeof(actorTemplate[prop]) === 'undefined') {
- throw new TypeError("Actor data invalid!" + body[prop] + " is missing");
- }
- }
- for (var prop in actorTemplate) {
- if (typeof(body[prop]) === 'undefined' && body[prop] != 'id') {
- throw new TypeError("Actor data invalid! " + actorTemplate[prop] + " is missing");
- }
- }
- if(actors.some((e) => { return e.actorname == body['actorname']; })) {
- throw new TypeError("Actorname " + body['actorname'] + " Not unique");
- }
- actors.push(new Actor(body.actorname, body.status, null));
- return 201;
- }
- function _put(id, body) {
- if (actors[id] === null || actors[id] === undefined)
- return 404;
- for (var prop in body) {
- if (typeof(actorTemplate[prop]) === 'undefined') {
- throw new TypeError("Actor data invalid!");
- }
- }
- for (var prop in body) {
- actors[id][prop] = body[prop];
- }
- return 202;
- }
- function _delete(id) {
- if(actors[id] == undefined)
- throw new RangeError("actor not found");
- delete actors[id];
- return 204;
- }
- //generateTestData();
- module.exports.get = _get;
- module.exports.post = _post;
- module.exports.put = _put;
- module.exports.delete = _delete;
- module.exports.getById = _getById;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement