]> git.ktnx.net Git - lsl.git/blobdiff - lib/App/LazyShoppingList/API/v1.pm
return exception responses as JSON
[lsl.git] / lib / App / LazyShoppingList / API / v1.pm
index 6e1cd3138a8b4a474dd5323c90e0912e974627bc..f94db2ea3d7aa4381723159345a4a618e160e671 100644 (file)
@@ -13,9 +13,7 @@ use JSON();
 use experimental 'signatures';
 
 set charset      => 'UTF-8';
-set serializer   => 'JSON';
-set content_type => 'application/json';
-
+set serializer => 'JSON';
 
 # get the URI for the list of shopping lists
 get '/' => sub {
@@ -34,7 +32,8 @@ get '/list' => sub {
             { order_by => { -asc => 'name' } } )->all
         )
     {
-        push @{ $r{lists} }, uri_for( "/list/" . $list->id );
+        push @{ $r{lists} },
+            { uri => uri_for( "/list/" . $list->id ), name => $list->name };
     }
 
     $dbh->txn_commit;
@@ -43,13 +42,11 @@ get '/list' => sub {
 
 # create shopping list
 post '/list' => sub {
-    my $req = decode_json(request_data);
+    my $req = request_data;
 
     my $name = $req->{name};
     unless ($name) {
-        status 400;
-        content_type 'text/plain';
-        return "Missing list name";
+        return exception 400, "Missing list name";
     }
 
     my $dbh = get_database;
@@ -86,9 +83,7 @@ get '/list/:list_id' => sub {
 
     unless ($list) {
         $dbh->txn_commit;
-        status 404;
-        content_type 'text/plain';
-        return "No list with that ID found";
+        return exception 404, "No list with that ID found";
     }
 
     $r{version} = $list->version;
@@ -116,7 +111,7 @@ post '/list/:id' => sub {
     length($list_id) and $list_id =~ /^\d{1,18}$/
         or return invalid_input('bad list ID');
 
-    my $req = decode_json(request_data);
+    my $req = request_data;
 
     my $descr = $req->{description};
     my $done  = JSON->boolean( $req->{done} // 0 );
@@ -132,9 +127,7 @@ post '/list/:id' => sub {
 
     unless ($list) {
         $dbh->txn_commit;
-        status 404;
-        content_type 'text/plain';
-        return "No such list";
+        exception 404, "No such list";
     }
 
     my $item = $dbh->resultset('ShoppingListItem')->create(
@@ -166,7 +159,7 @@ put '/list/:list_id/:item_id' => sub {
     length($item_id) and $item_id =~ /^\d{1,18}$/
         or return invalid_input('bad item ID');
 
-    my $req = decode_json(request_data);
+    my $req = request_data;
 
     my $descr = $req->{description};
     my $done  = JSON->boolean( $req->{done} // 0 );
@@ -185,17 +178,13 @@ put '/list/:list_id/:item_id' => sub {
     my $list = $dbh->resultset('ShoppingList')->find($list_id);
     unless ($list) {
         $dbh->txn_commit;
-        status 404;
-        content_type 'text/plain';
-        return "No such list";
+        return exception 404, "No such list";
     }
 
     my $item = $dbh->resultset('ShoppingListItem')->find({shopping_list => $list->id, id => $item_id});
     unless ($item) {
         $dbh->txn_commit;
-        status 404;
-        content_type 'text/plain';
-        return "No such item";
+        return exception 404, "No such item";
     }
 
     # in case no real changes are needed will return the current state
@@ -204,9 +193,7 @@ put '/list/:list_id/:item_id' => sub {
     {
         unless ($version == $item->version) {
             $dbh->txn_commit;
-            status 409;
-            content_type 'text/plain';
-            return
+            return exception 409, exception =>
                 sprintf( 'Outdated version (current is %d)', $item->version );
         }