| 1 | package Rapi::Blog::DB::ResultSet::Section; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | extends 'DBIx::Class::ResultSet'; | ||
| 8 | |||
| 9 | use RapidApp::Util ':all'; | ||
| 10 | |||
| 11 | __PACKAGE__->load_components(qw(Helper::ResultSet::CorrelateRelationship)); | ||
| 12 | |||
| 13 | sub by_popularity { | ||
| 14 | my $self = shift; | ||
| 15 | |||
| 16 | $self->search_rs(undef, { | ||
| 17 | order_by => { '-desc' => $self->correlate('trk_section_posts')->count_rs->as_query }, | ||
| 18 | }) | ||
| 19 | } | ||
| 20 | |||
| 21 | sub alphabetically { | ||
| 22 | (shift)->search_rs(undef, { order_by => { '-asc' => 'me.name' } }) | ||
| 23 | } | ||
| 24 | |||
| 25 | __PACKAGE__->load_components('+Rapi::Blog::DB::Component::ResultSet::ListAPI'); | ||
| 26 | |||
| 27 | sub _api_default_params {{ limit => 200, sort => 'alphabetical' }} | ||
| 28 | sub _api_param_arg_order { [qw/search parent_id/] } | ||
| 29 | sub _api_params_undef_map {{ parent_id => 'none' }}; | ||
| 30 | |||
| 31 | # Method exposed to templates: | ||
| 32 | sub list_sections { | ||
| 33 | my ($self, @args) = @_; | ||
| 34 | |||
| 35 | my $P = $self->_list_api_params(@args); | ||
| 36 | |||
| 37 | my $Rs = $self | ||
| 38 | ->search_rs(undef,{ '+columns' => { | ||
| 39 | # pre-load 'posts_count' -- the Row class will use it (see Result::Section) | ||
| 40 | posts_count => $self->correlate('trk_section_posts') ->count_rs->as_query, | ||
| 41 | subsections_count => $self->correlate('trk_section_sections_sections') ->count_rs->as_query, | ||
| 42 | |||
| 43 | }}); | ||
| 44 | |||
| 45 | $P->{sort} ||= 'alphabetical'; | ||
| 46 | if($P->{sort} eq 'alphabetical') { | ||
| 47 | $Rs = $Rs->alphabetically; | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | $Rs = $Rs->by_popularity | ||
| 51 | } | ||
| 52 | |||
| 53 | $Rs = $Rs->search_rs( | ||
| 54 | { 'me.parent_id' => $P->{parent_id} }, | ||
| 55 | ) if (exists $P->{parent_id}); | ||
| 56 | |||
| 57 | $Rs = $Rs->search_rs( | ||
| 58 | { 'me.name' => { like => join('','%',$P->{search},'%') } } | ||
| 59 | ) if ($P->{search}); | ||
| 60 | |||
| 61 | return $Rs->_list_api; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | 1; |