summaryrefslogtreecommitdiffstats
path: root/lib/Narwhal/Component/Wiki/Edit.pm
blob: fabeb0ce93f79b477b1e0e544c2329cbc120a91f (plain) (blame)
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
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;