We need a way to specify that we want the count of the relation result and not the result itself, this way we don't need to implement *_count methods (see bug 24467)
Created attachment 98046 [details] [review] Bug 24528: Unit tests for Koha::Object->to_api This patch introduces unit tests for the new to_api bejaviour for *_count methods. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => FAIL: Tests fail because the feature is not implemented
Created attachment 98047 [details] [review] Bug 24528: Add *_count support to to_api This patch adds a way to tell to_api an attribute needs to be calculated as the count on an existing method/relationship result. For example, if we wanted to include the holds_count attribute for a Koha::Patron object, we would call it: $ patron_json = $patron->to_api({ embed => { holds_count => { is_count => 1 } } }); This way to_api will internally call $json->{holds_count} = $self->holds->count; To test: 1. Apply the tests patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => FAIL: Tests fail! 3. Apply this patch 4. Repeat (2) => SUCCESS: Tests pass! 5. Sign off :-D
Submitting the first bit once I got it for review. It is pretty simple, but I had a nightmare because I picked Koha::Biblio as an example... Will need to fix the overloaded to_api once I get this sorted. Next step is parsing relation+count on the API code, and adding is_count => 1 to the structure.
Created attachment 98065 [details] [review] Bug 24528: Add a syntax to x-koha-embed to specify counts This patch adds a + syntax to specify on the x-koha-embed header that we want the count of a relation in the response. For example: GET /patrons/1 x-koha-embed: checkouts+count Would return a JSON representation of a Koha::Patron, with a new attribute added: checkouts_count, which will be the result of calling $patron->checkouts->count. This is all done automatically in to_api. This patch makes parsing the x-koha-embed header build the right structure for passing to the to_api method. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/Koha/REST/Plugin/Query.t => SUCCESS: Tests pass! 3. Sign off :-D Sponsored-by: ByWater Solutions
Ready for testing!
Created attachment 98199 [details] [review] Bug 24528: Unit tests for Koha::Object->to_api This patch introduces unit tests for the new to_api bejaviour for *_count methods. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => FAIL: Tests fail because the feature is not implemented Signed-off-by: David Nind <david@davidnind.com>
Created attachment 98200 [details] [review] Bug 24528: Add *_count support to to_api This patch adds a way to tell to_api an attribute needs to be calculated as the count on an existing method/relationship result. For example, if we wanted to include the holds_count attribute for a Koha::Patron object, we would call it: $ patron_json = $patron->to_api({ embed => { holds_count => { is_count => 1 } } }); This way to_api will internally call $json->{holds_count} = $self->holds->count; To test: 1. Apply the tests patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => FAIL: Tests fail! 3. Apply this patch 4. Repeat (2) => SUCCESS: Tests pass! 5. Sign off :-D Signed-off-by: David Nind <david@davidnind.com>
Created attachment 98201 [details] [review] Bug 24528: Add a syntax to x-koha-embed to specify counts This patch adds a + syntax to specify on the x-koha-embed header that we want the count of a relation in the response. For example: GET /patrons/1 x-koha-embed: checkouts+count Would return a JSON representation of a Koha::Patron, with a new attribute added: checkouts_count, which will be the result of calling $patron->checkouts->count. This is all done automatically in to_api. This patch makes parsing the x-koha-embed header build the right structure for passing to the to_api method. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/Koha/REST/Plugin/Query.t => SUCCESS: Tests pass! 3. Sign off :-D Sponsored-by: ByWater Solutions Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: David Nind <david@davidnind.com>
QAing
Created attachment 98212 [details] [review] Bug 24528: Unit tests for Koha::Object->to_api This patch introduces unit tests for the new to_api bejaviour for *_count methods. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => FAIL: Tests fail because the feature is not implemented Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Created attachment 98213 [details] [review] Bug 24528: Add *_count support to to_api This patch adds a way to tell to_api an attribute needs to be calculated as the count on an existing method/relationship result. For example, if we wanted to include the holds_count attribute for a Koha::Patron object, we would call it: $ patron_json = $patron->to_api({ embed => { holds_count => { is_count => 1 } } }); This way to_api will internally call $json->{holds_count} = $self->holds->count; To test: 1. Apply the tests patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => FAIL: Tests fail! 3. Apply this patch 4. Repeat (2) => SUCCESS: Tests pass! 5. Sign off :-D Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Created attachment 98214 [details] [review] Bug 24528: Add a syntax to x-koha-embed to specify counts This patch adds a + syntax to specify on the x-koha-embed header that we want the count of a relation in the response. For example: GET /patrons/1 x-koha-embed: checkouts+count Would return a JSON representation of a Koha::Patron, with a new attribute added: checkouts_count, which will be the result of calling $patron->checkouts->count. This is all done automatically in to_api. This patch makes parsing the x-koha-embed header build the right structure for passing to the to_api method. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/Koha/REST/Plugin/Query.t => SUCCESS: Tests pass! 3. Sign off :-D Sponsored-by: ByWater Solutions Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
+ if ( $embed =~ m/^(?<relation>.*)_count$/ and + if ( $curr =~ m/^(?<relation>.*)\+count/ ) { + my $key = $+{relation} . "_count"; Why did not you keep "relation+count"?
(In reply to Jonathan Druart from comment #13) > + if ( $embed =~ m/^(?<relation>.*)_count$/ > > and > > + if ( $curr =~ m/^(?<relation>.*)\+count/ ) { > + my $key = $+{relation} . "_count"; > > Why did not you keep "relation+count"? Inertia. Coding at 2 am. I agree with keeping the +.
Nice work everyone! Pushed to master for 20.05
(In reply to Tomás Cohen Arazi from comment #14) > (In reply to Jonathan Druart from comment #13) > > + if ( $embed =~ m/^(?<relation>.*)_count$/ > > > > and > > > > + if ( $curr =~ m/^(?<relation>.*)\+count/ ) { > > + my $key = $+{relation} . "_count"; > > > > Why did not you keep "relation+count"? > > Inertia. Coding at 2 am. I agree with keeping the +. We decided to keep with the snake_case syntax as using + would (1) not follow the API naming conventions, and (2) it would become problematic to access the hash elements i.e. order.biblio+count means concatenating order.biblio and count.
Enhancement not backported to 19.11.x