]> git.ktnx.net Git - lsl.git/blobdiff - lib/App/LazyShoppingList/API/v1.pm
debug messages in development environment
[lsl.git] / lib / App / LazyShoppingList / API / v1.pm
index 8a30acbc11b227928739df9dcf2f9f7a869c8890..3e3565b9d1a9a8dfa45a96de10467e7c90d82abc 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 {
@@ -48,9 +46,7 @@ post '/list' => sub {
 
     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;
@@ -87,15 +83,15 @@ 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;
 
-    my @items = $dbh->resultset('ShoppingListItem')
-        ->search( undef, { order_by => { -asc => 'id' } } )->all;
+    my @items = $dbh->resultset('ShoppingListItem')->search(
+        { shopping_list => $list->id },
+        { order_by      => { -asc => 'id' } }
+    )->all;
     for my $item (@items) {
         push @{ $r{items} },
             {   uri =>
@@ -133,9 +129,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(
@@ -186,17 +180,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
@@ -205,9 +195,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,
                 sprintf( 'Outdated version (current is %d)', $item->version );
         }