From a7c37d5489611bf79889dd689abb0ac686d640c9 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 17 Feb 2011 18:57:19 -0600 Subject: refactor this a bit --- lib/Narwhal/Component/Wiki/Edit.pm | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 lib/Narwhal/Component/Wiki/Edit.pm (limited to 'lib/Narwhal/Component/Wiki/Edit.pm') diff --git a/lib/Narwhal/Component/Wiki/Edit.pm b/lib/Narwhal/Component/Wiki/Edit.pm new file mode 100644 index 0000000..fabeb0c --- /dev/null +++ b/lib/Narwhal/Component/Wiki/Edit.pm @@ -0,0 +1,68 @@ +package Narwhal::Component::Wiki::Edit; +use Moose; + +has kioku => ( + isa => 'KiokuX::Model', + required => 1, + handles => 'KiokuDB::Role::API', +); + +has tt => ( + isa => 'Template', + required => 1, + handles => ['process'], +); + +has scope => ( + is => 'ro', + isa => 'KiokuDB::LiveObjects::Scope', + lazy => 1, + default => sub { shift->new_scope }, +); + +sub BUILD { + my $self = shift; + $self->scope; +} + +sub get { + my $self = shift; + my ($req, $page) = @_; + + my $page_obj = $self->lookup("page:$page"); + my $out; + $self->process( + 'edit.tt', + { + uri_for => sub { $req->uri_for({@_}) }, + text => ($page_obj ? $page_obj->text : ''), + page => $page + }, + \$out, + ); + return $req->new_response(200, [], $out); +} + +sub post { + my $self = shift; + my ($req, $page) = @_; + + my $page_obj = Narwhal::Page->new(text => $req->param('text')); + $self->txn_do(sub { + $self->delete("page:$page"); + $self->insert("page:$page" => $page_obj); + }); + my $res = $req->new_response(303); + $res->location( + $req->uri_for({ + action => 'page', + page_name => $page + }) + ); + return $res; +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; -- cgit v1.2.3-54-g00ecf