1 | var querystring = require('querystring'),
|
2 | Url = require('url'),
|
3 | _ = require('underscore'),
|
4 | forms = require('j-forms').forms;
|
5 |
|
6 | var MongooseAdmin;
|
7 |
|
8 | exports.setAdmin = function(admin){
|
9 | MongooseAdmin = admin;
|
10 | };
|
11 |
|
12 |
|
13 | exports.login = function(req, res) {
|
14 | MongooseAdmin.singleton.login(req.body.username, req.body.password, function(err, adminUser) {
|
15 | if (err) {
|
16 | res.writeHead(500);
|
17 | res.end();
|
18 | } else {
|
19 | if (!adminUser) {
|
20 | res.writeHead(401);
|
21 | res.end();
|
22 | } else {
|
23 | req.session._mongooseAdminUser = adminUser.toSessionStore();
|
24 | res.writeHead(200, {"Content-Type": "application/json"});
|
25 | res.write("{}");
|
26 | res.end();
|
27 | }
|
28 | }
|
29 | });
|
30 | };
|
31 |
|
32 | exports.documents = function(req, res) {
|
33 | var adminUser = req.session._mongooseAdminUser ? MongooseAdmin.userFromSessionStore(req.session._mongooseAdminUser) : null;
|
34 | if (!adminUser) {
|
35 | res.writeHead(401, {"Content-Type": "application/json"});
|
36 | res.end();
|
37 | return;
|
38 | } else {
|
39 | var query = querystring.parse(Url.parse(req.url).query);
|
40 | MongooseAdmin.singleton.modelCounts(query.collection, function(err, totalCount) {
|
41 | if (err) {
|
42 | res.writeHead(500);
|
43 | res.end();
|
44 | } else {
|
45 | MongooseAdmin.singleton.listModelDocuments(query.collection, query.start, query.count, function(err, documents) {
|
46 | if (err) {
|
47 | res.writeHead(500);
|
48 | res.end();
|
49 | } else {
|
50 | res.writeHead(200, {"Content-Type": "application/json"});
|
51 | res.write(JSON.stringify({'totalCount': totalCount, 'documents': documents}));
|
52 | res.end();
|
53 | }
|
54 | });
|
55 | }
|
56 | });
|
57 | }
|
58 | };
|
59 |
|
60 | exports.checkDependencies = function(req,res)
|
61 | {
|
62 | var modelName = req.body.model;
|
63 | var id = req.body.id;
|
64 | forms.checkDependecies(modelName,id,function(err,results)
|
65 | {
|
66 | var json = _.map(results,function(result) {
|
67 | return result.name || result.title || result.toString();
|
68 | });
|
69 | res.json(json,200);
|
70 | });
|
71 | };
|
72 |
|
73 | exports.createDocument = function(req, res) {
|
74 | var adminUser = req.session._mongooseAdminUser ? MongooseAdmin.userFromSessionStore(req.session._mongooseAdminUser) : null;
|
75 | if (!adminUser) {
|
76 | res.writeHead(401, {"Content-Type": "application/json"});
|
77 | res.end();
|
78 | return;
|
79 | } else {
|
80 | MongooseAdmin.singleton.createDocument(req,adminUser, req.params.collectionName, req.body, function(err, document) {
|
81 | if (err) {
|
82 | if(typeof(err)=='object')
|
83 | {
|
84 | res.json(err,400);
|
85 | }
|
86 | else
|
87 | {
|
88 | res.writeHead(500);
|
89 | res.end();
|
90 | }
|
91 | } else {
|
92 | res.writeHead(201, {"Content-Type": "application/json"});
|
93 | res.write(JSON.stringify({"collection": req.params.collectionName}));
|
94 | res.end();
|
95 | }
|
96 | });
|
97 | }
|
98 | };
|
99 |
|
100 | exports.updateDocument = function(req, res) {
|
101 | var adminUser = req.session._mongooseAdminUser ? MongooseAdmin.userFromSessionStore(req.session._mongooseAdminUser) : null;
|
102 | if (!adminUser) {
|
103 | res.writeHead(401, {"Content-Type": "application/json"});
|
104 | res.end();
|
105 | return;
|
106 | } else {
|
107 | MongooseAdmin.singleton.updateDocument(req,adminUser, req.params.collectionName, req.body._id, req.body, function(err, document) {
|
108 | if (err) {
|
109 | res.writeHead(500);
|
110 | res.end();
|
111 | } else {
|
112 | res.writeHead(200, {"Content-Type": "application/json"});
|
113 | res.write(JSON.stringify({"collection": req.params.collectionName}));
|
114 | res.end();
|
115 | }
|
116 | });
|
117 | }
|
118 | };
|
119 |
|
120 | exports.orderDocuments = function(req,res)
|
121 | {
|
122 | var adminUser = req.session._mongooseAdminUser ? MongooseAdmin.userFromSessionStore(req.session._mongooseAdminUser) : null;
|
123 | if (!adminUser) {
|
124 | res.writeHead(401, {"Content-Type": "application/json"});
|
125 | res.end();
|
126 | return;
|
127 | } else {
|
128 | MongooseAdmin.singleton.orderDocuments(adminUser, req.params.collectionName, req.body, function(err) {
|
129 | if (err) {
|
130 | res.writeHead(500);
|
131 | res.end();
|
132 | } else {
|
133 | res.writeHead(200, {"Content-Type": "application/json"});
|
134 | res.write(JSON.stringify({"collection": req.params.collectionName}));
|
135 | res.end();
|
136 | }
|
137 | });
|
138 | }
|
139 | };
|
140 |
|
141 | exports.actionDocuments = function(req,res)
|
142 | {
|
143 | var adminUser = req.session._mongooseAdminUser ? MongooseAdmin.userFromSessionStore(req.session._mongooseAdminUser) : null;
|
144 | if (!adminUser) {
|
145 | res.writeHead(401, {"Content-Type": "application/json"});
|
146 | res.end();
|
147 | return;
|
148 | } else {
|
149 | MongooseAdmin.singleton.actionDocuments(adminUser, req.params.collectionName,req.params.actionId, req.body, function(err) {
|
150 | if (err) {
|
151 | res.writeHead(500);
|
152 | res.end();
|
153 | } else {
|
154 | res.writeHead(200, {"Content-Type": "application/json"});
|
155 | res.write(JSON.stringify({"collection": req.params.collectionName}));
|
156 | res.end();
|
157 | }
|
158 | });
|
159 | }
|
160 | }
|
161 |
|
162 | exports.deleteDocument = function(req, res) {
|
163 | var adminUser = req.session._mongooseAdminUser ? MongooseAdmin.userFromSessionStore(req.session._mongooseAdminUser) : null;
|
164 | if (!adminUser) {
|
165 | res.writeHead(401, {"Content-Type": "application/json"});
|
166 | res.end();
|
167 | return;
|
168 | } else {
|
169 | var query = querystring.parse(Url.parse(req.url).query);
|
170 | MongooseAdmin.singleton.deleteDocument(adminUser, req.params.collectionName, query.document_id, function(err) {
|
171 | if (err) {
|
172 | res.writeHead(500);
|
173 | res.end();
|
174 | } else {
|
175 | res.writeHead(200, {"Content-Type": "application/json"});
|
176 | res.write(JSON.stringify({"collection": req.params.collectionName}));
|
177 | res.end();
|
178 | }
|
179 | });
|
180 | }
|
181 | };
|
182 |
|
183 | exports.linkedDocumentsList = function(req, res) {
|
184 | var adminUser = req.session._mongooseAdminUser ? MongooseAdmin.userFromSessionStore(req.session._mongooseAdminUser) : null;
|
185 | if (!adminUser) {
|
186 | res.writeHead(401, {"Content-Type": "application/json"});
|
187 | res.end();
|
188 | return;
|
189 | } else {
|
190 | MongooseAdmin.singleton.getModel(req.params.collectionName, function(err, model, fields, options) {
|
191 | if (err) {
|
192 | res.writeHead(500);
|
193 | res.end();
|
194 | } else {
|
195 | MongooseAdmin.singleton.listModelDocuments(req.params.collectionName, 0, 500, function(err, documents) {
|
196 | if (err) {
|
197 | res.writeHead(500);
|
198 | res.end();
|
199 | } else {
|
200 | var result = [];
|
201 | documents.forEach(function(document) {
|
202 | var d = {'_id': document._id};
|
203 | options.list.forEach(function(listField) {
|
204 | d[listField] = document[listField];
|
205 | });
|
206 | result.push(d);
|
207 | });
|
208 |
|
209 | res.writeHead(200, {"Content-Type": "application/json"});
|
210 | res.write(JSON.stringify(result));
|
211 | res.end();
|
212 | }
|
213 | });
|
214 | }
|
215 | });
|
216 | }
|
217 | }
|