749b3acc0571b6f25962c8c04e6ed65fd443f0bd
[catagits/catbook-code.git] / lib / LolCatalyst / Lite / Controller / Translate.pm
1 package LolCatalyst::Lite::Controller::Translate;
2
3 use strict;
4 use warnings;
5 use parent qw(Catalyst::Controller);
6
7 sub base :Chained('/') :PathPart('translate') :CaptureArgs(0) {
8   my ($self, $c) = @_;
9   $c->stash(collection => $c->model('SnippetStore'));
10 }
11
12 sub create :Chained('base') :PathPart('') :Args(0) {
13   my ($self, $c) = @_;
14   my $req = $c->req;
15   if ($req->method eq 'POST' && (my $lol = $req->body_params->{lol})) {
16     my $snippet = $c->stash->{collection}->create({ text => $lol });
17     $c->stash(object => $snippet);
18     $c->detach('view');
19   }
20 }
21
22 sub object :Chained('base') :PathPart('') :CaptureArgs(1) {
23   my ($self, $c, $id) = @_;
24   my $object = $c->stash->{collection}->find($id);
25   $c->detach('/error_404') unless $object;
26   $c->stash(object => $object);
27 }
28
29 sub view :Chained('object') :PathPart('') :Args(0) {
30   my ($self, $c) = @_;
31   my $object = $c->stash->{object};
32   $c->stash(
33     result => $object->translated
34   );
35 }
36
37 sub translate_to :Chained('object') :PathPart('to') :Args(1) {
38   my ($self, $c, $to) = @_;
39   my $object = $c->stash->{object};
40   $c->stash(
41     result => $object->translated_to($to)
42   );
43   $c->stash(template => 'translate/view.tt');
44 }
45
46 1;