From 8754ffd93a11a46f2eaa97be33cc382b69c6539d Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 23 Nov 2017 15:42:37 +0000 Subject: [PATCH] endpoint and code added for LIS orgs --- lib/Pear/LocalLoop.pm | 1 + .../Controller/Api/V1/Supplier/Location.pm | 70 ++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 6b513a1..1dd9f9a 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -158,6 +158,7 @@ sub startup { my $api_v1_supplier = $api_v1->under('/supplier'); $api_v1_supplier->post('/location')->to('api-v1-supplier-location#index'); + $api_v1_supplier->post('/location/lis')->to('api-v1-supplier-location#lis_load'); my $api_v1_org = $api_v1->under('/organisation')->to('api-v1-organisation#auth'); diff --git a/lib/Pear/LocalLoop/Controller/Api/V1/Supplier/Location.pm b/lib/Pear/LocalLoop/Controller/Api/V1/Supplier/Location.pm index accd3af..5b5187d 100644 --- a/lib/Pear/LocalLoop/Controller/Api/V1/Supplier/Location.pm +++ b/lib/Pear/LocalLoop/Controller/Api/V1/Supplier/Location.pm @@ -86,7 +86,7 @@ sub index { $org_rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); - my $suppliers = [ map { + my $suppliers = [ map { { latitude => $_->{organisation}->{latitude} * 1, longitude => $_->{organisation}->{longitude} * 1, @@ -106,4 +106,72 @@ sub index { ); } +sub lis_load { + 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; + my $orgs_lis = $valid_org = $c->schema->resultset('Entity')->find( $c->param('lis') ); + + # need: organisations only, with name, latitude, and longitude + my $org_rs = $orgs_lis->associations->search_related('entity', + { + 'entity.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, + locations => $locations, + self => { + latitude => $entity_type_object->latitude, + longitude => $entity_type_object->longitude, + } + }, + ); +} + 1;