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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use super::model_api::meta::Meta;
use models::dropoff::DropOff;
use models::pickup::PickUp;
use models::time::Time;
use super::model_api::resultarray::ResultArray;
use super::super::Json;
use super::super::Pool;
use super::super::PostgresConnectionManager;
use super::super::RoutesHandler;
use super::super::State;
use postgres::rows::Row;
use num_traits as num;
use super::NaiveDate;
use super::NaiveTime;
use postgres::types::ToSql;
use super::model_api::search::ascdesc::AscDesc;
use super::model_api::search::time::TimeSearch;
use super::model_api::search::time::TimeSort;
#[get("/times?<time_search>")]
pub fn times_query(rh: State<RoutesHandler>, time_search: TimeSearch) -> Json<ResultArray<Time>> {
let result = get_times_by_query(&time_search, &rh.pool);
let meta = Meta {
success: true,
error: None,
pagination: Option::None
};
Json(ResultArray::<Time> {
result: Some(result),
meta,
})
}
#[get("/times/by-trip/<trip_uid>")]
pub fn times_by_trip(rh: State<RoutesHandler>, trip_uid: String) -> Json<ResultArray<Time>> {
let result = get_times_by_trip(trip_uid, &rh.pool);
let meta = Meta {
success: true,
error: None,
pagination: Option::None
};
Json(ResultArray::<Time> {
result: Some(result),
meta,
})
}
#[get("/times/by-stop/<stop_id>?<time_search>")]
pub fn times_stop_query(
rh: State<RoutesHandler>,
stop_id: String,
mut time_search: TimeSearch,
) -> Json<ResultArray<Time>> {
time_search.stop = Some(stop_id);
let result = get_times_by_query(&time_search, &rh.pool);
let meta = Meta {
success: true,
error: None,
pagination: Option::None
};
Json(ResultArray::<Time> {
result: Some(result),
meta
})
}
#[get("/times/by-stop/<stop_id>")]
pub fn times_stop(rh: State<RoutesHandler>, stop_id: String) -> Json<ResultArray<Time>> {
let result = get_times_by_stop_id(stop_id, &rh.pool);
let meta = Meta {
success: true,
error: None,
pagination: Option::None
};
Json(ResultArray::<Time> {
result: Some(result),
meta,
})
}
fn get_times_by_query<'a>(
time_search: &TimeSearch,
pool: &Pool<PostgresConnectionManager>,
) -> Vec<Time> {
let mut query = String::from(
"SELECT t.uid,
arrival_time,
departure_time,
s.uid,
stop_sequence,
pickup_type,
drop_off_type,
r.uid,
c.uid,
c.monday,
c.tuesday,
c.wednesday,
c.thursday,
c.friday,
c.saturday,
c.sunday,
c.start_date,
c.end_date,
a.feed_id
FROM stop_time as a, stop as s, trip as t, route as r, calendar as c
WHERE
a.stop_id = s.id AND
a.trip_id = t.trip_id AND
t.route_id = r.id AND
t.service_id = c.service_id AND
a.feed_id = s.feed_id AND
t.feed_id = s.feed_id AND
c.feed_id = t.feed_id AND
r.feed_id = c.feed_id AND
r.id = t.route_id",
);
let mut dates: Vec<NaiveDate> = Vec::new();
let mut times: Vec<NaiveTime> = Vec::new();
let mut values: Vec<&bool> = Vec::new();
let mut i32_values: Vec<i32> = Vec::new();
let mut string_values: Vec<&String> = Vec::new();
let mut params: Vec<&ToSql> = Vec::new();
let mut i = 0;
let mut addition: String;
if time_search.stop.is_some() {
string_values.push(time_search.stop.as_ref().unwrap());
i += 1;
addition = format!(" AND s.uid=${} ", &i);
query.push_str(&addition);
}
for &val in string_values.iter() {
params.push(val);
}
string_values = Vec::new();
if time_search.monday.is_some() {
values.push(time_search.monday.as_ref().unwrap());
i += 1;
addition = format!(" AND monday = ${}", &i);
query.push_str(&addition);
}
if time_search.tuesday.is_some() {
values.push(time_search.tuesday.as_ref().unwrap());
i += 1;
addition = format!(" AND tuesday = ${}", &i);
query.push_str(&addition);
}
if time_search.wednesday.is_some() {
values.push(time_search.wednesday.as_ref().unwrap());
i += 1;
addition = format!(" AND wednesday = ${}", &i);
query.push_str(&addition);
}
if time_search.thursday.is_some() {
values.push(time_search.thursday.as_ref().unwrap());
i += 1;
addition = format!(" AND thursday = ${}", &i);
query.push_str(&addition);
}
if time_search.friday.is_some() {
values.push(time_search.friday.as_ref().unwrap());
i += 1;
addition = format!(" AND friday = ${}", &i);
query.push_str(&addition);
}
if time_search.saturday.is_some() {
values.push(time_search.saturday.as_ref().unwrap());
i += 1;
addition = format!(" AND saturday = ${}", &i);
query.push_str(&addition);
}
if time_search.sunday.is_some() {
values.push(time_search.sunday.as_ref().unwrap());
i += 1;
addition = format!(" AND sunday = ${}", &i);
query.push_str(&addition);
}
for val in &values {
params.push(val);
}
if time_search.date.is_some() {
string_values.push(time_search.date.as_ref().unwrap());
i += 1;
addition = format!(" AND start_date <= ${0} AND end_date >= ${0}", &i);
query.push_str(&addition);
}
for &val in string_values.iter() {
&dates.push(val.parse::<NaiveDate>().unwrap());
}
for val in &dates {
params.push(val);
}
i32_values = Vec::new();
if time_search.stop_sequence.is_some() {
i32_values.push(time_search.stop_sequence.unwrap());
i += 1;
addition = format!(" AND stop_sequence = ${}", &i);
query.push_str(&addition);
}
if time_search.drop_off_type.is_some() {
let drop_off: DropOff = DropOff::from_string(time_search.drop_off_type.as_ref().unwrap());
i32_values.push(num::ToPrimitive::to_i32(&drop_off).unwrap());
i += 1;
addition = format!(" AND a.drop_off_type = ${}", &i);
query.push_str(&addition);
}
if time_search.pickup_type.is_some() {
let pickup: PickUp = PickUp::from_string(time_search.pickup_type.as_ref().unwrap());
i32_values.push(num::ToPrimitive::to_i32(&pickup).unwrap());
i += 1;
addition = format!(" AND a.pickup_type = ${}", &i);
query.push_str(&addition);
}
for val in &i32_values {
params.push(val);
}
string_values = Vec::new();
if time_search.at_a.is_some() {
string_values.push(time_search.at_a.as_ref().unwrap());
i += 1;
addition = format!(" AND arrival_time >= ${}", &i);
query.push_str(&addition);
}
if time_search.at_b.is_some() {
string_values.push(time_search.at_b.as_ref().unwrap());
i += 1;
addition = format!(" AND arrival_time <= ${}", &i);
query.push_str(&addition);
}
for &val in string_values.iter() {
×.push(val.parse::<NaiveTime>().unwrap());
}
for val in × {
params.push(val);
}
string_values = Vec::new();
if time_search.trip_id.is_some() {
string_values.push(time_search.trip_id.as_ref().unwrap());
i += 1;
addition = format!(" AND t.uid = ${}", &i);
query.push_str(&addition);
}
if time_search.route.is_some() {
string_values.push(time_search.route.as_ref().unwrap());
i += 1;
addition = format!(" AND r.uid = ${}", &i);
query.push_str(&addition);
}
for &val in string_values.iter() {
params.push(val);
}
if time_search.sort_by.is_some() {
let mut sort: AscDesc = AscDesc::ASC;
if time_search.sort_order.as_ref().is_some() {
let res = time_search.sort_order.as_ref().unwrap();
sort = match res.to_lowercase().as_str() {
"asc" => AscDesc::ASC,
"desc" => AscDesc::DESC,
_ => AscDesc::ASC,
};
}
let sort_by = match time_search.sort_by.as_ref().unwrap().as_str() {
"arrival_time" => TimeSort::ArrivalTime,
"departure_time" => TimeSort::DepartureTime,
"stop_sequence" => TimeSort::StopSequence,
&_ => TimeSort::ArrivalTime,
};
addition = format!(" ORDER BY {} {}", sort_by.as_str(), sort.as_str());
query.push_str(&addition);
}
query.push_str(" LIMIT 50");
println!("{}", query);
let conn = pool.clone().get().unwrap();
let times = conn.query(&query, ¶ms.as_slice());
println!("{:?}", params.as_slice());
let mut times_result: Vec<Time> = Vec::new();
for row in times.expect("Query failed").iter() {
let time = parse_time_row(&row);
times_result.push(time);
}
times_result
}
pub fn get_times_by_trip(trip_uid: String, pool: &Pool<PostgresConnectionManager>) -> Vec<Time> {
let mut ts: TimeSearch = Default::default();
ts.trip = Some(trip_uid);
get_times_by_query(&ts, pool)
}
fn get_times_by_stop_id(stop_uid: String, pool: &Pool<PostgresConnectionManager>) -> Vec<Time> {
let mut ts: TimeSearch = Default::default();
ts.stop = Some(stop_uid);
get_times_by_query(&ts, pool)
}
fn parse_time_row(row: &Row) -> Time {
let pickup_int: i32 = row.get(5);
let dropoff_int: i32 = row.get(6);
let arrival: NaiveTime = row.get(1);
let departure: NaiveTime = row.get(2);
let start_date: NaiveDate = row.get(16);
let end_date: NaiveDate = row.get(17);
let time = Time {
trip_id: row.get(0),
arrival_time: arrival.format("%H:%M:%S").to_string(),
departure_time: departure.format("%H:%M:%S").to_string(),
stop_id: row.get(3),
stop_sequence: row.get(4),
pickup_type: num::FromPrimitive::from_i32(pickup_int).unwrap(),
drop_off_type: num::FromPrimitive::from_i32(dropoff_int).unwrap(),
route_id: row.get(7),
service_days: vec![
row.get(9),
row.get(10),
row.get(11),
row.get(12),
row.get(13),
row.get(14),
row.get(15),
],
service_uid: row.get(8),
start_date,
end_date,
feed_id: row.get(18),
};
time
}