From b03b7ccc5fa9f5e226add5a1fd70e91c6dfa4ddf Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Sun, 23 Apr 2017 14:02:10 +0100 Subject: [PATCH] Added error flag to validation errors and added age_range api endpoint --- lib/Pear/LocalLoop.pm | 4 ++++ lib/Pear/LocalLoop/Controller/Api/Info.pm | 12 ++++++++++++ lib/Pear/LocalLoop/Schema/Result/AgeRange.pm | 9 +++++++++ 3 files changed, 25 insertions(+) create mode 100644 lib/Pear/LocalLoop/Controller/Api/Info.pm diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 0449d52..4c54816 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -62,6 +62,7 @@ sub startup { json => { success => Mojo::JSON->false, message => $c->error_messages->{$val}->{$check}->{message}, + error => $check, }, status => $c->error_messages->{$val}->{$check}->{status}, ); @@ -98,6 +99,9 @@ sub startup { $r->post('/register')->to('register#register'); $r->any('/logout')->to('root#auth_logout'); + my $api_public_get = $r->under('/api'); + $api_public_get->get('/info/ages')->to('api-info#get_ages'); + # Always available api routes my $api_public = $r->under('/api')->to('api-auth#check_json'); diff --git a/lib/Pear/LocalLoop/Controller/Api/Info.pm b/lib/Pear/LocalLoop/Controller/Api/Info.pm new file mode 100644 index 0000000..949847b --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Api/Info.pm @@ -0,0 +1,12 @@ +package Pear::LocalLoop::Controller::Api::Info; +use Mojo::Base 'Mojolicious::Controller'; + +sub get_ages { + my $c = shift; + + my $ages = $c->schema->resultset('AgeRange'); + + $c->render( json => { ages => [ $ages->all ] } ); +} + +1; diff --git a/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm b/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm index b963f5c..73c4ffd 100644 --- a/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm +++ b/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm @@ -25,4 +25,13 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +sub TO_JSON { + my $self = shift; + + return { + id => $self->id, + string => $self->string, + }; +} + 1;