-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
53 lines (46 loc) · 1.39 KB
/
server.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
const faker = require('faker');
faker.locale = 'en_US';
const Sequelize = require('sequelize');
const conn = new Sequelize(process.env.DATABASE_URL || 'postgres://localhost/acme_event_store_db',{logging:false});
const Event = conn.define('event', {
name: {
type: Sequelize.STRING,
allowNull: false,
validate: { notEmpty: true }
},
date: {
type: Sequelize.DATE,
allowNull: false
}
});
Event.createRandom = function(){ return Event.create({ name: `${faker.lorem.sentences(1)} - ${faker.address.state()}`, date: faker.date.future() }); }
conn.sync({ force: true })
.then(()=> {
const events = [];
while(events.length < 10){
events.push(Event.createRandom());
}
return Promise.all(events);
});
const path = require('path');
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.listen(port);
app.get('/', (req, res, next)=> res.sendFile(path.join(__dirname, 'index.html')));
app.get('/api/events', (req, res, next)=> {
Event.findAll()
.then(events => res.send(events))
.catch(next);
});
app.post('/api/events', (req, res, next)=> {
Event.createRandom()
.then(_event => res.status(201).send(_event))
.catch(next);
});
app.delete('/api/events/:id', (req, res, next)=> {
Event.findByPk(req.params.id)
.then(_event => _event.destroy())
.then(()=> res.sendStatus(204))
.catch(next);
});