summaryrefslogtreecommitdiff
path: root/index.pl
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-10-05 16:33:59 +0200
committerDaniel Friesel <derf@finalrewind.org>2018-10-05 16:33:59 +0200
commitd75351b30c38dc22e0071e18dc68be551eb628a7 (patch)
tree42cf1576b093977f8c0a95cdd49931ec0ad7a8ae /index.pl
parent3198689286984a47d0250712d9928c7a9ff2f7b9 (diff)
Use POST requests to modify data
Diffstat (limited to 'index.pl')
-rw-r--r--index.pl83
1 files changed, 53 insertions, 30 deletions
diff --git a/index.pl b/index.pl
index dcc941c..6d00a1d 100644
--- a/index.pl
+++ b/index.pl
@@ -181,6 +181,10 @@ app->attr(
sub epoch_to_dt {
my ($epoch) = @_;
+ # Bugs (and user errors) may lead to undefined timestamps. Set them to
+ # 1970-01-01 to avoid crashing and show obviously wrong data instead.
+ $epoch //= 0;
+
return DateTime->from_epoch(
epoch => $epoch,
time_zone => 'Europe/Berlin'
@@ -479,10 +483,7 @@ helper 'get_user_status' => sub {
if ( @{$rows} ) {
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
- my $ts = DateTime->from_epoch(
- epoch => $rows->[0][1],
- time_zone => 'Europe/Berlin'
- );
+ my $ts = epoch_to_dt( $rows->[0][1] );
my $checkin_station_name = $rows->[0][3];
my @route = split( qr{[|]}, $rows->[0][8] // q{} );
my @route_after;
@@ -529,41 +530,63 @@ get '/' => sub {
$self->render('landingpage');
};
-get '/a/checkin' => sub {
- my ($self) = @_;
- my $station = $self->param('station');
- my $train_id = $self->param('train');
-
- my ( $train, $error ) = $self->checkin( $station, $train_id );
+post '/action' => sub {
+ my ($self) = @_;
+ my $params = $self->req->json;
- if ($error) {
- $self->render(
- 'checkin',
- error => $error,
- train => undef
- );
+ if ( not exists $params->{action} ) {
+ $params = $self->req->params->to_hash;
}
- else {
+
+ if ( not $params->{action} ) {
$self->render(
- 'checkin',
- error => undef,
- train => $train
+ json => {},
+ status => 400,
);
+ return;
}
-};
-get '/a/checkout' => sub {
- my ($self) = @_;
- my $station = $self->param('station');
- my $force = $self->param('force');
+ my $station = $params->{station};
- my $error = $self->checkout( $station, $force );
+ if ( $params->{action} eq 'checkin' ) {
- if ($error) {
- $self->render( 'checkout', error => $error );
+ my ( $train, $error )
+ = $self->checkin( $params->{station}, $params->{train}, );
+
+ if ($error) {
+ $self->render(
+ json => {
+ success => 0,
+ error => $error,
+ },
+ );
+ }
+ else {
+ $self->render(
+ json => {
+ success => 1,
+ },
+ );
+ }
}
- else {
- $self->redirect_to("/${station}");
+ elsif ( $params->{action} eq 'checkout' ) {
+ my $error = $self->checkout( $params->{station}, $params->{force}, );
+
+ if ($error) {
+ $self->render(
+ json => {
+ success => 0,
+ error => $error,
+ },
+ );
+ }
+ else {
+ $self->render(
+ json => {
+ success => 1,
+ },
+ );
+ }
}
};