diff --git a/client/client.coffee b/client/client.coffee index bb1c08c..e998a47 100644 --- a/client/client.coffee +++ b/client/client.coffee @@ -1,5 +1,5 @@ # Homework - Client Side -version = "1.2" +version = "1.3" # Utilities tick = new Tracker.Dependency() Meteor.setInterval (-> tick.changed();), 15000 @@ -132,7 +132,6 @@ Router.route '/archive/:_id?', waitOn: -> @notes = Meteor.subscribe 'notes', yes onStop: -> @notes.stop() controller: loggedInController -Router.route '/(.*)', -> @render '404' # Catch-all route # Client Templates diff --git a/server/api.coffee b/server/api.coffee new file mode 100644 index 0000000..1380762 --- /dev/null +++ b/server/api.coffee @@ -0,0 +1,67 @@ +notes = share.notes = new Mongo.Collection 'notes' + +if !Meteor.settings.enableAPI? then return + +console.log 'RESTful HTTP API enabled' + +apiKeyToUser = (key) -> Meteor.users.findOne apiKey: key +respond = (res, code, obj) -> + res.writeHead code, 'Content-Type': 'application/json' + res.end JSON.stringify obj + +# GET NOTES +Router.route '/api/:key', where: 'server' + .get -> + user = apiKeyToUser @params.key + if !user + respond @response, 400, error: 'invalid api key' + else + respond @response, 200, notes.find( + { userId: user._id, archived: no }, + { fields: { archived: 0, userId: 0 }, sort: { date: 1 }}).fetch() + +# GET ARCHIVE +Router.route '/api/:key/archived', where: 'server' + .get -> + user = apiKeyToUser @params.key + if !user + respond @response, 400, error: 'invalid api key' + else + respond @response, 200, notes.find( + { userId: user._id, archived: yes }, + { fields: { archived: 0, userId: 0 }, sort: { date: 1 }}).fetch() + +# BACKUP +Router.route '/api/:key/backup', where: 'server' + .get -> + user = apiKeyToUser @params.key + if !user + respond @response, 400, error: 'invalid api key' + else + respond @response, 200, notes.find(userId: user._id).fetch() + +# RESTORE to be implemented + +# INSERT NOTE +Router.route '/api/:key/:title/:desc', where: 'server' + .post -> + user = apiKeyToUser @params.key + if !user + respond @response, 400, error: 'invalid api key' + else + notes.insert { + userId: user._id + title: @params.title + content: @params.desc + date: no + archived: no + }, (e) => respond @response, (if e then 500 else 200), e or {} + +# DELETE NOTE +Router.route '/api/:id', where: 'server' + .delete -> + user = apiKeyToUser @params.key + if !user + respond @response, 400, error: 'invalid api key' + else + notes.remove @params.id, (e) => respond @response, (if e then 500 else 200), e or {} diff --git a/server/server.coffee b/server/server.coffee index a5d3d71..ddcb4d2 100644 --- a/server/server.coffee +++ b/server/server.coffee @@ -1,12 +1,11 @@ # Homework - Server Side -notes = share.notes console.log "Started Homework server!" if process.env.MAIL_URL console.log "Sending emails using "+process.env.MAIL_URL else console.log "Not Sending Emails, please set the MAIL_URL environment variable" -notes = new Meteor.Collection "notes" +notes = share.notes getUser = (id) -> Meteor.users.findOne { _id: id } isUsers = (u,doc) -> u and doc.userId is u