Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | angular.module('xos.UITutorial') |
| 4 | .service('ExploreCmd', function($injector, ResponseHandler, ErrorHandler){ |
| 5 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 6 | this.resourceExec = (cmd, args, done) => { |
| 7 | switch(args[0]){ |
| 8 | case 'list': |
| 9 | return this.listAvailableResources(done); |
| 10 | break; |
| 11 | default: |
| 12 | // use the resource |
| 13 | const resourceName = args.shift(); |
| 14 | const method = args.shift(); |
| 15 | return this.consumeResource(resourceName, method, args, done); |
| 16 | } |
| 17 | }; |
| 18 | |
| 19 | this.resourceCompletion = (cmd, arg, line, done) => { |
| 20 | const args = ['list'].concat(this.getAvailableResources()); |
| 21 | if(line.text.match(/resource\s[A-Z][a-z]+\s/)){ |
| 22 | // if arg is a resource, then return available methods |
| 23 | if(args.indexOf(arg) !== -1){ |
| 24 | arg = ''; |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 25 | } |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 26 | const methods = ['query', 'get', 'save', '$save', 'delete']; |
| 27 | return done(this.shell.bestMatch(arg, methods)); |
| 28 | } |
| 29 | return done(this.shell.bestMatch(arg, args)); |
| 30 | }; |
| 31 | |
| 32 | this.setup = (shell) => { |
| 33 | this.shell = shell; |
| 34 | shell.setCommandHandler('resource', { |
| 35 | exec: this.resourceExec, |
| 36 | completion: this.resourceCompletion |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 37 | }); |
| 38 | }; |
| 39 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 40 | this.getAvailableResources = () => { |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 41 | return angular.module('xos.helpers')._invokeQueue |
| 42 | .filter((d) => { |
| 43 | if(d[1] !== 'service'){ |
| 44 | return false; |
| 45 | } |
| 46 | const serviceDeps = d[2][1]; |
| 47 | return serviceDeps.indexOf('$resource') !== -1; |
| 48 | }) |
| 49 | .reduce((list, i) => list.concat([i[2][0]]), []); |
| 50 | } |
| 51 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 52 | this.listAvailableResources = (done) => { |
| 53 | // TODO use a template |
| 54 | const resources = this.getAvailableResources() |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 55 | .reduce((html, i) => `${html}${i}<br/>`, ''); |
| 56 | done(resources); |
| 57 | } |
| 58 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 59 | this.consumeResource = (resourceName, method, args, done) => { |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 60 | |
| 61 | // TODO if not resourceName/method print cmd instructions |
| 62 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 63 | if(this.getAvailableResources().indexOf(resourceName) === -1){ |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 64 | return ErrorHandler.print(`Resource "${resourceName}" does not exists`, done); |
| 65 | } |
| 66 | |
| 67 | if(['query', 'get', 'save', '$save', 'delete'].indexOf(method) === -1){ |
| 68 | return ErrorHandler.print(`Method "${method}" not allowed`, done); |
| 69 | } |
| 70 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 71 | // TODO @Teo if get/delete check for arguments |
| 72 | let params = {}; |
| 73 | |
| 74 | // if the method require arguments checks for them |
| 75 | if(['get', '$save', 'delete'].indexOf(method) !== -1){ |
| 76 | if(args.length === 0){ |
| 77 | return ErrorHandler.print(`Method "${method}" require parameters`, done); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // if there are arguments parse them |
| 82 | // TODO wrap in a try catch, we have no guarantee that a user insert the correct params |
| 83 | if(args.length > 0){ |
| 84 | params = eval(`(${args[0]})`); |
| 85 | } |
| 86 | |
| 87 | // if it is a query is not possible to use id as parameter |
| 88 | if(method === 'query' && angular.isDefined(params.id)){ |
| 89 | return ErrorHandler.print(`Is not possible to use "id" as filter in method "${method}", use "get" instead!`, done); |
| 90 | } |
| 91 | |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 92 | let Resource; |
| 93 | try{ |
| 94 | Resource = $injector.get(resourceName); |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 95 | Resource[method](params).$promise |
| 96 | .then(res => { |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 97 | const jsCode = `${resourceName}.${method}(${Object.keys(params).length > 0 ? JSON.stringify(params): ''})`; |
| 98 | return ResponseHandler.parse(res, jsCode, done); |
| 99 | }) |
| 100 | .catch(e => { |
| 101 | if(e.status === 404){ |
| 102 | return ErrorHandler.print(`${resourceName} with method "${method}" and parameters ${JSON.stringify(params)} ${e.data.detail}`, done); |
| 103 | } |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 104 | }); |
| 105 | } |
| 106 | catch(e){ |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 107 | console.error(e); |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 108 | return ErrorHandler.print(`Failed to inject resource "${resourceName}"`, done); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | }); |
| 113 | })(); |