blob: 0aba8fa2a568201671e02c6cb967ef47a8a0c780 [file] [log] [blame]
Matteo Scandolo86f3f282016-08-11 11:21:33 -07001(function () {
2 'use strict';
3 angular.module('xos.UITutorial')
4 .service('ExploreCmd', function($injector, ResponseHandler, ErrorHandler){
5
Matteo Scandoloef969922016-08-11 13:49:12 -07006 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 Scandolo86f3f282016-08-11 11:21:33 -070025 }
Matteo Scandoloef969922016-08-11 13:49:12 -070026 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 Scandolo86f3f282016-08-11 11:21:33 -070037 });
38 };
39
Matteo Scandoloef969922016-08-11 13:49:12 -070040 this.getAvailableResources = () => {
Matteo Scandolo86f3f282016-08-11 11:21:33 -070041 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 Scandoloef969922016-08-11 13:49:12 -070052 this.listAvailableResources = (done) => {
53 // TODO use a template
54 const resources = this.getAvailableResources()
Matteo Scandolo86f3f282016-08-11 11:21:33 -070055 .reduce((html, i) => `${html}${i}<br/>`, '');
56 done(resources);
57 }
58
Matteo Scandoloef969922016-08-11 13:49:12 -070059 this.consumeResource = (resourceName, method, args, done) => {
Matteo Scandolo86f3f282016-08-11 11:21:33 -070060
61 // TODO if not resourceName/method print cmd instructions
62
Matteo Scandoloef969922016-08-11 13:49:12 -070063 if(this.getAvailableResources().indexOf(resourceName) === -1){
Matteo Scandolo86f3f282016-08-11 11:21:33 -070064 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 Scandoloef969922016-08-11 13:49:12 -070071 // 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 Scandolo86f3f282016-08-11 11:21:33 -070092 let Resource;
93 try{
94 Resource = $injector.get(resourceName);
Matteo Scandolo86f3f282016-08-11 11:21:33 -070095 Resource[method](params).$promise
96 .then(res => {
Matteo Scandoloef969922016-08-11 13:49:12 -070097 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 Scandolo86f3f282016-08-11 11:21:33 -0700104 });
105 }
106 catch(e){
Matteo Scandoloef969922016-08-11 13:49:12 -0700107 console.error(e);
Matteo Scandolo86f3f282016-08-11 11:21:33 -0700108 return ErrorHandler.print(`Failed to inject resource "${resourceName}"`, done);
109 }
110 };
111
112 });
113})();