-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.coffee
63 lines (54 loc) · 1.88 KB
/
routes.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Resource = require './models/Resource'
User = require './models/User'
passport = require 'passport'
@include = ->
@get '/': ->
@render 'landing': {passport: @session.passport}
# Authenication
@app.get '/auth/google', passport.authenticate 'google'
@app.get '/auth/google/return', passport.authenticate 'google', { successRedirect: '/', failureRedirect: '/login' }
@get '/auth/:provider': ->
passport.authenticate @params.provider
@get '/auth/:provider/return': ->
passport.authenticate @params.provider, { successRedirect: '/', failureRedirect: '/login' }
#
# JSON for landing page stream (offers)
#
@get '/offers': ->
Resource.find ['type': 'offer', 'taker': '$eq': null, 'start': '$gte': new Date(), 'end': '$gte': new Date()], (err, offers) =>
@response.json offers
#
# JSON for landing page stream (requests)
#
@get '/requests': ->
Resource.find ['type': 'requests', 'taker': '$eq': null, 'start': '$lte': new Date(), 'end': '$gte': new Date()], (err, requests) =>
@response.json requests
#
# Mark a resource consumed (tickbox)
#
@get /\/consume\/(\w+)/, ->
if @session?.user?
Resource.update '_id': @params[0], [ 'taker': @session.user ], {}, (err, updated) =>
console.log "Error while updating resource" if err?
@response.json updated unless err?
else
console.log "You are not logged in"
@response.redirect 'home'
#
# Adding offers
#
@post '/offers': ->
offer = new Resource @body.post
offer.owner = @session.user
offer.save (err) ->
console.log "Error while saving offer" if err?
@response.json offer unless err?
#
# Adding requests
#
@post '/requests': ->
request = new Resource @body.post
request.owner = @session.user
request.save (err) ->
console.log "Error while saving request" if err?
@response.json request unless err?