Merge pull request #6 from GotPPay/api-refactoring

Api refactoring
This commit is contained in:
MirnaM
2018-01-15 16:34:33 +01:00
committed by GitHub
4 changed files with 10 additions and 19 deletions

View File

@@ -15,7 +15,7 @@ constants.apiResultCodes = {
ok:0, ok:0,
amazonError:1, amazonError:1,
databaseError:2, databaseError:2,
IDLengthError:3,
} }
constants.skillIDLength = 24; constants.skillIDLength = 24;

View File

@@ -1,6 +1,5 @@
var express = require ('express'), router = express.Router (); var express = require ('express'), router = express.Router ();
router.use ('/getSkill', require ('./skill')); router.use ('/skill', require ('./skill'));
router.use ('/updateSkill', require ('./skill')); // this will be fixed in next code refactoring
module.exports = router; module.exports = router;

View File

@@ -22,7 +22,7 @@ router.get ('/:id', async (req, res, next) => {
} }
}); });
router.post ('/:id', bodyParser.json (), async (req, res, next) => { router.put ('/:id', bodyParser.json (), async (req, res, next) => {
let id = req.params.id; let id = req.params.id;
let dataFromWeb = JSON.stringify (req.body); let dataFromWeb = JSON.stringify (req.body);
let skill = JSON.parse (dataFromWeb); let skill = JSON.parse (dataFromWeb);
@@ -33,7 +33,9 @@ router.post ('/:id', bodyParser.json (), async (req, res, next) => {
console.log ('id = ' + id); console.log ('id = ' + id);
if (id !== '-1') { //TODO : Fix inconsistency ! If skill is sent to amazon and accepted, but
//fails in database (ID doesn't exist)
if (id.length === constants.skillIDLength) {
if (updateOnAmazon) { if (updateOnAmazon) {
amazonHelper amazonHelper
.updateSkill (skill) .updateSkill (skill)
@@ -84,7 +86,7 @@ router.post ('/:id', bodyParser.json (), async (req, res, next) => {
}); });
} }
} else { } else {
//no new skills for now res.json ({result: constants.IDLengthError, message: ''});
} }
}); });

View File

@@ -1,26 +1,16 @@
import fetch from 'isomorphic-fetch'; import fetch from 'isomorphic-fetch';
import {BASE_URL} from '../config/config'; import {BASE_URL} from '../config/config';
export const getAllIntents = (id)=>{
let url = `http://${BASE_URL}/intents/${id}`
return fetch(url, {method: 'GET'});
}
export const getSkill = (id)=>{ export const getSkill = (id)=>{
let url = `http://${BASE_URL}/getSkill/${id}` let url = `http://${BASE_URL}/skill/${id}`
return fetch(url, {method: 'GET'});
}
export const deleteSkill = (id)=>{
let url = `http://${BASE_URL}/deleteSkill/${id}`
return fetch(url, {method: 'GET'}); return fetch(url, {method: 'GET'});
} }
export const updateSkill = (skill)=>{ export const updateSkill = (skill)=>{
let id = (skill._id) ? skill._id : -1; let id = (skill._id) ? skill._id : -1;
let url = `http://${BASE_URL}/updateSkill/${id}` let url = `http://${BASE_URL}/skill/${id}`
return fetch(url, { return fetch(url, {
method: 'POST', method: 'PUT',
headers: { headers: {
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Type': 'application/json' 'Content-Type': 'application/json'