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