forked from HerronTech/soajs.artifact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (83 loc) · 2.19 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict';
var soajs = require('soajs');
var config = require('./config.js');
var BLModule = require("./lib/index");
var service = new soajs.server.service(config);
function initBLModel(req, res, cb) {
var modelName = "mongo";
if(process.env.SOAJS_TEST && req.soajs.inputmaskData.model){
modelName = req.soajs.inputmaskData.model;
}
BLModule.init(modelName, function (error, BL) {
if (error) {
req.soajs.log.error(error);
return res.json(req.soajs.buildResponse({"code": 407, "msg": config.errors[407]}));
}
else {
return cb(BL);
}
});
}
service.init(function () {
/**
* Get one contact
*/
service.get("/contact/:id", function (req, res) {
initBLModel(req, res, function (BL) {
BL.getEntry(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
/**
* Search for contact
*/
service.get("/contact/match/:q", function (req, res) {
initBLModel(req, res, function (BL) {
BL.matchEntry(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
/**
* Get all Contacts
*/
service.get("/contacts", function (req, res) {
initBLModel(req, res, function (BL) {
BL.getEntries(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
/**
* Add one or more contacts
*/
service.post("/contact", function (req, res) {
initBLModel(req, res, function (BL) {
BL.addEntry(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
/**
* Update one contact
*/
service.put("/contact/:id", function (req, res) {
initBLModel(req, res, function (BL) {
BL.updateEntry(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
/**
* Delete one contact
*/
service.delete("/contact/:id", function (req, res) {
initBLModel(req, res, function (BL) {
BL.deleteEntry(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
service.start();
});