forked from bread-n-butter/eventify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventController.js
196 lines (173 loc) · 6.24 KB
/
eventController.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var User = require('../models/user.js');
var Event = require('../models/event.js');
var EventUser = require('../models/eventuser.js');
module.exports = {
getAllEvents: function(req, res) {
Event
.fetchAll({})
.then(function(collection){
res.json({data: collection.models});
})
.catch(function(error){
console.log(error);
res.send('Error at fetchAll');
});
},
getEvent: function(req, res){
var data = req.body;
Event
.where({id: data.eventId})
.fetch({require: true})
.then(function(event){
res.json({data: event.attributes});
})
.catch(function(error){
console.log(error);
res.send('Error at fetchEvent');
});
},
editEvent: function(req, res){
var data = req.body;
Event
.where({id: data.eventId})
.fetch({require: true})
.then(function(event){
return event.save({
event_name: data.eventName || event.get('event_name'),
event_date: data.date || event.get('event_date'),
total_number_of_people_req: data.totalPeople || event.get('total_number_of_people_req'),
price_per_person: data.pricePerPerson || event.get('price_per_person'),
description: data.description || event.get('description'),
image_url: data.image_url || event.get('image_url'),
event_address_label: data.addressLabel || event.get('event_address_label'),
event_long: data.long || event.get('event_long'),
event_lat: data.lat || event.get('event_lat')
}, {method: 'update'});
}).then(function(){
res.sendStatus(200);
}).catch(function(error){
console.log(error);
res.send('Error at editEvent');
});
},
deleteEvent: function(req, res){
var data = req.body;
Event
.where({id: data.eventId})
.fetch({require: true})
.then(function(event){
return event.destroy();
}).then(function(){
res.sendStatus(200);
})
.catch(function(error){
console.log(error);
res.send('Error at deleteEvent');
});
},
addEvent: function(req, res) {
var data = req.body;
console.log(data);
new Event({
event_name: data.eventName,
event_date: data.date,
total_number_of_people_req: data.totalPeople,
price_per_person: data.pricePerPerson,
description: data.description,
image_url: data.image_url,
event_address_label: data.addressLabel,
event_long: data.long,
event_lat: data.lat,
creator: data.userId
}).save()
.then(function(event){
User
.where({id: event.get('creator')})
.fetch({require: true})
.then(function(user){
return event.save({
creator_first_name: user.get('first_name'),
creator_last_name: user.get('last_name')
});
})
.then(function(){
res.json('your data was posted to the database successfully');
});
});
},
getAllCreatedEvents: function(req, res){
var data = req.body;
Event
.where({creator: data.userId})
.fetchAll()
.then(function(collection){
res.json({data: collection.models});
})
.catch(function(error){
console.log(error);
res.send('Error at getallcreatedevents');
});
},
joinEvent: function(req, res){
var data = req.body;
new EventUser({
event_id: data.eventId,
user_id: data.userId
})
.save()
.then(function(){
Event.where({id: data.eventId})
.fetch({require: true})
.then(function(event){
var total = event.get('num_of_people_joined');
event.set({num_of_people_joined: total+1});
event.save();
}).then(function(){
res.json('Joined Event successfully');
});
});
},
unjoinEvent: function(req, res){
var data = req.body;
console.log(data);
EventUser
.where({event_id: data.eventId, user_id: data.userId})
.fetch({require: true})
.then(function(model){
return model.destroy();
})
.then(function(){
res.sendStatus(200);
})
.catch(function(error){
console.log(error);
res.send('Error at deleteuser');
});
},
getAllJoinedEvents: function(req, res){
var data = req.body;
User
.where({id: data.userId})
.fetch({withRelated: ['events']})
.then(function(collection){
res.json({data: collection});
})
.catch(function(error){
console.log(error);
res.send('Error at getalljoinedevents');
});
}
};
//curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:8080/api/events
//curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:8080/api/events/1
/**
* DummyData
*/
//curl -H "Content-Type: application/json" -X POST -d '{"eventName":"third test party","numOfPeopleJoined":"30", "totalPeople": "30", "pricePerPerson":"30", "description":"will test work", "userId": "1"}' http://localhost:3000/api/events
//curl -H "Content-Type: application/json" -X POST -d '{"firstName":"Kristen","lastName":"Haydel"}' http://localhost:3000/api/users
//curl -H "Content-Type: application/json" -X POST -d '{"eventName":"Mini Medical School","numOfPeopleJoined":"10", "totalPeople":"20", "pricePerPerson":"30", "description":"Awesome event dude"}' http://localhost:3000/api/events
//curl -H "Content-Type: application/json" -X POST -d '{"eventName":"Walla Walla Wine","numOfPeopleJoined":"10", "totalPeople":"20", "pricePerPerson":"30", "description":"Awesome event dude"}' http://localhost:3000/api/events
//curl -H "Content-Type: application/json" -X POST -d '{"eventName":"Northwest Flower & Garden Show","numOfPeopleJoined":"10", "totalPeople":"20", "pricePerPerson":"30", "description":"Awesome event dude"}' http://localhost:3000/api/events
//curl -i -X PUT -H "Content-Type:application/json" http://localhost:3000/api/events/1 -d '{"eventName":"third test2 party","numOfPeopleJoined":"30", "totalPeople": "20", "pricePerPerson":"60", "description":"will dis work"}'
//curl -H "Content-Type: application/json" -X PUT -d '{"eventName":"third test2 party","numOfPeopleJoined":"30", "totalPeople": "20", "pricePerPerson":"60", "description":"will dis work"}' http://localhost:3000/api/events/2
//curl -X "DELETE" http://localhost:3000/api/events/3