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

(-)a/Koha/REST/V1/Patrons/Quotas.pm (+155 lines)
Line 0 Link Here
1
package Koha::REST::V1::Patrons::Quotas;
2
3
use Modern::Perl;
4
use Mojo::Base 'Mojolicious::Controller';
5
6
use Koha::Patron::Quotas;
7
8
use Try::Tiny qw( catch try );
9
10
=head1 API
11
12
=head2 Methods
13
14
=head3 list
15
16
Controller method for listing patron quotas
17
18
=cut
19
20
sub list {
21
    my $c = shift->openapi->valid_input or return;
22
23
    return try {
24
        my $patron_id = $c->param('patron_id'); 
25
        my $only_active = $c->param('only_active');
26
27
        # Remove params we've handled
28
        $c->req->params->remove('patron_id')->remove('only_active');
29
30
        my $quotas_set = Koha::Patron::Quotas->new;
31
32
        if ($patron_id) {
33
            $quotas_set = $quotas_set->search({ patron_id => $patron_id });
34
        }
35
36
        if ($only_active) {
37
            $quotas_set = $quotas_set->get_active_quotas;
38
        }
39
40
        return $c->render(
41
            status => 200,
42
            openapi => $c->objects->search($quotas_set)
43
        );
44
    }
45
    catch {
46
        $c->unhandled_exception($_);
47
    };
48
}
49
50
=head3 get
51
52
Controller method for retrieving a single quota
53
54
=cut
55
56
sub get {
57
    my $c = shift->openapi->valid_input or return;
58
59
    return try {
60
        my $quota = Koha::Patron::Quotas->find( $c->param('quota_id') );
61
        
62
        return $c->render_resource_not_found("Quota") unless $quota;
63
64
        return $c->render( status => 200, openapi => $c->objects->to_api($quota) );
65
    }
66
    catch {
67
        $c->unhandled_exception($_);
68
    };
69
}
70
71
=head3 add
72
73
Controller method for adding a new quota
74
75
=cut
76
77
sub add {
78
    my $c = shift->openapi->valid_input or return;
79
80
    return try {
81
        my $body = $c->req->json;
82
        $body->{patron_id} = $c->param('patron_id');
83
        
84
        my $quota = Koha::Patron::Quota->new_from_api($body);
85
        $quota->store;
86
87
        $c->res->headers->location($c->req->url->to_string . '/' . $quota->id);
88
        return $c->render(
89
            status  => 201,
90
            openapi => $c->objects->to_api($quota)
91
        );
92
    }
93
    catch {
94
        if (ref($_) eq 'Koha::Exceptions::Quota::Clash') {
95
            return $c->render(
96
                status  => 409,
97
                openapi => { error => "Quota period overlaps with existing quota" }
98
            );
99
        }
100
        $c->unhandled_exception($_);
101
    };
102
}
103
104
=head3 update
105
106
Controller method for updating a quota
107
108
=cut
109
110
sub update {
111
    my $c = shift->openapi->valid_input or return;
112
113
    my $quota = Koha::Patron::Quotas->find($c->param('quota_id'));
114
    
115
    return $c->render_resource_not_found("Quota") unless $quota;
116
117
    return try {
118
        $quota->set_from_api($c->req->json);
119
        $quota->store;
120
        return $c->render(status => 200, openapi => $c->objects->to_api($quota));
121
    }
122
    catch {
123
        if (ref($_) eq 'Koha::Exceptions::Quota::Clash') {
124
            return $c->render(
125
                status  => 409,
126
                openapi => { error => "Quota period overlaps with existing quota" }
127
            );
128
        }
129
        $c->unhandled_exception($_);
130
    };
131
}
132
133
=head3 delete
134
135
Controller method for deleting a quota
136
137
=cut
138
139
sub delete {
140
    my $c = shift->openapi->valid_input or return;
141
142
    my $quota = Koha::Patron::Quotas->find($c->param('quota_id'));
143
    
144
    return $c->render_resource_not_found("Quota") unless $quota;
145
146
    return try {
147
        $quota->delete;
148
        return $c->render_resource_deleted;
149
    }
150
    catch {
151
        $c->unhandled_exception($_);
152
    };
153
}
154
155
1;
(-)a/api/v1/swagger/definitions/quota.yaml (+34 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  id:
5
    type: integer
6
    description: Internal quota identifier
7
    readOnly: true
8
  description:
9
    type: string
10
    description: User friendly description of the quota
11
  patron_id:
12
    type: integer
13
    description: Link to patron
14
    readOnly: true
15
  start_date:
16
    type: string
17
    format: date
18
    description: Start date of quota period
19
  end_date:
20
    type: string
21
    format: date
22
    description: End date of quota period
23
  allocation:
24
    type: integer
25
    description: Total quota amount for period
26
  used:
27
    type: integer
28
    description: Amount of quota used
29
additionalProperties: false
30
required:
31
  - patron_id
32
  - start_date
33
  - end_date
34
  - allocation
(-)a/api/v1/swagger/paths/patrons_quotas.yaml (+199 lines)
Line 0 Link Here
1
---
2
/patrons/{patron_id}/quotas:
3
  post:
4
    x-mojo-to: Patrons::Quotas#add
5
    operationId: addPatronQuota
6
    tags:
7
      - quotas
8
    summary: Add new quota for patron
9
    parameters:
10
      - $ref: "../swagger.yaml#/parameters/patron_id_pp"
11
      - name: body
12
        in: body
13
        description: A JSON object containing quota information
14
        required: true
15
        schema:
16
          $ref: "../swagger.yaml#/definitions/quota"
17
    produces:
18
      - application/json
19
    responses:
20
      "201":
21
        description: Quota created
22
        schema:
23
          $ref: "../swagger.yaml#/definitions/quota"
24
      "400":
25
        description: Bad request
26
        schema:
27
          $ref: "../swagger.yaml#/definitions/error"
28
      "404":
29
        description: Patron not found
30
        schema:
31
          $ref: "../swagger.yaml#/definitions/error"
32
      "409":
33
        description: Quota period clash
34
        schema:
35
          $ref: "../swagger.yaml#/definitions/error"
36
      "500":
37
        description: Internal server error
38
        schema:
39
          $ref: "../swagger.yaml#/definitions/error"
40
    x-koha-authorization:
41
      permissions:
42
        borrowers: manage_borrower_quotas
43
  get:
44
    x-mojo-to: Patrons::Quotas#list
45
    operationId: listQuotas
46
    tags:
47
      - quotas
48
    summary: List all quotas
49
    parameters:
50
      - $ref: "../swagger.yaml#/parameters/patron_id_pp"
51
      - name: only_active
52
        in: query
53
        description: Only return active quotas
54
        required: false
55
        type: boolean
56
      - $ref: "../swagger.yaml#/parameters/page"
57
      - $ref: "../swagger.yaml#/parameters/per_page"
58
      - $ref: "../swagger.yaml#/parameters/match"
59
      - $ref: "../swagger.yaml#/parameters/order_by"
60
      - $ref: "../swagger.yaml#/parameters/q_param"
61
      - $ref: "../swagger.yaml#/parameters/q_body"
62
      - $ref: "../swagger.yaml#/parameters/request_id_header"
63
    produces:
64
      - application/json
65
    responses:
66
      "200":
67
        description: A list of quotas
68
        schema:
69
          type: array
70
          items:
71
            $ref: "../swagger.yaml#/definitions/quota"
72
      "400":
73
        description: |
74
          Bad request. Possible `error_code` attribute values:
75
            * `bad_request`
76
            * `invalid_query`
77
        schema:
78
          $ref: "../swagger.yaml#/definitions/error"
79
      "403":
80
        description: Access forbidden
81
        schema:
82
          $ref: "../swagger.yaml#/definitions/error"
83
      "500":
84
        description: Internal server error
85
        schema:
86
          $ref: "../swagger.yaml#/definitions/error"
87
    x-koha-authorization:
88
      permissions:
89
        borrowers: view_borrower_quotas
90
/patrons/{patron_id}/quotas/{quota_id}:
91
  get:
92
    x-mojo-to: Patrons::Quotas#get
93
    operationId: getPatronQuota
94
    tags:
95
      - quotas
96
    summary: Get quota details
97
    parameters:
98
      - $ref: "../swagger.yaml#/parameters/patron_id_pp"
99
      - name: quota_id
100
        in: path
101
        description: Internal quota identifier
102
        required: true
103
        type: string
104
    produces:
105
      - application/json
106
    responses:
107
      "200":
108
        description: A quota
109
        schema:
110
          $ref: "../swagger.yaml#/definitions/quota"
111
      "400":
112
        description: Bad request
113
        schema:
114
          $ref: "../swagger.yaml#/definitions/error"
115
      "404":
116
        description: Quota not found
117
        schema:
118
          $ref: "../swagger.yaml#/definitions/error"
119
      "500":
120
        description: Internal server error
121
        schema:
122
          $ref: "../swagger.yaml#/definitions/error"
123
    x-koha-authorization:
124
      permissions:
125
        borrowers: view_borrower_quotas
126
  put:
127
    x-mojo-to: Patrons::Quotas#update
128
    operationId: updatePatronQuota
129
    tags:
130
      - quotas
131
    summary: Update quota
132
    parameters:
133
      - $ref: "../swagger.yaml#/parameters/patron_id_pp"
134
      - $ref: "../swagger.yaml#/parameters/quota_id_pp"
135
      - name: body
136
        in: body
137
        description: A JSON object containing quota information
138
        required: true
139
        schema:
140
          $ref: "../swagger.yaml#/definitions/quota"
141
    produces:
142
      - application/json
143
    responses:
144
      "200":
145
        description: Quota updated
146
        schema:
147
          $ref: "../swagger.yaml#/definitions/quota"
148
      "400":
149
        description: Bad request
150
        schema:
151
          $ref: "../swagger.yaml#/definitions/error"
152
      "404":
153
        description: Quota not found
154
        schema:
155
          $ref: "../swagger.yaml#/definitions/error"
156
      "409":
157
        description: Quota period clash
158
        schema:
159
          $ref: "../swagger.yaml#/definitions/error"
160
      "500":
161
        description: Internal server error
162
        schema:
163
          $ref: "../swagger.yaml#/definitions/error"
164
    x-koha-authorization:
165
      permissions:
166
        borrowers: manage_borrower_quotas
167
  delete:
168
    x-mojo-to: Patrons::Quotas#delete
169
    operationId: deletePatronQuota
170
    tags:
171
      - quotas
172
    summary: Delete quota
173
    parameters:
174
      - $ref: "../swagger.yaml#/parameters/patron_id_pp"
175
      - name: quota_id
176
        in: path
177
        description: Internal quota identifier
178
        required: true
179
        type: integer
180
    produces:
181
      - application/json
182
    responses:
183
      "204":
184
        description: Quota deleted
185
      "400":
186
        description: Bad request
187
        schema:
188
          $ref: "../swagger.yaml#/definitions/error"
189
      "404":
190
        description: Quota not found
191
        schema:
192
          $ref: "../swagger.yaml#/definitions/error"
193
      "500":
194
        description: Internal server error
195
        schema:
196
          $ref: "../swagger.yaml#/definitions/error"
197
    x-koha-authorization:
198
      permissions:
199
        borrowers: manage_borrower_quotas
(-)a/api/v1/swagger/swagger.yaml (-1 / +15 lines)
Lines 166-171 definitions: Link Here
166
    $ref: ./definitions/preservation_processing.yaml
166
    $ref: ./definitions/preservation_processing.yaml
167
  quote:
167
  quote:
168
    $ref: ./definitions/quote.yaml
168
    $ref: ./definitions/quote.yaml
169
  quota:
170
    $ref: ./definitions/quota.yaml
169
  recall:
171
  recall:
170
    $ref: ./definitions/recall.yaml
172
    $ref: ./definitions/recall.yaml
171
  recalls:
173
  recalls:
Lines 519-524 paths: Link Here
519
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password"
521
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password"
520
  "/patrons/{patron_id}/password/expiration_date":
522
  "/patrons/{patron_id}/password/expiration_date":
521
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password~1expiration_date"
523
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password~1expiration_date"
524
  "/patrons/{patron_id}/quotas":
525
    $ref: "./paths/patrons_quotas.yaml#/~1patrons~1{patron_id}~1quotas"
526
  "/patrons/{patron_id}/quotas/{quota_id}":
527
    $ref: "./paths/patrons_quotas.yaml#/~1patrons~1{patron_id}~1quotas~1{quota_id}"
522
  "/patrons/{patron_id}/recalls":
528
  "/patrons/{patron_id}/recalls":
523
    $ref: "./paths/patrons_recalls.yaml#/~1patrons~1{patron_id}~1recalls"
529
    $ref: "./paths/patrons_recalls.yaml#/~1patrons~1{patron_id}~1recalls"
524
  "/patrons/{patron_id}/ill/requests":
530
  "/patrons/{patron_id}/ill/requests":
Lines 973-978 parameters: Link Here
973
    name: quote_id
979
    name: quote_id
974
    required: true
980
    required: true
975
    type: integer
981
    type: integer
982
  quota_id_pp:
983
    description: Internal quota identifier
984
    in: path
985
    name: quota_id
986
    required: true
987
    type: integer
976
  record_source_id_header:
988
  record_source_id_header:
977
    description: Internal record source identifier.
989
    description: Internal record source identifier.
978
    name: x-record-source-id
990
    name: x-record-source-id
Lines 1374-1379 tags: Link Here
1374
  - description: "Manage purchase suggestions\n"
1386
  - description: "Manage purchase suggestions\n"
1375
    name: suggestions
1387
    name: suggestions
1376
    x-displayName: Purchase suggestions
1388
    x-displayName: Purchase suggestions
1389
  - description: "Manage quotas\n"
1390
    name: quotas
1391
    x-displayName: Quotas
1377
  - description: "Manage quotes\n"
1392
  - description: "Manage quotes\n"
1378
    name: quotes
1393
    name: quotes
1379
    x-displayName: Quotes
1394
    x-displayName: Quotes
1380
- 

Return to bug 38924