OSDN Git Service

Controller:API::Comment: add 'get' method
authorhylom <hylom@users.sourceforge.jp>
Sat, 29 Oct 2016 14:24:39 +0000 (23:24 +0900)
committerhylom <hylom@users.sourceforge.jp>
Sat, 29 Oct 2016 14:24:39 +0000 (23:24 +0900)
src/newslash_web/lib/Newslash/Web/Controller/API/Comment.pm

index 5525d5f..c82c74a 100644 (file)
@@ -65,4 +65,37 @@ EOT
     }
 }
 
+sub get {
+    my $c = shift;
+
+    my $users = $c->model('users');
+    my $comments = $c->model('comments');
+    my $user = $c->stash('user');
+
+    my $data = $c->req->query_params->to_hash;
+    my ($key, $value);
+
+    if ($data->{discussion_id}) {
+        $key = 'discussion_id';
+        $value = $data->{discussion_id};
+    }
+    elsif ($data->{cid}) {
+        $key = 'cid';
+        $value = $data->{cid};
+    }
+    elsif ($data->{pid}) {
+        $key = 'pid';
+        $value = $data->{pid};
+    }
+
+    my $comments_ref = $comments->select($key => $value);
+    if (!defined $comments_ref) {
+        $c->render(json => { error => 1, message => 'invalid request' });
+        $c->rendered(400);
+        return;
+    }
+    $c->render(json => { $key => $value, comments => $comments_ref });
+    return;
+}
+
 1;