Skip to content

Commit c8421f2

Browse files
authored
Merge pull request #21 from hyperstudio/bs-newschema
Bs newschema
2 parents bebb865 + d51a4b8 commit c8421f2

File tree

2 files changed

+78
-34
lines changed

2 files changed

+78
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "MIT-Annotation-Data-Store",
2+
"name": "mit-annotation-data-store",
33
"version": "0.1.6",
44
"dependencies": {
55
"errorhandler": "^1.5.1",

web.js

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
// Setup
2+
3+
const uriFormat = require('mongodb-uri')
4+
function encodeMongoURI (urlString) {
5+
if (urlString) {
6+
let parsed = uriFormat.parse(urlString)
7+
urlString = uriFormat.format(parsed);
8+
}
9+
return urlString;
10+
};
11+
12+
213
var application_root = __dirname,
314
secret = process.env.SECRET,
415
port = process.env.PORT,
5-
db = process.env.DB,
16+
db = encodeMongoURI(process.env.DB),
617
consumer = process.env.CONSUMER,
718
version = process.env.VERSION,
819
path = require("path"),
@@ -30,6 +41,7 @@ var allowCrossDomain = function(req, res, next) {
3041
}
3142
};
3243

44+
3345
// Schemas
3446
var Schema = mongoose.Schema;
3547

@@ -131,7 +143,7 @@ var Annotation = new Schema({
131143
required: false
132144
},
133145
groups: [String],
134-
subgroups: [String],
146+
group_ids: [Number],
135147
ranges: [Ranges],
136148
tags: [String],
137149
permissions: {
@@ -144,11 +156,33 @@ var Annotation = new Schema({
144156
sort_position: {
145157
type: String,
146158
required: false
159+
},
160+
doc_title: {
161+
type: String,
162+
required: false
163+
},
164+
doc_author: {
165+
type: String,
166+
required: false
167+
},
168+
doc_date: {
169+
type: String,
170+
required: false
171+
},
172+
doc_publisher: {
173+
type: String,
174+
required: false
175+
},
176+
doc_edition: {
177+
type: String,
178+
required: false
179+
},
180+
doc_source: {
181+
type: String,
182+
required: false
147183
}
148184
});
149185

150-
var AnnotationModel = mongoose.model('Annotation', Annotation);
151-
152186
// DB
153187
mongoose.connect(db);
154188

@@ -159,13 +193,27 @@ app.use(express.urlencoded({
159193
}));
160194
app.use(express.json());
161195
app.use(methodOverride());
162-
196+
app.use(lessMiddleware(__dirname + '/public', {
197+
render:{
198+
compress: true
199+
}
200+
}));
201+
202+
app.use(express.static(path.join(application_root, "public")));
203+
app.use(errorhandler({
204+
dumpExceptions: true,
205+
showStack: true
206+
}));
207+
163208

164209
Annotation.pre('save', function(next) {
165210
this.id = this._id;
166211
next();
167212
});
168213

214+
var AnnotationModel = mongoose.model('Annotation', Annotation);
215+
216+
169217
// ROUTES
170218
app.get('/api', function(req, res) {
171219
res.send('Annotations API is running');
@@ -209,18 +257,6 @@ app.get('/api/search', tokenOK, function(req, res) {
209257
case 'user':
210258
query.where('user').equals(req.query.user);
211259
break;
212-
case 'group':
213-
if(req.query.subgroups){
214-
var clean_subgroups = req.query.subgroups;
215-
var index = clean_subgroups.indexOf("");
216-
if(index > -1) clean_subgroups.splice(index, 1);
217-
query.where('subgroups'). in (clean_subgroups);
218-
}
219-
else{
220-
query.where('subgroups'). in ([]);
221-
}
222-
query.$where('this.permissions.read.length < 1');
223-
break;
224260
case 'class':
225261
if(req.query.groups){
226262
var clean_groups = req.query.groups;
@@ -233,6 +269,15 @@ app.get('/api/search', tokenOK, function(req, res) {
233269
}
234270
query.$where('this.permissions.read.length < 1');
235271
break;
272+
case 'groupId':
273+
if(req.query.group_ids){
274+
query.where('group_ids'). in (req.query.group_ids);
275+
}
276+
else{
277+
query.where('group_ids'). in ([]);
278+
}
279+
query.$where('this.permissions.read.length < 1');
280+
break;
236281
case 'admin':
237282
query.$where('this.permissions.read.length < 1');
238283
break;
@@ -270,7 +315,6 @@ app.get('/api/search', tokenOK, function(req, res) {
270315
else {
271316
query.exec(function(err, annotations) {
272317
if (!err) {
273-
// console.info(annotations);
274318
if (annotations.length > 0) {
275319
return res.send({
276320
'rows': annotations
@@ -328,14 +372,20 @@ app.post('/api/annotations', tokenOK, function(req, res) {
328372
quote: req.body.quote,
329373
tags: req.body.tags,
330374
groups: req.body.groups,
331-
subgroups: req.body.subgroups,
375+
group_ids: req.body.group_ids,
332376
uuid: req.body.uuid,
333377
parentIndex: req.body.parentIndex,
334378
ranges: req.body.ranges,
335379
shapes: req.body.shapes,
336380
permissions: req.body.permissions,
337381
annotation_categories: req.body.annotation_categories,
338-
sort_position: req.body.sort_position
382+
sort_position: req.body.sort_position,
383+
doc_title: req.body.doc_title,
384+
doc_author: req.body.doc_author,
385+
doc_date: req.body.doc_date,
386+
doc_publisher: req.body.doc_publisher,
387+
doc_edition: req.body.doc_edition,
388+
doc_source: req.body.doc_source
339389
});
340390

341391
annotation.save(function(err) {
@@ -375,13 +425,19 @@ app.put('/api/annotations/:id', tokenOK, function(req, res) {
375425
annotation.quote = req.body.quote;
376426
annotation.tags = req.body.tags;
377427
annotation.groups = req.body.groups;
378-
annotation.subgroups = req.body.subgroups;
428+
annotation.group_ids = req.body.group_ids;
379429
annotation.uuid = req.body.uuid;
380430
annotation.parentIndex = req.body.parentIndex;
381431
annotation.ranges = req.body.ranges;
382432
annotation.permissions = req.body.permissions;
383433
annotation.annotation_categories = req.body.annotation_categories;
384434
annotation.sort_position = req.body.sort_position;
435+
annotation.doc_title = req.body.doc_title;
436+
annotation.doc_author = req.body.doc_author;
437+
annotation.doc_date = req.body.doc_date;
438+
annotation.doc_publisher = req.body.doc_publisher;
439+
annotation.doc_edition = req.body.doc_edition;
440+
annotation.doc_source = req.body.doc_source;
385441

386442
return annotation.save(function(err) {
387443
if (!err) {
@@ -408,18 +464,6 @@ app.delete('/api/annotations/:id', tokenOK, function(req, res) {
408464
});
409465
});
410466

411-
// Middleware config
412-
app.use(lessMiddleware(__dirname + '/public', {
413-
render:{
414-
compress: true
415-
}
416-
}));
417-
418-
app.use(express.static(path.join(application_root, "public")));
419-
app.use(errorhandler({
420-
dumpExceptions: true,
421-
showStack: true
422-
}));
423467

424468
// Authentication
425469
function tokenOK(req, res, next) {

0 commit comments

Comments
 (0)