-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 1.8 KB
/
index.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
const express = require('express');
const path = require('path');
const app = express();
const port = 3001;
const bodyParser = require('body-parser');
const sequelize = require('sequelize');
const db = require('./db/index.js');
app.use(bodyParser());
app.use('/booking/:listingID', express.static('/Users/katherinewang/Desktop/Reservations/public/dist'));
app.get('/listing/:listingID', (req, res) => {
const { listingID } = req.params;
db.Listing.findOne({
where: {
id: listingID,
},
}).then(results => res.send(results))
.catch(error => res.send(error));
});
app.get('/reserved/month/', (req, res) => {
const { id, month, year } = req.query;
db.Reserved.findAll({
attributes: ['date'],
where: {
where: sequelize.where(sequelize.fn('YEAR', sequelize.col('date')), year),
$and: sequelize.where(sequelize.fn('MONTH', sequelize.col('date')), month),
listing_id: id,
},
})
.then((results) => {
const days = results.map(date => Number(date.date.slice(-2)));
res.send(days);
})
.catch(error => res.send(error));
});
app.get('/custom/month/', (req, res) => {
const { id, time } = req.query;
db.CustomRates.findAll({
attributes: ['date', 'price'],
where: {
date: time,
listing_id: id,
},
})
.then(results => res.send(results))
.catch(error => res.send(error));
// db.CustomRates.findAll({
// attributes: ['date', 'price'],
// where: {
// where: sequelize.where(sequelize.fn('YEAR', sequelize.col('date')), year),
// $and: sequelize.where(sequelize.fn('MONTH', sequelize.col('date')), month),
// listing_id: id,
// },
// })
// .then(results => res.send(results))
// .catch(error => res.send(error));
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));