Added supplier location endpoint

This commit is contained in:
Tom Bloor 2017-09-27 18:01:06 +01:00
parent df90f62b7a
commit 5e078f8f8b
8 changed files with 414 additions and 1 deletions

View file

@ -148,6 +148,10 @@ sub startup {
my $api_v1 = $api->under('/v1');
my $api_v1_supplier = $api_v1->under('/supplier');
$api_v1_supplier->post('/location')->to('api-v1-supplier-location#index');
my $api_v1_org = $api_v1->under('/organisation')->to('api-v1-organisation#auth');
$api_v1_org->post('/graphs')->to('api-v1-organisation-graphs#index');

View file

@ -36,7 +36,6 @@ sub run {
split_postcode => 1,
);
use Devel::Dwarn;
my $pc_rs = $self->app->schema->resultset('GbPostcode');
while ( my $pc = $iter->() ) {
$pc_rs->find_or_create(

View file

@ -0,0 +1,109 @@
package Pear::LocalLoop::Controller::Api::V1::Supplier::Location;
use Mojo::Base 'Mojolicious::Controller';
has validation_data => sub {
my $children_errors = {
latitude => {
validation => [
{ required => {} },
{ number => { error_prefix => 'not_number' } },
{ in_range => { args => [ -90, 90 ], error_prefix => 'outside_range' } },
],
},
longitude => {
validation => [
{ required => {} },
{ number => { error_prefix => 'not_number' } },
{ in_range => { args => [ -180, 180 ], error_prefix => 'outside_range' } },
],
},
};
return {
index => {
north_east => {
validation => [
{ required => {} },
{ is_object => { error_prefix => 'not_object' } },
],
children => $children_errors,
},
south_west => {
validation => [
{ required => {} },
{ is_object => { error_prefix => 'not_object' } },
],
children => $children_errors,
},
}
}
};
sub index {
my $c = shift;
return if $c->validation_error('index');
my $json = $c->stash->{api_json};
# Extra custom error, because its funny
if ( $json->{north_east}->{latitude} < $json->{south_west}->{latitude} ) {
return $c->render(
json => {
success => Mojo::JSON->false,
errors => [ 'upside_down' ],
},
status => 400,
);
}
my $entity = $c->stash->{api_user}->entity;
my $entity_type_object = $entity->type_object;
# need: organisations only, with name, latitude, and longitude
my $org_rs = $entity->purchases->search_related('seller',
{
'seller.type' => 'organisation',
'organisation.latitude' => { -between => [
$json->{south_west}->{latitude},
$json->{north_east}->{latitude},
] },
'organisation.longitude' => { -between => [
$json->{south_west}->{longitude},
$json->{north_east}->{longitude},
] },
},
{
join => [ qw/ organisation / ],
columns => [
'organisation.name',
'organisation.latitude',
'organisation.longitude',
],
group_by => [ qw/ organisation.id / ],
},
);
$org_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $suppliers = [ map {
{
latitude => $_->{organisation}->{latitude} * 1,
longitude => $_->{organisation}->{longitude} * 1,
name => $_->{organisation}->{name},
}
} $org_rs->all ];
$c->render(
json => {
success => Mojo::JSON->true,
suppliers => $suppliers,
self => {
latitude => $entity_type_object->latitude,
longitude => $entity_type_object->longitude,
}
},
);
}
1;

View file

@ -64,6 +64,115 @@ sub register {
$value = $app->parse_iso_datetime( $value );
return defined $value ? undef : 1;
});
$app->validator->add_check( is_object => sub {
my ( $validation, $name, $value ) = @_;
return ref ( $value ) eq 'HASH' ? undef : 1;
});
$app->validator->add_check( in_range => sub {
my ( $validation, $name, $value, $low, $high ) = @_;
return $low < $value && $value < $high ? undef : 1;
});
$app->helper( validation_error => sub { _validation_error(@_) } );
}
=head2 validation_error
Returns undef if there is no validation error, returns true otherwise - having
set the errors up as required. Renders out the errors as an array, with status
400
=cut
sub _validation_error {
my ( $c, $sub_name ) = @_;
my $val_data = $c->validation_data->{ $sub_name };
return unless defined $val_data;
my $data = $c->stash->{api_json};
my @errors = _validate_set( $c, $val_data, $data );
if ( scalar @errors ) {
my @sorted_errors = sort @errors;
$c->render(
json => {
success => Mojo::JSON->false,
errors => \@sorted_errors,
},
status => 400,
);
return \@errors;
}
return;
}
sub _validate_set {
my ( $c, $val_data, $data, $parent_name ) = @_;
my @errors;
# MUST get a raw validation object
my $validation = $c->app->validator->validation;
$validation->input( $data );
for my $val_data_key ( keys %$val_data ) {
$validation->topic( $val_data_key );
my $val_set = $val_data->{$val_data_key};
my $custom_check_prefix = {};
for my $val_error ( @{$val_set->{validation}} ) {
my ( $val_validator ) = keys %$val_error;
unless (
$validation->validator->checks->{$val_validator}
|| $val_validator =~ /required|optional/
) {
$c->app->log->warn( 'Unknown Validator [' . $val_validator . ']' );
next;
}
if ( my $custom_prefix = $val_error->{ $val_validator }->{ error_prefix } ) {
$custom_check_prefix->{ $val_validator } = $custom_prefix;
}
my $val_args = $val_error->{ $val_validator }->{ args };
$validation->$val_validator(
( $val_validator =~ /required|optional/ ? $val_data_key : () ),
( defined $val_args ? @$val_args : () )
);
# stop bothering checking if failed, validation stops after first failure
last if $validation->has_error( $val_data_key );
}
if ( $validation->has_error( $val_data_key ) ) {
my ( $check ) = @{ $validation->error( $val_data_key ) };
my $error_prefix = defined $custom_check_prefix->{ $check }
? $custom_check_prefix->{ $check }
: $check;
my $error_string = join ('_',
$error_prefix,
( defined $parent_name ? $parent_name : () ),
$val_data_key,
);
push @errors, $error_string;
} elsif ( defined $val_set->{ children } ) {
push @errors, _validate_set(
$c,
$val_set->{ children },
$data->{ $val_data_key },
$val_data_key );
}
}
return @errors;
}
1;

View file

@ -63,4 +63,16 @@ sub name {
}
}
sub type_object {
my $self = shift;
if ( $self->type eq 'customer' ) {
return $self->customer;
} elsif ( $self->type eq 'organisation' ) {
return $self->organisation;
} else {
return;
}
}
1;