| Lines 25-35
          use Koha::Cities;
      
      
        Link Here | 
        
          | 25 | use Try::Tiny; | 25 | use Try::Tiny; | 
        
          | 26 |  | 26 |  | 
        
          | 27 | sub list { | 27 | sub list { | 
          
            
              | 28 |     my ( $c, $args, $cb ) = @_; | 28 |     my $c = shift->openapi->valid_input or return; | 
        
          | 29 |  | 29 |  | 
        
          | 30 |     my $cities; | 30 |     my $cities; | 
        
          | 31 |     my $filter; | 31 |     my $filter; | 
          
            
              | 32 |     $args //= {}; | 32 |     my $args = $c->req->params->to_hash; | 
        
          | 33 |  | 33 |  | 
        
          | 34 |     for my $filter_param ( keys %$args ) { | 34 |     for my $filter_param ( keys %$args ) { | 
        
          | 35 |         $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" }; | 35 |         $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" }; | 
  
    | Lines 37-132
          sub list {
      
      
        Link Here | 
        
          | 37 |  | 37 |  | 
        
          | 38 |     return try { | 38 |     return try { | 
        
          | 39 |         $cities = Koha::Cities->search($filter); | 39 |         $cities = Koha::Cities->search($filter); | 
          
            
              | 40 |         return $c->$cb( $cities, 200 ); | 40 |         return $c->render( status => 200, openapi => $cities ); | 
        
          | 41 |     } | 41 |     } | 
        
          | 42 |     catch { | 42 |     catch { | 
        
          | 43 |         if ( $_->isa('DBIx::Class::Exception') ) { | 43 |         if ( $_->isa('DBIx::Class::Exception') ) { | 
          
            
              | 44 |             return $c->$cb( { error => $_->{msg} }, 500 ); | 44 |             return $c->render( status  => 500, | 
            
              |  |  | 45 |                                openapi => { error => $_->msg } ); | 
        
          | 45 |         } | 46 |         } | 
        
          | 46 |         else { | 47 |         else { | 
          
            
              | 47 |             return $c->$cb( | 48 |             return $c->render( status => 500, | 
            
              | 48 |                 { error => "Something went wrong, check the logs." }, 500 ); | 49 |                 openapi => { error => "Something went wrong, check the logs."} ); | 
        
          | 49 |         } | 50 |         } | 
        
          | 50 |     }; | 51 |     }; | 
            
              |  |  | 52 |  | 
        
          | 51 | } | 53 | } | 
        
          | 52 |  | 54 |  | 
        
          | 53 | sub get { | 55 | sub get { | 
          
            
              | 54 |     my ( $c, $args, $cb ) = @_; | 56 |     my $c = shift->openapi->valid_input or return; | 
        
          | 55 |  | 57 |  | 
          
            
              | 56 |     my $city = Koha::Cities->find( $args->{cityid} ); | 58 |     my $city = Koha::Cities->find( $c->validation->param('cityid') ); | 
        
          | 57 |     unless ($city) { | 59 |     unless ($city) { | 
          
            
              | 58 |         return $c->$cb( { error => "City not found" }, 404 ); | 60 |         return $c->render( status  => 404, | 
            
              |  |  | 61 |                            openapi => { error => "City not found" } ); | 
        
          | 59 |     } | 62 |     } | 
        
          | 60 |  | 63 |  | 
          
            
              | 61 |     return $c->$cb( $city, 200 ); | 64 |     return $c->render( status => 200, openapi => $city ); | 
        
          | 62 | } | 65 | } | 
        
          | 63 |  | 66 |  | 
        
          | 64 | sub add { | 67 | sub add { | 
          
            
              | 65 |     my ( $c, $args, $cb ) = @_; | 68 |     my $c = shift->openapi->valid_input or return; | 
        
          | 66 |  | 69 |  | 
          
            
              | 67 |     my $city = Koha::City->new( $args->{body} ); | 70 |     my $city = Koha::City->new( $c->validation->param('body') ); | 
        
          | 68 |  | 71 |  | 
        
          | 69 |     return try { | 72 |     return try { | 
        
          | 70 |         $city->store; | 73 |         $city->store; | 
          
            
              | 71 |         return $c->$cb( $city, 200 ); | 74 |         return $c->render( status => 200, openapi => $city ); | 
        
          | 72 |     } | 75 |     } | 
        
          | 73 |     catch { | 76 |     catch { | 
        
          | 74 |         if ( $_->isa('DBIx::Class::Exception') ) { | 77 |         if ( $_->isa('DBIx::Class::Exception') ) { | 
          
            
              | 75 |             return $c->$cb( { error => $_->msg }, 500 ); | 78 |             return $c->render( status  => 500, | 
            
              |  |  | 79 |                                openapi => { error => $_->message } ); | 
        
          | 76 |         } | 80 |         } | 
        
          | 77 |         else { | 81 |         else { | 
          
            
              | 78 |             return $c->$cb( | 82 |             return $c->render( status => 500, | 
            
              | 79 |                 { error => "Something went wrong, check the logs." }, 500 ); | 83 |                 openapi => { error => "Something went wrong, check the logs."} ); | 
        
          | 80 |         } | 84 |         } | 
        
          | 81 |     }; | 85 |     }; | 
        
          | 82 | } | 86 | } | 
        
          | 83 |  | 87 |  | 
        
          | 84 | sub update { | 88 | sub update { | 
          
            
              | 85 |     my ( $c, $args, $cb ) = @_; | 89 |     my $c = shift->openapi->valid_input or return; | 
        
          | 86 |  | 90 |  | 
        
          | 87 |     my $city; | 91 |     my $city; | 
        
          | 88 |  | 92 |  | 
        
          | 89 |     return try { | 93 |     return try { | 
          
            
              | 90 |         $city = Koha::Cities->find( $args->{cityid} ); | 94 |         $city = Koha::Cities->find( $c->validation->param('cityid') ); | 
            
              | 91 |         $city->set( $args->{body} ); | 95 |         my $params = $c->req->json; | 
            
              |  |  | 96 |         $city->set( $params ); | 
        
          | 92 |         $city->store(); | 97 |         $city->store(); | 
          
            
              | 93 |         return $c->$cb( $city, 200 ); | 98 |         return $c->render( status => 200, openapi => $city ); | 
        
          | 94 |     } | 99 |     } | 
        
          | 95 |     catch { | 100 |     catch { | 
        
          | 96 |         if ( not defined $city ) { | 101 |         if ( not defined $city ) { | 
          
            
              | 97 |             return $c->$cb( { error => "Object not found" }, 404 ); | 102 |             return $c->render( status  => 404, | 
            
              |  |  | 103 |                                openapi => { error => "Object not found" } ); | 
        
          | 98 |         } | 104 |         } | 
        
          | 99 |         elsif ( $_->isa('Koha::Exceptions::Object') ) { | 105 |         elsif ( $_->isa('Koha::Exceptions::Object') ) { | 
          
            
              | 100 |             return $c->$cb( { error => $_->message }, 500 ); | 106 |             return $c->render( status  => 500, | 
            
              |  |  | 107 |                                openapi => { error => $_->message } ); | 
        
          | 101 |         } | 108 |         } | 
        
          | 102 |         else { | 109 |         else { | 
          
            
              | 103 |             return $c->$cb( | 110 |             return $c->render( status => 500, | 
            
              | 104 |                 { error => "Something went wrong, check the logs." }, 500 ); | 111 |                 openapi => { error => "Something went wrong, check the logs."} ); | 
        
          | 105 |         } | 112 |         } | 
        
          | 106 |     }; | 113 |     }; | 
        
          | 107 |  | 114 |  | 
        
          | 108 | } | 115 | } | 
        
          | 109 |  | 116 |  | 
        
          | 110 | sub delete { | 117 | sub delete { | 
          
            
              | 111 |     my ( $c, $args, $cb ) = @_; | 118 |     my $c = shift->openapi->valid_input or return; | 
        
          | 112 |  | 119 |  | 
        
          | 113 |     my $city; | 120 |     my $city; | 
        
          | 114 |  | 121 |  | 
        
          | 115 |     return try { | 122 |     return try { | 
          
            
              | 116 |         $city = Koha::Cities->find( $args->{cityid} ); | 123 |         $city = Koha::Cities->find( $c->validation->param('cityid') ); | 
        
          | 117 |         $city->delete; | 124 |         $city->delete; | 
          
            
              | 118 |         return $c->$cb( "", 200 ); | 125 |         return $c->render( status => 200, openapi => "" ); | 
        
          | 119 |     } | 126 |     } | 
        
          | 120 |     catch { | 127 |     catch { | 
        
          | 121 |         if ( not defined $city ) { | 128 |         if ( not defined $city ) { | 
          
            
              | 122 |             return $c->$cb( { error => "Object not found" }, 404 ); | 129 |             return $c->render( status  => 404, | 
            
              |  |  | 130 |                                openapi => { error => "Object not found" } ); | 
        
          | 123 |         } | 131 |         } | 
        
          | 124 |         elsif ( $_->isa('DBIx::Class::Exception') ) { | 132 |         elsif ( $_->isa('DBIx::Class::Exception') ) { | 
          
            
              | 125 |             return $c->$cb( { error => $_->msg }, 500 ); | 133 |             return $c->render( status  => 500, | 
            
              |  |  | 134 |                                openapi => { error => $_->msg } ); | 
        
          | 126 |         } | 135 |         } | 
        
          | 127 |         else { | 136 |         else { | 
          
            
              | 128 |             return $c->$cb( | 137 |             return $c->render( status => 500, | 
            
              | 129 |                 { error => "Something went wrong, check the logs." }, 500 ); | 138 |                 openapi => { error => "Something went wrong, check the logs."} ); | 
        
          | 130 |         } | 139 |         } | 
        
          | 131 |     }; | 140 |     }; | 
        
          | 132 |  | 141 |  |