|
Lines 21-27
sub list {
Link Here
|
| 21 |
my $c = shift->openapi->valid_input or return; |
21 |
my $c = shift->openapi->valid_input or return; |
| 22 |
|
22 |
|
| 23 |
return try { |
23 |
return try { |
| 24 |
my $patron_id = $c->param('patron_id'); |
24 |
my $patron_id = $c->param('patron_id'); |
| 25 |
my $only_active = $c->param('only_active'); |
25 |
my $only_active = $c->param('only_active'); |
| 26 |
|
26 |
|
| 27 |
# Remove params we've handled |
27 |
# Remove params we've handled |
|
Lines 30-36
sub list {
Link Here
|
| 30 |
my $quotas_set = Koha::Patron::Quotas->new; |
30 |
my $quotas_set = Koha::Patron::Quotas->new; |
| 31 |
|
31 |
|
| 32 |
if ($patron_id) { |
32 |
if ($patron_id) { |
| 33 |
$quotas_set = $quotas_set->search({ patron_id => $patron_id }); |
33 |
$quotas_set = $quotas_set->search( { patron_id => $patron_id } ); |
| 34 |
} |
34 |
} |
| 35 |
|
35 |
|
| 36 |
if ($only_active) { |
36 |
if ($only_active) { |
|
Lines 38-48
sub list {
Link Here
|
| 38 |
} |
38 |
} |
| 39 |
|
39 |
|
| 40 |
return $c->render( |
40 |
return $c->render( |
| 41 |
status => 200, |
41 |
status => 200, |
| 42 |
openapi => $c->objects->search($quotas_set) |
42 |
openapi => $c->objects->search($quotas_set) |
| 43 |
); |
43 |
); |
| 44 |
} |
44 |
} catch { |
| 45 |
catch { |
|
|
| 46 |
$c->unhandled_exception($_); |
45 |
$c->unhandled_exception($_); |
| 47 |
}; |
46 |
}; |
| 48 |
} |
47 |
} |
|
Lines 58-69
sub get {
Link Here
|
| 58 |
|
57 |
|
| 59 |
return try { |
58 |
return try { |
| 60 |
my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') ); |
59 |
my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') ); |
| 61 |
|
60 |
|
| 62 |
return $c->render_resource_not_found("Quota") unless $quota; |
61 |
return $c->render_resource_not_found("Quota") unless $quota; |
| 63 |
|
62 |
|
| 64 |
return $c->render( status => 200, openapi => $c->objects->to_api($quota) ); |
63 |
return $c->render( status => 200, openapi => $c->objects->to_api($quota) ); |
| 65 |
} |
64 |
} catch { |
| 66 |
catch { |
|
|
| 67 |
$c->unhandled_exception($_); |
65 |
$c->unhandled_exception($_); |
| 68 |
}; |
66 |
}; |
| 69 |
} |
67 |
} |
|
Lines 80-97
sub add {
Link Here
|
| 80 |
return try { |
78 |
return try { |
| 81 |
my $body = $c->req->json; |
79 |
my $body = $c->req->json; |
| 82 |
$body->{patron_id} = $c->param('patron_id'); |
80 |
$body->{patron_id} = $c->param('patron_id'); |
| 83 |
|
81 |
|
| 84 |
my $quota = Koha::Patron::Quota->new_from_api($body); |
82 |
my $quota = Koha::Patron::Quota->new_from_api($body); |
| 85 |
$quota->store; |
83 |
$quota->store; |
| 86 |
|
84 |
|
| 87 |
$c->res->headers->location($c->req->url->to_string . '/' . $quota->id); |
85 |
$c->res->headers->location( $c->req->url->to_string . '/' . $quota->id ); |
| 88 |
return $c->render( |
86 |
return $c->render( |
| 89 |
status => 201, |
87 |
status => 201, |
| 90 |
openapi => $c->objects->to_api($quota) |
88 |
openapi => $c->objects->to_api($quota) |
| 91 |
); |
89 |
); |
| 92 |
} |
90 |
} catch { |
| 93 |
catch { |
91 |
if ( ref($_) eq 'Koha::Exceptions::Quota::Clash' ) { |
| 94 |
if (ref($_) eq 'Koha::Exceptions::Quota::Clash') { |
|
|
| 95 |
return $c->render( |
92 |
return $c->render( |
| 96 |
status => 409, |
93 |
status => 409, |
| 97 |
openapi => { error => "Quota period overlaps with existing quota" } |
94 |
openapi => { error => "Quota period overlaps with existing quota" } |
|
Lines 110-126
Controller method for updating a quota
Link Here
|
| 110 |
sub update { |
107 |
sub update { |
| 111 |
my $c = shift->openapi->valid_input or return; |
108 |
my $c = shift->openapi->valid_input or return; |
| 112 |
|
109 |
|
| 113 |
my $quota = Koha::Patron::Quotas->find($c->param('quota_id')); |
110 |
my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') ); |
| 114 |
|
111 |
|
| 115 |
return $c->render_resource_not_found("Quota") unless $quota; |
112 |
return $c->render_resource_not_found("Quota") unless $quota; |
| 116 |
|
113 |
|
| 117 |
return try { |
114 |
return try { |
| 118 |
$quota->set_from_api($c->req->json); |
115 |
$quota->set_from_api( $c->req->json ); |
| 119 |
$quota->store; |
116 |
$quota->store; |
| 120 |
return $c->render(status => 200, openapi => $c->objects->to_api($quota)); |
117 |
return $c->render( status => 200, openapi => $c->objects->to_api($quota) ); |
| 121 |
} |
118 |
} catch { |
| 122 |
catch { |
119 |
if ( ref($_) eq 'Koha::Exceptions::Quota::Clash' ) { |
| 123 |
if (ref($_) eq 'Koha::Exceptions::Quota::Clash') { |
|
|
| 124 |
return $c->render( |
120 |
return $c->render( |
| 125 |
status => 409, |
121 |
status => 409, |
| 126 |
openapi => { error => "Quota period overlaps with existing quota" } |
122 |
openapi => { error => "Quota period overlaps with existing quota" } |
|
Lines 139-153
Controller method for deleting a quota
Link Here
|
| 139 |
sub delete { |
135 |
sub delete { |
| 140 |
my $c = shift->openapi->valid_input or return; |
136 |
my $c = shift->openapi->valid_input or return; |
| 141 |
|
137 |
|
| 142 |
my $quota = Koha::Patron::Quotas->find($c->param('quota_id')); |
138 |
my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') ); |
| 143 |
|
139 |
|
| 144 |
return $c->render_resource_not_found("Quota") unless $quota; |
140 |
return $c->render_resource_not_found("Quota") unless $quota; |
| 145 |
|
141 |
|
| 146 |
return try { |
142 |
return try { |
| 147 |
$quota->delete; |
143 |
$quota->delete; |
| 148 |
return $c->render_resource_deleted; |
144 |
return $c->render_resource_deleted; |
| 149 |
} |
145 |
} catch { |
| 150 |
catch { |
146 |
$c->unhandled_exception($_); |
|
|
147 |
}; |
| 148 |
} |
| 149 |
|
| 150 |
=head3 get_usage |
| 151 |
|
| 152 |
Controller method for getting usage for a quota |
| 153 |
|
| 154 |
=cut |
| 155 |
|
| 156 |
sub get_usage { |
| 157 |
my $c = shift->openapi->valid_input or return; |
| 158 |
|
| 159 |
my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') ); |
| 160 |
return $c->render_resource_not_found("Quota") unless $quota; |
| 161 |
|
| 162 |
return try { |
| 163 |
my $usage_set = $quota->usages; |
| 164 |
return $c->render( |
| 165 |
status => 200, |
| 166 |
openapi => $c->objects->search($usage_set) |
| 167 |
); |
| 168 |
} catch { |
| 151 |
$c->unhandled_exception($_); |
169 |
$c->unhandled_exception($_); |
| 152 |
}; |
170 |
}; |
| 153 |
} |
171 |
} |