Started with basic today stats
This commit is contained in:
parent
22abc4e464
commit
86955c0d1c
5 changed files with 150 additions and 0 deletions
22
lib/Pear/LocalLoop/Controller/Api/Stats.pm
Normal file
22
lib/Pear/LocalLoop/Controller/Api/Stats.pm
Normal file
|
@ -0,0 +1,22 @@
|
|||
package Pear::LocalLoop::Controller::Api::Stats;
|
||||
use Mojo::Base 'Mojolicious::Controller';
|
||||
|
||||
sub post_today {
|
||||
my $c = shift;
|
||||
|
||||
my $user = $c->stash->{api_user};
|
||||
|
||||
my $today_rs = $user->transactions->today_rs;
|
||||
my $today_sum = $today_rs->get_column('value')->sum;
|
||||
my $today_count = $today_rs->count;
|
||||
|
||||
return $c->render( json => {
|
||||
success => Mojo::JSON->true,
|
||||
today_sum => $today_sum,
|
||||
today_count => $today_count,
|
||||
today_avg => $today_sum / $today_count,
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
1;
|
29
lib/Pear/LocalLoop/Schema/ResultSet/Transaction.pm
Normal file
29
lib/Pear/LocalLoop/Schema/ResultSet/Transaction.pm
Normal file
|
@ -0,0 +1,29 @@
|
|||
package Pear::LocalLoop::Schema::ResultSet::Transaction;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use base 'DBIx::Class::ResultSet';
|
||||
|
||||
use DateTime;
|
||||
|
||||
sub today_rs {
|
||||
my ( $self ) = @_;
|
||||
|
||||
my $dtf = $self->result_source->schema->storage->datetime_parser;
|
||||
return $self->search({
|
||||
submitted_at => {
|
||||
-between => [
|
||||
$dtf->format_datetime(DateTime->today()),
|
||||
$dtf->format_datetime(DateTime->today()->add( days => 1 )),
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
sub today_for_user {
|
||||
my ( $self, $user ) = @_;
|
||||
return $self->search({ buyer_id => $user->id })->today_rs;
|
||||
}
|
||||
|
||||
1;
|
Reference in a new issue