-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
136 lines (106 loc) · 3.71 KB
/
app.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const path = require('path');
const koa = require('koa');
const app = module.exports = new koa();
const routes = require('koa-route');
const views = require('koa-views');
const serve = require('koa-static');
const config = require('./config');
const db = require('./db');
// This library help us to interact with post requests
const parse = require('co-body');
const moment = require('moment');
const createEvent = require('./domain/event');
const createFileSystemEventSource = require('./services/file-system-event-source');
const TEMPORAL_DIR = path.join(process.cwd(), '.tmp');
// set static folders
app.use(serve(TEMPORAL_DIR));
// Identify views files and template languages to use
app.use(views('views', {
map: config.templates
}));
function *renderHome(){
yield this.render('index', {});
}
function *renderAboutUs(){
yield this.render('about_us', {});
}
function *renderContact(){
yield this.render('contact', {});
}
function *renderDonations(){
yield this.render('donations', {});
}
function *renderSponsors(){
const sponsors = yield db.sponsors.find();
const context = {sponsors:sponsors};
yield this.render('sponsors', context);
}
function *renderEvents(){
// TODO: Inject source dependency.
const source = createFileSystemEventSource({
path: path.join(TEMPORAL_DIR, 'events.json'),
factory: createEvent
});
const events = yield source.getAll();
const isPending = (e) => !e.isDue;
const filteredEvents = events.filter(isPending);
const formatDate = (format, date) => {
return moment(date).format(format);
};
yield this.render('events', {events: filteredEvents, formatDate});
}
function *renderTalkSubmissionForm() {
const userGroups = yield db.usergroups.find();
const context = {usergroups: userGroups};
yield this.render('submit_talk', context);
}
function *submitTalk() {
// TODO: Only allow a set of specific fields.
const rawBody = yield parse(this);
const belongsToUserGroup = rawBody.usergroups && rawBody.usergroups.length;
// TODO: Render an error message if talk doesn't belong to an user group.
if (belongsToUserGroup) {
yield db.talks.create(rawBody);
}
return this.redirect('/speakers');
}
function *usergroup(){
var group = new db.usergroups({
name: "Python Dominicana",
facebook_url: "https://www.facebook.com/groups/pythondo/",
website: "www.python.com.do",
logo: "https://scontent-mia1-1.xx.fbcdn.net/hphotos-ash2/v/t1.0-9/10624973_10204014303853300_9205003069849326284_n.jpg?oh=7bcdfcda40ccb89341d73ebfe9bcdbb6&oe=570E52D0",
created: new Date("2013-09-25T19:00:00.001Z"),
description: "Python Dominicana is an amazing place, is the place where Programmers, engineers an all kind of people interested in the Python Computer Language get together to talk, learn and share about all the stuff that can be done with it, from web applications to robots, from distributed systems to desktop apps.",
});
group.save(function(err){
if(err) throw err
});
this.body = "OK";
}
function *users(){
db.users.insert({
first_name: 'Vivian',
last_name: 'Guillen',
email: 'vivian@codetiger.co',
admin: true,
}, function(err){
if(err) throw err;
});
}
function *pageNotFound(){
if( this.status === 404){
yield this.render('404');
}
}
app.use(routes.get('/', renderHome));
app.use(routes.get('/about', renderAboutUs));
app.use(routes.get('/contact', renderContact));
app.use(routes.get('/donations', renderDonations));
app.use(routes.get('/events', renderEvents));
app.use(routes.get('/speakers', renderTalkSubmissionForm));
app.use(routes.post('/speakers', submitTalk));
app.use(routes.get('/sponsors', renderSponsors));
app.use(routes.get('/usergroup', usergroup));
app.use(pageNotFound);
app.listen(config.port);