-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathBlog.pm
87 lines (59 loc) · 1.27 KB
/
Blog.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package Schema::ResultSet::Blog;
use Modern::Perl;
use base 'DBIx::Class::ResultSet';
sub by_id {
return shift->find({id => pop});
}
sub by_name {
return shift->find({title => {LIKE => pop}});
}
sub by_id_or_name {
my ($self, $param) = @_;
return $param =~ /^\d+$/ ? $self->by_id($param) : $self->by_name($param);
}
sub by_tags {
my ($self, @tags) = @_;
return if !@tags;
my $tags = $self->search_related(tags => {name => [@tags]});
return map $_->blog => $tags->all;
}
sub latest {
return
shift->not_hidden->search({},
{order_by => {-desc => 'created_time'}, rows => 1})->single;
}
sub hidden {
return shift->search(
{},
{ join => 'tags',
group_by => 'me.id',
where => {'tags.name' => 'hidden'}
}
);
}
sub not_hidden {
return shift->search(
{},
{ join => 'tags',
group_by => 'me.id',
where => {'tags.name' => {'!=' => 'hidden'}}
}
);
}
1;
=head1 NAME
Schema::ResultSet::Blog
=head1 METHODS
=head2 by_id
Find one blog entry by id
=head2 by_name
Find one blog entry by title
=head2 by_tags
Search blogs by related tags
=head2 latest
Most recently posted entry, chained from L<not_hidden>
=head2 hidden
Hidden entries (by 'hidden' tag)
=head2 not_hidden
Available entries
=cut