1
0
mirror of https://github.com/fazo96/homework.git synced 2025-01-10 12:14:22 +01:00

Added experimental RESTful API, bump version to 1.3

This commit is contained in:
Enrico Fasoli 2015-03-07 17:50:26 +01:00
parent c567751331
commit 57d99d5c94
3 changed files with 69 additions and 4 deletions

View File

@ -1,5 +1,5 @@
# Homework - Client Side # Homework - Client Side
version = "1.2" version = "1.3"
# Utilities # Utilities
tick = new Tracker.Dependency() tick = new Tracker.Dependency()
Meteor.setInterval (-> tick.changed();), 15000 Meteor.setInterval (-> tick.changed();), 15000
@ -132,7 +132,6 @@ Router.route '/archive/:_id?',
waitOn: -> @notes = Meteor.subscribe 'notes', yes waitOn: -> @notes = Meteor.subscribe 'notes', yes
onStop: -> @notes.stop() onStop: -> @notes.stop()
controller: loggedInController controller: loggedInController
Router.route '/(.*)', -> @render '404' # Catch-all route
# Client Templates # Client Templates

67
server/api.coffee Normal file
View File

@ -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 {}

View File

@ -1,12 +1,11 @@
# Homework - Server Side # Homework - Server Side
notes = share.notes
console.log "Started Homework server!" console.log "Started Homework server!"
if process.env.MAIL_URL if process.env.MAIL_URL
console.log "Sending emails using "+process.env.MAIL_URL console.log "Sending emails using "+process.env.MAIL_URL
else else
console.log "Not Sending Emails, please set the MAIL_URL environment variable" 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 } getUser = (id) -> Meteor.users.findOne { _id: id }
isUsers = (u,doc) -> u and doc.userId is u isUsers = (u,doc) -> u and doc.userId is u