-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapi.service.js
More file actions
66 lines (59 loc) · 2.27 KB
/
swapi.service.js
File metadata and controls
66 lines (59 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(function () {
var module = angular.module("plunker");
var swapiService = function ($http) {
var getType = function (type) {
if (type === "people") {
return $http.get("http://swapi.co/api/" + type).
then(function (response) {
return response.data;
});
} else {
throw "type not implemented. Use 'people'.";
}
}
var getPeople = function (type) {
return $http.get("http://swapi.co/api/" + type + "/").
then(function (response) {
return response.data.results.map(
function (row) {
row.mass = parseInt(row.mass);
row.height = parseInt(row.height);
return row;
}
);
});
}
var getUserDetails = function (type, id) {
return $http.get("http://swapi.co/api/" + type + "/"+id).
then(function (response) {
response.data.films = response.data.films.map(
function (row) {
let url = row;
row = {};
row.url = url;
return row;
}
);
let films = response.data.films;
if (films && Array.isArray(films)) {
angular.forEach(films,
function (val, key) {
let film = this;
$http.get(val.url)
.then(function (filmDetail) {
film[key] = filmDetail.data;
});
}, films
);
}
return response.data;
});
}
return {
getType: getType,
getPeople: getPeople,
getUserDetails: getUserDetails
}
}
module.factory("swapiService", swapiService);
})();