diff --git a/lib/server.js b/lib/server.js index e1d0c75..61f799c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,5 +1,5 @@ (function() { - var AdmZip, DEFAULT_LIMIT, GridStore, MongoClient, api_root, app, bodyParser, check_archive, compression, content_type, express, fs, get_from_gs, get_zipped, methodOverride, mongo_url, mongodb, mongodb_credential, mongodb_host, mongodb_port, morgan, repo_backend, request, upload_content, upload_content_data, version, yaml, zlib; + var AdmZip, DEFAULT_LIMIT, GridStore, MongoClient, api_root, app, bodyParser, check_archive, compression, content_type, express, fs, get_from_gs, get_zipped, methodOverride, mongo_url, mongodb, mongodb_credential, mongodb_host, mongodb_port, morgan, re_or_str, repo_backend, request, upload_content, upload_content_data, version, yaml, zlib; fs = require('fs'); @@ -109,18 +109,27 @@ }); }; + re_or_str = function(src) { + console.log(src); + console.log(src[0]); + console.log(src[-1]); + if (src[0] === '/' && src.slice(-1) === '/') { + return { + "$in": [new RegExp(src.slice(1, -1))] + }; + } else { + return src; + } + }; + app.route(api_root + '/books').get(function(req, res) { var options, query; query = {}; if (req.query.title) { - query['title'] = { - "$in": [new RegExp(req.query.title)] - }; + query['title'] = re_or_str(req.query.title); } if (req.query.author) { - query['authors.full_name'] = { - "$in": [new RegExp(req.query.author)] - }; + query['authors.full_name'] = re_or_str(req.query.author); } if (req.query.after) { query['release_date'] = { @@ -320,9 +329,7 @@ var query; query = {}; if (req.query.name) { - query['full_name'] = { - "$in": [new RegExp(req.query.name)] - }; + query['full_name'] = re_or_str(req.query.name); } return app.my.persons.find(query, { _id: 0 @@ -360,9 +367,7 @@ var query; query = {}; if (req.query.name) { - query.name = { - "$in": [new RegExp(req.query.name)] - }; + query.name = re_or_str(req.query.name); } return app.my.workers.find(query, { _id: 0 diff --git a/src/server.coffee b/src/server.coffee index df4ef59..46cb0a6 100644 --- a/src/server.coffee +++ b/src/server.coffee @@ -90,13 +90,19 @@ upload_content_data = (db, book_id, source, cb)-> gs.close (err)-> cb err +re_or_str = (src)-> + if src[0] is '/' and src[-1..] is '/' + return {"$in": [new RegExp src[1...-1]]} + else + return src + app.route api_root + '/books' .get (req, res)-> query = {} if req.query.title - query['title'] = {"$in": [new RegExp req.query.title]} + query['title'] = re_or_str req.query.title if req.query.author - query['authors.full_name'] = {"$in": [new RegExp req.query.author]} + query['authors.full_name'] = re_or_str req.query.author if req.query.after query['release_date'] = {"$gte": new Date (req.query.after)} options = @@ -238,7 +244,8 @@ app.route api_root + '/persons' .get (req, res)-> query = {} if req.query.name - query['full_name'] = {"$in": [new RegExp req.query.name]} + query['full_name'] = re_or_str req.query.name + app.my.persons.find query, {_id: 0}, (err, items)-> items.toArray (err, docs)-> if err @@ -265,7 +272,8 @@ app.route api_root + '/workers' .get (req, res)-> query = {} if req.query.name - query.name = {"$in": [new RegExp req.query.name]} + query.name = re_or_str req.query.name + app.my.workers.find query, {_id: 0}, (err, items)-> items.toArray (err, docs)-> if err