View | Details | Raw Unified | Return to bug 28002
Collapse All | Expand All

(-)a/Koha/REST/V1/Patrons.pm (-40 / +86 lines)
Lines 19-24 use Modern::Perl; Link Here
19
19
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
21
22
use Koha::Database;
22
use Koha::DateUtils;
23
use Koha::DateUtils;
23
use Koha::Patrons;
24
use Koha::Patrons;
24
25
Lines 103-155 sub add { Link Here
103
104
104
    return try {
105
    return try {
105
106
106
        my $patron = Koha::Patron->new_from_api( $c->validation->param('body') )->store;
107
        Koha::Database->new->schema->txn_do(
108
            sub {
107
109
108
        $c->res->headers->location( $c->req->url->to_string . '/' . $patron->borrowernumber );
110
                my $body = $c->validation->param('body');
109
        return $c->render(
111
110
            status  => 201,
112
                my $extended_attributes = delete $body->{extended_attributes} // [];
111
            openapi => $patron->to_api
113
114
                my $patron = Koha::Patron->new_from_api($body)->store;
115
                $patron->extended_attributes(
116
                    [
117
                        map { { code => $_->{type}, attribute => $_->{value} } }
118
                          @$extended_attributes
119
                    ]
120
                );
121
122
                $c->res->headers->location($c->req->url->to_string . '/' . $patron->borrowernumber);
123
                return $c->render(
124
                    status  => 201,
125
                    openapi => $patron->to_api
126
                );
127
            }
112
        );
128
        );
113
    }
129
    }
114
    catch {
130
    catch {
115
131
116
        my $to_api_mapping = Koha::Patron->new->to_api_mapping;
132
        my $to_api_mapping = Koha::Patron->new->to_api_mapping;
117
133
118
        unless ( blessed $_ && $_->can('rethrow') ) {
134
        if ( blessed $_ ) {
119
            return $c->render(
135
            if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
120
                status  => 500,
136
                return $c->render(
121
                openapi => { error => "Something went wrong, check Koha logs for details." }
137
                    status  => 409,
122
            );
138
                    openapi => { error => $_->error, conflict => $_->duplicate_id }
123
        }
139
                );
124
        if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
140
            }
125
            return $c->render(
141
            elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
126
                status  => 409,
142
                return $c->render(
127
                openapi => { error => $_->error, conflict => $_->duplicate_id }
143
                    status  => 400,
128
            );
144
                    openapi => {
129
        }
145
                            error => "Given "
130
        elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
146
                            . $to_api_mapping->{ $_->broken_fk }
131
            return $c->render(
147
                            . " does not exist"
132
                status  => 400,
148
                    }
133
                openapi => {
149
                );
134
                          error => "Given "
150
            }
135
                        . $to_api_mapping->{ $_->broken_fk }
151
            elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
136
                        . " does not exist"
152
                return $c->render(
137
                }
153
                    status  => 400,
138
            );
154
                    openapi => {
139
        }
155
                            error => "Given "
140
        elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
156
                            . $to_api_mapping->{ $_->parameter }
141
            return $c->render(
157
                            . " does not exist"
142
                status  => 400,
158
                    }
143
                openapi => {
159
                );
144
                          error => "Given "
160
            }
145
                        . $to_api_mapping->{ $_->parameter }
161
            elsif (
146
                        . " does not exist"
162
                $_->isa('Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute')
147
                }
163
              )
148
            );
164
            {
149
        }
165
                return $c->render(
150
        else {
166
                    status  => 400,
151
            $c->unhandled_exception($_);
167
                    openapi => { error => "$_" }
168
                );
169
            }
170
            elsif (
171
                $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType')
172
              )
173
            {
174
                return $c->render(
175
                    status  => 400,
176
                    openapi => { error => "$_" }
177
                );
178
            }
179
            elsif (
180
                $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable')
181
              )
182
            {
183
                return $c->render(
184
                    status  => 400,
185
                    openapi => { error => "$_" }
186
                );
187
            }
188
            elsif (
189
                $_->isa('Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
190
              )
191
            {
192
                return $c->render(
193
                    status  => 400,
194
                    openapi => { error => "$_" }
195
                );
196
            }
152
        }
197
        }
198
199
        $c->unhandled_exception($_);
153
    };
200
    };
154
}
201
}
155
202
156
- 

Return to bug 28002