3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
26 var navigation_manager
= require('../navigation_manager');
27 var Promise
= require('promise');
28 var constants
= require('../../api_utils/constants');
29 var navigationAPI
= {};
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
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
]) {
43 statusCode
: constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
,
44 data
: navigation
[req
.params
.plugin_id
]
48 statusCode
: constants
.HTTP_RESPONSE_CODES
.ERROR
.NOT_FOUND
,
49 errorMessage
: 'No navigation found for plugin_id ' + req
.params
.plugin_id
54 statusCode
: constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
,
55 data
: navigation_manager
.getNavigation()
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
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
!= {}) {
74 statusCode
: constants
.HTTP_RESPONSE_CODES
.ERROR
.CONFLICT
,
75 errorMessage
: 'Navigation for ' + req
.params
.plugin_id
+ ' already exist'
78 navigation_manager
.addNavigation(req
.params
.plugin_id
, req
.body
);
80 statusCode
: constants
.HTTP_RESPONSE_CODES
.SUCCESS
.CREATED
,
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
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) {
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'
102 navigation_manager
.addNavigation(req
.plugin_id
, req
.body
);
104 statusCode
: constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
,
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
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) {
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'
126 navigation_manager
.deleteNavigation(req
.params
.plugin_id
, req
.params
.route_id
);
128 statusCode
: constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
,
136 module
.exports
= navigationAPI
;