-
Notifications
You must be signed in to change notification settings - Fork 1
/
moduleService.js
68 lines (60 loc) · 1.57 KB
/
moduleService.js
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
67
68
const modelData = require("./data/modules.json")
const categoryData = require("./data/categories.json")
let models = []
let categories = []
function initialize() {
return new Promise((resolve, reject) => {
models = modelData // a whole step combining the two arrays by category ID
if (models) {
resolve("success")
} else {
reject("Error initializing models")
}
})
}
function getModels() {
return new Promise((resolve, reject) => {
if (models) {
resolve(models)
} else {
reject("No models available")
}
})
}
function getCategories() {
return new Promise((resolve, reject) => {
if (categoryData) {
categories = categoryData
resolve(categories)
} else {
reject("No categories available")
}
})
}
function getModelByID(id) {
return new Promise((resolve, reject) => {
let modelByID = models.find(model => model.id == id)
if (modelByID) {
resolve(modelByID)
} else {
reject("No model by that ID found")
}
})
}
function getModelByCategory(categoryID) {
return new Promise((resolve, reject) => {
let modelByCategory = models.filter(model => model.category == categoryID)
if (modelByCategory) {
resolve(modelByCategory)
} else {
reject("No model by that Category found")
}
})
}
module.exports = {
initialize,
getModels,
getModelByID,
getModelByCategory,
getCategories
}