Add manual journey entry

Closes #3
This commit is contained in:
Daniel Friesel 2019-04-26 19:53:01 +02:00
parent 2cae5a51d2
commit e07063c52c
10 changed files with 198 additions and 120 deletions

View file

@ -220,27 +220,16 @@ sub startup {
} }
); );
# Returns (checkin id, checkout id, error) # Returns (journey id, error)
# Must be called during a transaction. # Must be called during a transaction.
# Must perform a rollback on error. # Must perform a rollback on error.
$self->helper( $self->helper(
'add_journey' => sub { 'add_journey' => sub {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;
$self->app->log->error( my $db = $opt{db};
"add_journey is not implemented at the moment");
return ( undef, undef, 'not implemented' );
my $user_status = $self->get_user_status;
if ( $user_status->{checked_in} or $user_status->{cancelled} ) {
# TODO: change database schema to one row per journey instead of two
return ( undef, undef,
'Während einer Zugfahrt können momentan keine manuellen Einträge vorgenommen werden. Klingt komisch, ist aber so.'
);
}
my $uid = $self->current_user->{id}; my $uid = $self->current_user->{id};
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
my $dep_station = get_station( $opt{dep_station} ); my $dep_station = get_station( $opt{dep_station} );
my $arr_station = get_station( $opt{arr_station} ); my $arr_station = get_station( $opt{arr_station} );
@ -251,66 +240,48 @@ sub startup {
return ( undef, undef, 'Unbekannter Zielbahnhof' ); return ( undef, undef, 'Unbekannter Zielbahnhof' );
} }
my $checkin_id; my $entry = {
my $checkout_id;
eval {
$checkin_id = $self->pg->db->insert(
'user_actions',
{
user_id => $uid, user_id => $uid,
action_id => 'checkin', train_type => $opt{train_type},
station_id => $self->get_station_id( train_line => $opt{train_line},
train_no => $opt{train_no},
train_id => 'manual',
checkin_station_id => $self->get_station_id(
ds100 => $dep_station->[0], ds100 => $dep_station->[0],
name => $dep_station->[1], name => $dep_station->[1],
), ),
action_time => checkin_time => $now,
DateTime->now( time_zone => 'Europe/Berlin' ), sched_departure => $opt{sched_departure},
edited => 0x0f, real_departure => $opt{rt_departure},
train_type => $opt{train_type}, checkout_station_id => $self->get_station_id(
train_line => $opt{train_line},
train_no => $opt{train_no},
sched_time => $opt{sched_departure},
real_time => $opt{rt_departure},
},
{ returning => 'id' }
)->hash->{id};
};
if ($@) {
$self->app->log->error(
"add_journey($uid, checkin): INSERT failed: $@");
return ( undef, undef, 'INSERT failed: ' . $@ );
}
eval {
$checkout_id = $self->pg->db->insert(
'user_actions',
{
user_id => $uid,
action_id => 'checkout',
station_id => $self->get_station_id(
ds100 => $arr_station->[0], ds100 => $arr_station->[0],
name => $arr_station->[1], name => $arr_station->[1],
), ),
action_time => sched_arrival => $opt{sched_arrival},
DateTime->now( time_zone => 'Europe/Berlin' ), real_arrival => $opt{rt_arrival},
edited => 0x0f, checkout_time => $now,
train_type => $opt{train_type}, edited => 0x3fff,
train_line => $opt{train_line}, cancelled => $opt{cancelled} ? 1 : 0,
train_no => $opt{train_no}, route => $dep_station->[1] . '|' . $arr_station->[1],
sched_time => $opt{sched_arrival},
real_time => $opt{rt_arrival},
},
{ returnning => 'id' }
)->hash->{id};
}; };
if ($@) {
$self->app->log->error( if ( $opt{comment} ) {
"add_journey($uid, checkout): INSERT failed: $@"); $entry->{messages} = '0:' . $opt{comment};
return ( undef, undef, 'INSERT failed: ' . $@ );
} }
return ( $checkin_id, $checkout_id, undef );
my $journey_id = undef;
eval {
$journey_id
= $db->insert( 'journeys', $entry, { returning => 'id' } )
->hash->{id};
};
if ($@) {
$self->app->log->error("add_journey($uid): $@");
return ( undef, 'add_journey failed: ' . $@ );
}
return ( $journey_id, undef );
} }
); );
@ -1096,6 +1067,31 @@ sub startup {
} }
); );
$self->helper(
'get_oldest_journey_ts' => sub {
my ($self) = @_;
my $res_h = $self->pg->db->select(
'journeys_str',
['sched_dep_ts'],
{
user_id => $self->current_user->{id},
},
{
limit => 1,
order_by => {
-asc => 'real_dep_ts',
},
}
)->hash;
if ($res_h) {
return epoch_to_dt( $res_h->{sched_dep_ts} );
}
return undef;
}
);
$self->helper( $self->helper(
'get_user_travels' => sub { 'get_user_travels' => sub {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;
@ -1171,12 +1167,12 @@ sub startup {
} }
$ref->{messages} = [ reverse @parsed_messages ]; $ref->{messages} = [ reverse @parsed_messages ];
$ref->{sched_duration} $ref->{sched_duration}
= $ref->{sched_arrival} = $ref->{sched_arrival}->epoch
? $ref->{sched_arrival}->epoch ? $ref->{sched_arrival}->epoch
- $ref->{sched_departure}->epoch - $ref->{sched_departure}->epoch
: undef; : undef;
$ref->{rt_duration} $ref->{rt_duration}
= $ref->{rt_arrival} = $ref->{rt_arrival}->epoch
? $ref->{rt_arrival}->epoch - $ref->{rt_departure}->epoch ? $ref->{rt_arrival}->epoch - $ref->{rt_departure}->epoch
: undef; : undef;
my ( $km, $skip ) my ( $km, $skip )

View file

@ -562,6 +562,7 @@ sub add_journey_form {
for my $key (qw(sched_departure rt_departure sched_arrival rt_arrival)) for my $key (qw(sched_departure rt_departure sched_arrival rt_arrival))
{ {
if ( $self->param($key) ) {
my $datetime = $parser->parse_datetime( $self->param($key) ); my $datetime = $parser->parse_datetime( $self->param($key) );
if ( not $datetime ) { if ( not $datetime ) {
$self->render( $self->render(
@ -573,26 +574,48 @@ sub add_journey_form {
} }
$opt{$key} = $datetime; $opt{$key} = $datetime;
} }
}
for my $key (qw(dep_station arr_station)) { for my $key (qw(dep_station arr_station cancelled comment)) {
$opt{$key} = $self->param($key); $opt{$key} = $self->param($key);
} }
#my ( $checkin_id, $checkout_id, $error ) = $self->add_journey(%opt); my $db = $self->pg->db;
my $tx = $db->begin;
$opt{db} = $db;
my ( $journey_id, $error ) = $self->add_journey(%opt);
if ( not $error ) {
my $journey = $self->get_journey(
uid => $self->current_user->{id},
db => $db,
journey_id => $journey_id,
verbose => 1
);
$error = $self->journey_sanity_check($journey);
}
if ($error) {
$self->render( $self->render(
'add_journey', 'add_journey',
with_autocomplete => 1, with_autocomplete => 1,
error => 'not implemented', error => $error,
); );
return;
} }
else {
$tx->commit;
$self->redirect_to("/journey/${journey_id}");
}
}
else {
$self->render( $self->render(
'add_journey', 'add_journey',
with_autocomplete => 1, with_autocomplete => 1,
error => undef error => undef
); );
} }
}
1; 1;

View file

@ -13,7 +13,7 @@
% for my $travel (@{$journeys}) { % for my $travel (@{$journeys}) {
% my $detail_link = '/journey/' . $travel->{id}; % my $detail_link = '/journey/' . $travel->{id};
<tr> <tr>
<td><%= $travel->{sched_departure}->strftime('%d.%m.') %></td> <td><%= $travel->{sched_departure}->strftime($date_format) %></td>
<td><a href="<%= $detail_link %>"><%= $travel->{type} %> <%= $travel->{line} // $travel->{no} %></a></td> <td><a href="<%= $detail_link %>"><%= $travel->{type} %> <%= $travel->{line} // $travel->{no} %></a></td>
<td> <td>
<a href="<%= $detail_link %>" class="unmarked"> <a href="<%= $detail_link %>" class="unmarked">

View file

@ -1,4 +1,18 @@
<h1>Zugfahrt eingeben</h1> <h1>Zugfahrt eingeben</h1>
% if (not get_oldest_journey_ts()) {
<div class="row">
<div class="col s12">
<div class="card yellow lighten-4">
<div class="card-content">
<span class="card-title">Hinweis</span>
<p>travelynx ist darauf ausgelegt, über die Hauptseite in
Echtzeit in Züge ein- und auszuchecken. Die manuelle
Eingabe von Zugfahrten ist nur als Notlösung vorgesehen.</p>
</div>
</div>
</div>
</div>
% }
% if ($error) { % if ($error) {
<div class="row"> <div class="row">
<div class="col s12"> <div class="col s12">
@ -23,37 +37,49 @@
%= form_for '/journey/add' => (method => 'POST') => begin %= form_for '/journey/add' => (method => 'POST') => begin
%= csrf_field %= csrf_field
<div class="row"> <div class="row">
<div class="input-field col s12"> <div class="input-field col s12 m6 l6">
%= text_field 'train', id => 'train', class => 'validate', required => undef, pattern => '[0-9a-zA-Z]+ +[0-9a-zA-Z]* *[0-9]+' %= text_field 'train', id => 'train', class => 'validate', required => undef, pattern => '[0-9a-zA-Z]+ +[0-9a-zA-Z]* *[0-9]+'
<label for="train">Zug (Typ Linie Nummer)</label> <label for="train">Zug (Typ Linie Nummer)</label>
</div> </div>
<div class="input-field col s12 m6 l6">
<label>
%= check_box cancelled => 1
<span>Fahrt ist ausgefallen</span>
</label>
</div>
</div> </div>
<div class="row"> <div class="row">
<div class="input-field col s12"> <div class="input-field col s12">
%= text_field 'dep_station', id => 'dep_station', class => 'autocomplete validate', required => undef %= text_field 'dep_station', id => 'dep_station', class => 'autocomplete validate', required => undef
<label for="dep_station">Startbahnhof (Name oder DS100)</label> <label for="dep_station">Start (Name oder DS100)</label>
</div> </div>
<div class="input-field col s12"> <div class="input-field col s12">
%= text_field 'sched_departure', id => 'sched_departure', class => 'validate', required => undef, pattern => '[0-9][0-9]?[.][0-9][0-9]?[.][0-9][0-9][0-9][0-9] +[0-9][0-9]:[0-9][0-9]' %= text_field 'sched_departure', id => 'sched_departure', class => 'validate', required => undef, pattern => '[0-9][0-9]?[.][0-9][0-9]?[.][0-9][0-9][0-9][0-9] +[0-9][0-9]:[0-9][0-9]'
<label for="sched_departure">Geplante Abfahrt</label> <label for="sched_departure">Geplante Abfahrt</label>
</div> </div>
<div class="input-field col s12"> <div class="input-field col s12">
%= text_field 'rt_departure', id => 'rt_departure', class => 'validate', pattern => '[0-9][0-9]?[.][0-9][0-9]?[.][0-9][0-9][0-9][0-9] +[0-9][0-9]:[0-9][0-9]' %= text_field 'rt_departure', id => 'rt_departure', class => 'validate', required => undef, pattern => '[0-9][0-9]?[.][0-9][0-9]?[.][0-9][0-9][0-9][0-9] +[0-9][0-9]:[0-9][0-9]'
<label for="rt_departure">Tatsächliche Abfahrt (optional)</label> <label for="rt_departure">Tatsächliche Abfahrt</label>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="input-field col s12"> <div class="input-field col s12">
%= text_field 'arr_station', id => 'arr_station', class => 'autocomplete validate', required => undef %= text_field 'arr_station', id => 'arr_station', class => 'autocomplete validate', required => undef
<label for="arr_station">Zielbahnhof (Name oder DS100)</label> <label for="arr_station">Ziel (Name oder DS100)</label>
</div> </div>
<div class="input-field col s12"> <div class="input-field col s12">
%= text_field 'sched_arrival', id => 'sched_arrival', class => 'validate', required => undef, pattern => '[0-9][0-9]?[.][0-9][0-9]?[.][0-9][0-9][0-9][0-9] +[0-9][0-9]:[0-9][0-9]' %= text_field 'sched_arrival', id => 'sched_arrival', class => 'validate', required => undef, pattern => '[0-9][0-9]?[.][0-9][0-9]?[.][0-9][0-9][0-9][0-9] +[0-9][0-9]:[0-9][0-9]'
<label for="sched_arrival">Geplante Ankunft</label> <label for="sched_arrival">Geplante Ankunft</label>
</div> </div>
<div class="input-field col s12"> <div class="input-field col s12">
%= text_field 'rt_arrival', id => 'rt_arrival', class => 'validate', pattern => '[0-9][0-9]?[.][0-9][0-9]?[.][0-9][0-9][0-9][0-9] +[0-9][0-9]:[0-9][0-9]' %= text_field 'rt_arrival', id => 'rt_arrival', class => 'validate', required => undef, pattern => '[0-9][0-9]?[.][0-9][0-9]?[.][0-9][0-9][0-9][0-9] +[0-9][0-9]:[0-9][0-9]'
<label for="rt_arrival">Tatsächliche Ankunft (optional)</label> <label for="rt_arrival">Tatsächliche Ankunft</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
%= text_field 'comment'
<label for="comment">Kommentar</label>
</div> </div>
</div> </div>
<div class="row"> <div class="row">

View file

@ -17,4 +17,4 @@
</div> </div>
</div> </div>
%= include '_history_trains', journeys => stash('journeys'); %= include '_history_trains', date_format => '%d.%m.%Y', journeys => stash('journeys');

View file

@ -8,24 +8,44 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col s12 m12 l4 center-align"> <div class="col s12 m12 l3 center-align">
<a href="/cancelled" class="waves-effect waves-light btn"><i class="material-icons left">cancel</i> Zugausfälle</a> <a href="/cancelled" class="waves-effect waves-light btn"><i class="material-icons left">cancel</i> Zugausfälle</a>
</div> </div>
<div class="col s12 m12 l4"> <div class="col s12 m12 l1">&nbsp;</div>
&nbsp;
</div>
<div class="col s12 m12 l4 center-align"> <div class="col s12 m12 l4 center-align">
<a href="/journey/add" class="waves-effect waves-light btn"><i class="material-icons left">add</i> Neue Fahrt</a>
</div>
<div class="col s12 m12 l1">&nbsp;</div>
<div class="col s12 m12 l3 center-align">
<a href="/history.json" class="waves-effect waves-light btn"><i class="material-icons left">cloud</i> JSON-Export</a> <a href="/history.json" class="waves-effect waves-light btn"><i class="material-icons left">cloud</i> JSON-Export</a>
</div> </div>
</div> </div>
<h2>Nach Jahr</h2> <h2>Nach Jahr</h2>
% my $since = get_user_data()->{registered_at}; % my $since = get_oldest_journey_ts();
% my $now = DateTime->now(time_zone => 'Europe/Berlin'); % my $now = DateTime->now(time_zone => 'Europe/Berlin');
% if ($since) {
%= include '_history_years', current => '', since => $since->clone, now => $now; %= include '_history_years', current => '', since => $since->clone, now => $now;
% }
% else {
<div class="row">
<div class="col s12">
Noch keine Fahrten.
</div>
</div>
% }
<h2>Nach Monat</h2> <h2>Nach Monat</h2>
% if ($since) {
%= include '_history_months', current => '', since => $since->clone, now => $now; %= include '_history_months', current => '', since => $since->clone, now => $now;
% }
% else {
<div class="row">
<div class="col s12">
Noch keine Fahrten.
</div>
</div>
% }
% if (stash('statistics')) { % if (stash('statistics')) {
%= include '_history_stats', stats => stash('statistics'); %= include '_history_stats', stats => stash('statistics');

View file

@ -1,4 +1,4 @@
% my $since = get_user_data()->{registered_at}; % my $since = get_oldest_journey_ts();
% my $now = DateTime->now(time_zone => 'Europe/Berlin'); % my $now = DateTime->now(time_zone => 'Europe/Berlin');
%= include '_history_months', current => "${year}/${month}", since => $since, now => $now; %= include '_history_months', current => "${year}/${month}", since => $since, now => $now;
@ -9,5 +9,5 @@
% } % }
% if (stash('journeys')) { % if (stash('journeys')) {
%= include '_history_trains', journeys => stash('journeys'); %= include '_history_trains', date_format => '%d.%m.', journeys => stash('journeys');
% } % }

View file

@ -1,4 +1,4 @@
% my $since = get_user_data()->{registered_at}; % my $since = get_oldest_journey_ts();
% my $now = DateTime->now(time_zone => 'Europe/Berlin'); % my $now = DateTime->now(time_zone => 'Europe/Berlin');
%= include '_history_years', current => $year, since => $since, now => $now; %= include '_history_years', current => $year, since => $since, now => $now;
@ -9,5 +9,5 @@
% } % }
% if (stash('journeys')) { % if (stash('journeys')) {
%= include '_history_trains', journeys => stash('journeys'); %= include '_history_trains', date_format => '%d.%m.', journeys => stash('journeys');
% } % }

View file

@ -121,6 +121,17 @@
% } % }
</td> </td>
</tr> </tr>
% if ($journey->{edited} == 0x3fff) {
<tr>
<th scope="row">Kommentar</th>
<td>
% for my $message (@{$journey->{messages} // []}) {
<%= $message->[1] %><br/>
% }
</td>
</tr>
% }
% else {
<tr> <tr>
<th scope="row">Meldungen</th> <th scope="row">Meldungen</th>
<td> <td>
@ -130,6 +141,7 @@
% } % }
</td> </td>
</tr> </tr>
% }
<tr> <tr>
<th scope="row">Route</th> <th scope="row">Route</th>
<td> <td>
@ -172,6 +184,7 @@
%= form_for '/journey/edit' => (method => 'POST') => begin %= form_for '/journey/edit' => (method => 'POST') => begin
%= hidden_field 'journey_id' => param('journey_id') %= hidden_field 'journey_id' => param('journey_id')
<button class="btn waves-effect waves-light" type="submit" name="action" value="edit"> <button class="btn waves-effect waves-light" type="submit" name="action" value="edit">
<i class="material-icons left">edit</i>
Bearbeiten Bearbeiten
</button> </button>
%= end %= end

View file

@ -73,7 +73,7 @@
</div> </div>
</div> </div>
<h1>Letzte Fahrten</h1> <h1>Letzte Fahrten</h1>
%= include '_history_trains', journeys => [get_user_travels(limit => 5)]; %= include '_history_trains', date_format => '%d.%m', journeys => [get_user_travels(limit => 5)];
% } % }
% else { % else {
<div class="row"> <div class="row">