chained translator with store
[catagits/catbook-code.git] / lib / LolCatalyst / Lite / Controller / Translate.pm
diff --git a/lib/LolCatalyst/Lite/Controller/Translate.pm b/lib/LolCatalyst/Lite/Controller/Translate.pm
new file mode 100644 (file)
index 0000000..8df27b7
--- /dev/null
@@ -0,0 +1,38 @@
+package LolCatalyst::Lite::Controller::Translate;
+
+use strict;
+use warnings;
+use parent qw(Catalyst::Controller);
+
+sub base :Chained('/') :PathPart('translate') :CaptureArgs(0) {
+  my ($self, $c) = @_;
+  $c->stash(collection => $c->model('SnippetStore'));
+}
+
+sub create :Chained('base') :PathPart('') :Args(0) {
+  my ($self, $c) = @_;
+  my $req = $c->req;
+  if ($req->method eq 'POST' && (my $lol = $req->body_params->{lol})) {
+    my $snippet = $c->stash->{collection}->create({ text => $lol });
+    $c->stash(object => $snippet);
+    $c->detach('view');
+  }
+}
+
+sub object :Chained('base') :PathPart('') :CaptureArgs(1) {
+  my ($self, $c, $id) = @_;
+  my $object = $c->stash->{collection}->find($id);
+  $c->detach('/error_404') unless $object;
+  $c->stash(object => $object);
+}
+
+sub view :Chained('object') :PathPart('') :Args(0) {
+  my ($self, $c) = @_;
+  my $object = $c->stash->{object};
+  $c->stash(
+    result => $c->model('Translator')
+                ->translate($object->{text})
+  );
+}
+
+1;