use HTML::Zoom::FilterBuilder::Template;
use List::Util qw(first);
-{
- package App::IdiotBox::Announcement;
-
- sub id { shift->{id} }
- sub made_at { shift->{made_at} }
- sub bucket { shift->{bucket} }
- sub video_count { shift->{video_count} }
-
- package App::IdiotBox::Bucket;
-
- sub slug { shift->{slug} }
- sub name { shift->{name} }
- sub video_count {
- exists $_[0]->{video_count}
- ? $_[0]->{video_count}
- : $_[0]->{videos}->count
- }
- sub videos { shift->{videos} }
-
- package App::IdiotBox::Video;
-
- sub slug { shift->{slug} }
- sub name { shift->{name} }
- sub author { shift->{author} }
- sub details { shift->{details} }
- sub bucket { shift->{bucket} }
- sub file_name {
- (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
- $s;
- }
- sub url_path {
- join('/', $_[0]->bucket->slug, $_[0]->slug);
- }
-}
+use App::IdiotBox::Announcement;
+use App::IdiotBox::Bucket;
+use App::IdiotBox::Video;
has $_ => (is => 'ro') for qw(recent_announcements buckets);
sub dispatch_request {
my $self = shift;
sub (/) { $self->show_front_page },
+
sub (/admin/) {
sub (%new_name=&new_slug=) {
my ($self, $name, $slug) = @_;
- unless ($name && $slug) {
- return $self->show_admin_page(error => "Please enter a name and a bucket");
- }
- if ($name =~ /^\s+$/ || $slug =~ /^\s+$/) {
- return $self->show_admin_page(error => "Names/buckets must not be all whitespace");
- }
-
- $slug =~ s/ /-/g;
-
- my $nb = $self->buckets->add(bless({
+ my ($nb, $err) = App::IdiotBox::Bucket->create(
slug => $slug,
name => $name,
- }, 'App::IdiotBox::Bucket'));
+ );
+
+ return $self->show_admin_page(error => $err) if $err;
+
+ my $nb = $self->buckets->add($nb);
$self->show_admin_page;
},
my $bucket = $self->buckets->get({ slug => $_[1] });
sub (%new_name=) {
my ($self, $new_name) = @_;
- if (!$new_name) {
- return $self->show_edit_bucket_page($bucket, error => "Please enter a new name");
- } elsif ($new_name =~ /^\s+$/) {
- return $self->show_edit_bucket_page($bucket, error => "Names must not be all whitespace");
- }
- $self->buckets->replace($bucket, bless({
- slug => $bucket->slug,
- name => $new_name,
- }, 'App::IdiotBox::Bucket'));
+
+ my ($ub, $err) = $bucket->update(
+ name => $new_name,
+ );
+
+ return $self->show_admin_page(error => $err) if $err;
+
+ $self->buckets->replace($bucket, $ub);
+
$self->show_bucket_edited_page($bucket);
},
sub (/) {
--- /dev/null
+package App::IdiotBox::Bucket;
+
+use Moo;
+
+sub fields { return qw(slug name) }
+
+with 'App::IdiotBox::Clonable';
+
+sub slug { shift->{slug} }
+sub name { shift->{name} }
+sub video_count {
+ exists $_[0]->{video_count}
+ ? $_[0]->{video_count}
+ : $_[0]->{videos}->count
+}
+sub videos { shift->{videos} }
+
+sub create {
+ my ($class, %args) = @_;
+
+ unless ($args{name} && $args{slug}) {
+ return (undef, "Please enter a name and a bucket");
+ }
+ if ($args{name} =~ /^\s+$/ || $args{slug} =~ /^\s+$/) {
+ return (undef, "Names/buckets must not be all whitespace");
+ }
+
+ $args{slug} =~ s/\s+/-/g;
+
+ return bless {
+ name => $args{name},
+ slug => $args{slug},
+ }, $class;
+}
+
+sub update {
+ my ($self, %args) = @_;
+
+ unless ($args{name}) {
+ return (undef, "Please enter a new name");
+ }
+
+ if ($args{name} =~ /^\s+$/) {
+ return (undef, "Names must not be all whitespace");
+ }
+
+ $self->{name} = $args{name};
+
+ $self->clone;
+}
+
+1;
--- /dev/null
+package App::IdiotBox::Video;
+
+use Moo;
+
+sub fields { return qw(slug bucket_slug name author details announcement_id) }
+
+sub slug { shift->{slug} }
+sub name { shift->{name} }
+sub author { shift->{author} }
+sub details { shift->{details} }
+sub bucket { shift->{bucket} }
+sub file_name {
+ (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
+ $s;
+}
+sub url_path {
+ join('/', $_[0]->bucket->slug, $_[0]->slug);
+}
+
+1;