| 1 | # ABSTRACT: DAO request role - `fields` | ||||||||||
| 2 | package PONAPI::DAO::Request::Role::HasFields; | ||||||||||
| 3 | |||||||||||
| 4 | 8 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | has fields => ( | ||||||||||
| 7 | traits => [ 'Hash' ], | ||||||||||
| 8 | is => 'ro', | ||||||||||
| 9 | isa => 'HashRef', | ||||||||||
| 10 | 88 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 8 | handles => { | |||||||||
| 12 | "has_fields" => 'count', | ||||||||||
| 13 | }, | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | sub _validate_fields { | ||||||||||
| 17 | 97 | ▶ | my ( $self, $args ) = @_; | ||||||||
|
Subroutine
_validate_fields: called CC=10 SCAR=23.0
| |||||||||||
| 18 | |||||||||||
| 19 | 97 | ▶ | return unless defined $args->{fields}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 14 | ▶ | return unless $self->has_fields; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 14 | my $fields = $self->fields; | |||||||||
| 23 | 14 | my $repo = $self->repository; | |||||||||
| 24 | |||||||||||
| 25 | 14 | foreach my $fields_type ( keys %$fields ) { | |||||||||
| 26 | 18 | ▶ | if ( !$repo->has_type( $fields_type ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 2 | $self->_bad_request( "Type `$fields_type` doesn't exist.", 404 ); | |||||||||
| 28 | } | ||||||||||
| 29 | else { | ||||||||||
| 30 | 16 | my $fields_array = $fields->{$fields_type}; | |||||||||
| 31 | 16 | my @fields = @$fields_array; | |||||||||
| 32 | |||||||||||
| 33 | 16 | my $ok = $repo->type_has_fields( $fields_type, \@fields ); | |||||||||
| 34 | 16 | ▶ | next if $ok; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | # Sigh... let's test for this: | ||||||||||
| 37 | # fields => { articles => [qw/ title authors /] } | ||||||||||
| 38 | # where authors is a *relationship* | ||||||||||
| 39 | # There can't be clashes, so yes, this is fine. Somehow. | ||||||||||
| 40 | # http://jsonapi.org/format/#document-resource-object-fields | ||||||||||
| 41 | 5 | my (@real_fields, @relationships); | |||||||||
| 42 | 5 | foreach my $maybe_rel ( @fields ) { | |||||||||
| 43 | 6 | ▶ | if ( $repo->has_relationship($fields_type, $maybe_rel) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 2 | push @relationships, $maybe_rel; | |||||||||
| 45 | } | ||||||||||
| 46 | else { | ||||||||||
| 47 | 4 | push @real_fields, $maybe_rel; | |||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | $ok = @real_fields | ||||||||||
| 52 | 5 | ▶ | ? $repo->type_has_fields($fields_type, \@real_fields) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | : 1; | ||||||||||
| 54 | |||||||||||
| 55 | 5 | ▶ | if (!$ok) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 3 | $self->_bad_request( | |||||||||
| 57 | "Type `$fields_type` does not have at least one of the requested fields" | ||||||||||
| 58 | ); | ||||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 8 | ▶ | no Moose::Role; 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 65 | |||||||||||
| 66 | __END__ | ||||||||||
| 67 | |||||||||||
| 68 | =pod | ||||||||||
| 69 | |||||||||||
| 70 | =encoding UTF-8 | ||||||||||
| 71 | |||||||||||
| 72 | =head1 NAME | ||||||||||
| 73 | |||||||||||
| 74 | PONAPI::DAO::Request::Role::HasFields - DAO request role - `fields` | ||||||||||
| 75 | |||||||||||
| 76 | =head1 VERSION | ||||||||||
| 77 | |||||||||||
| 78 | version 0.003002 | ||||||||||
| 79 | |||||||||||
| 80 | =head1 AUTHORS | ||||||||||
| 81 | |||||||||||
| 82 | =over 4 | ||||||||||
| 83 | |||||||||||
| 84 | =item * | ||||||||||
| 85 | |||||||||||
| 86 | Mickey Nasriachi <mickey@cpan.org> | ||||||||||
| 87 | |||||||||||
| 88 | =item * | ||||||||||
| 89 | |||||||||||
| 90 | Stevan Little <stevan@cpan.org> | ||||||||||
| 91 | |||||||||||
| 92 | =item * | ||||||||||
| 93 | |||||||||||
| 94 | Brian Fraser <hugmeir@cpan.org> | ||||||||||
| 95 | |||||||||||
| 96 | =back | ||||||||||
| 97 | |||||||||||
| 98 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 99 | |||||||||||
| 100 | This software is copyright (c) 2017 by Mickey Nasriachi, Stevan Little, Brian Fraser. | ||||||||||
| 101 | |||||||||||
| 102 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 103 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||