Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / framework / core / modules / api / navigation.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 /**
20 * navigation api module. Provides API functions to interact with
21 * the navigation_manager
22 * @module framework/core/modules/api/navigation
23 * @author Kiran Kashalkar <kiran.kashalkar@riftio.com>
24 */
25
26 var navigation_manager = require('../navigation_manager');
27 var Promise = require('promise');
28 var constants = require('../../api_utils/constants');
29 var navigationAPI = {};
30
31 /**
32 * Get the navigation object stored in this instance
33 * @param {Object} req - the Express request object with or without a plugin_id
34 * @return {Function} Promise that resolves with success object or rejects
35 * with error
36 */
37 navigationAPI.get = function(req) {
38 return new Promise(function(resolve, reject) {
39 if (req.params.plugin_id) {
40 var navigation = navigation_manager.getNavigation();
41 if (navigation[req.params.plugin_id]) {
42 resolve({
43 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK,
44 data: navigation[req.params.plugin_id]
45 });
46 } else {
47 reject({
48 statusCode: constants.HTTP_RESPONSE_CODES.ERROR.NOT_FOUND,
49 errorMessage: 'No navigation found for plugin_id ' + req.params.plugin_id
50 });
51 }
52 } else {
53 resolve({
54 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK,
55 data: navigation_manager.getNavigation()
56 });
57 }
58 });
59 };
60
61 /**
62 * Create the navigation object stored in this instance for a plugin.
63 * To be used across framework instances for informing each other of
64 * inter-instance navigation options.
65 * @param {Object} req - the Express request object
66 * @return {Function} Promise that resolves with success object or rejects
67 * with error
68 */
69 navigationAPI.post = function(req) {
70 return new Promise(function(resolve, reject) {
71 var plugin_nav = navigation_manager.getNavigation()[req.params.plugin_id] || {};
72 if (plugin_nav != {}) {
73 reject({
74 statusCode: constants.HTTP_RESPONSE_CODES.ERROR.CONFLICT,
75 errorMessage: 'Navigation for ' + req.params.plugin_id + ' already exist'
76 });
77 } else {
78 navigation_manager.addNavigation(req.params.plugin_id, req.body);
79 resolve({
80 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.CREATED,
81 data: {}
82 });
83 }
84 });
85 };
86
87 /**
88 * Update the navigation object stored in this instance for a plugin.
89 * @param {Object} req - the Express request object with a plugin_id and route_id
90 * @return {Function} Promise that resolves with success object or rejects
91 * with error
92 */
93 navigationAPI.put = function(req) {
94 return new Promise(function(resolve, reject) {
95 var plugin_nav = navigation_manager.getNavigation()[req.params.plugin_id]['routes'][req.params.route_id];
96 if (plugin_nav == null || plugin_nav == undefined) {
97 reject({
98 statusCode: constants.HTTP_RESPONSE_CODES.ERROR.BAD_REQUEST,
99 errorMessage: 'Navigation for route' + req.params.route_id + ' under plugin ' + req.params.plugin_id + ' does not exist'
100 });
101 } else {
102 navigation_manager.addNavigation(req.plugin_id, req.body);
103 resolve({
104 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK,
105 data: {}
106 });
107 }
108 });
109 };
110
111 /**
112 * Delete a particulat route navigation object stored in this instance for a plugin.
113 * @param {Object} req - the Express request object with a plugin_id and route_id
114 * @return {Function} Promise that resolves with success object or rejects
115 * with error
116 */
117 navigationAPI.delete = function(req) {
118 return new Promise(function(resolve, reject) {
119 var plugin_nav = navigation_manager.getNavigation()[req.params.plugin_id]['routes'[req.params.route_id]];
120 if (plugin_nav == null || plugin_nav == undefined) {
121 reject({
122 statusCode: constants.HTTP_RESPONSE_CODES.ERROR.BAD_REQUEST,
123 errorMessage: 'Navigation for route' + req.params.route_id + ' under plugin ' + req.params.plugin_id + ' does not exist'
124 });
125 } else {
126 navigation_manager.deleteNavigation(req.params.plugin_id, req.params.route_id);
127 resolve({
128 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK,
129 data: {}
130 });
131 }
132 });
133 };
134
135
136 module.exports = navigationAPI;