From 3a4c0d89dd6a800156675c5b6c0a5491043b99dd Mon Sep 17 00:00:00 2001
From: Kyle Browning <kylebrowning@me.com>
Date: Tue, 22 Mar 2011 10:11:27 -0700
Subject: [PATCH] Re-roll of 1058934

---
 resources/comment_resource.inc |   55 ---------------------------------
 resources/node_resource.inc    |   66 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 55 deletions(-)

diff --git a/resources/comment_resource.inc b/resources/comment_resource.inc
index 94ce4ad..ecf7627 100644
--- a/resources/comment_resource.inc
+++ b/resources/comment_resource.inc
@@ -86,39 +86,6 @@ function _comment_resource_definition() {
         ),
       ),
       'actions' => array(
-        'loadNodeComments' => array(
-          'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
-          'help'   => t('This method returns the number of new comments on a given node since a given timestamp.'),
-          'access callback' => '_comment_resource_access',
-          'access arguments' => array('view'),
-          'access arguments append' => TRUE,
-          'callback' => '_comment_resource_load_node_comments',
-          'args'     => array(
-            array(
-              'name'         => 'nid',
-              'type'         => 'int',
-              'description'  => t('The node id to load comments for.'),
-              'source'       => 'data',
-              'optional'     => FALSE,
-            ),
-            array(
-              'name'         => 'count',
-              'type'         => 'int',
-              'description'  => t('Number of comments to load.'),
-              'source'       => 'data',
-              'optional'     => TRUE,
-              'default value'=> 10,
-            ),
-            array(
-              'name'         => 'start',
-              'type'         => 'int',
-              'description'  => t('If count is set to non-zero value, you can pass also non-zero value for start. For example to get comments from 5 to 15, pass count=10 and start=5.'),
-              'source'       => 'data',
-              'optional'     => TRUE,
-              'default value'=> 0,
-            ),
-          ),
-        ),
         'countAll' => array(
           'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
           'help'   => t('This method returns the number of comments on a given node.'),
@@ -276,28 +243,6 @@ function _comment_resource_delete($cid) {
 }
 
 /**
- * Returns the comments of a specified node.
- *
- * @param $nid
- *   Unique identifier for the node.
- * @param $count
- *   Number of comments to return.
- * @param $start
- *   Which comment to start with. if present, $start and $count are used together
- *   to create a LIMIT clause for selecting comments. This could be used to do paging.
- * @return
- *   An array of comment objects.
- */
-function _comment_resource_load_node_comments($nid, $count, $start) {
-  $comments = array();
-  $result = db_query_range('SELECT c.cid FROM {comment} c WHERE c.nid = :nid', $start, $count, array(':nid' => $nid));
-  foreach ($result as $record) {
-    $comments[] = comment_load($record->cid);
-  }
-  return $comments;
-}
-
-/**
  * Returns the number of comments on a given node id.
  *
  * @param $nid
diff --git a/resources/node_resource.inc b/resources/node_resource.inc
index 64ab64b..c2a52e7 100644
--- a/resources/node_resource.inc
+++ b/resources/node_resource.inc
@@ -130,6 +130,37 @@ function _node_resource_definition() {
             ),
           ),
         ),
+        'comments' => array(
+          'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
+          'help'   => t('This method returns the number of new comments on a given node.'),
+          'access callback' => 'user_access',
+          'access arguments' => array('access comments'),
+          'access arguments append' => FALSE,
+          'callback' => '_node_resource_load_node_comments',
+          'args'     => array(
+            array(
+              'name'         => 'nid',
+              'type'         => 'int',
+              'description'  => t('The node id to load comments for.'),
+              'source' => array('path' => 0),
+              'optional'     => FALSE,
+            ),
+            array(
+              'name'         => 'count',
+              'type'         => 'int',
+              'description'  => t('Number of comments to load.'),
+              'source' => array('path' => 2),
+              'optional'     => TRUE,
+            ),
+            array(
+              'name'         => 'start',
+              'type'         => 'int',
+              'description'  => t('If count is set to non-zero value, you can pass also non-zero value for start. For example to get comments from 5 to 15, pass count=10 and start=5.'),
+              'source' => array('path' => 3),
+              'optional'     => TRUE,
+            ),
+          ),
+        ),        
       ),
     ),
   );
@@ -424,3 +455,38 @@ function _node_resource_load_node_files($nid, $include_file_contents = TRUE) {
   return $files;
 }
 
+/**
+ * Returns the comments of a specified node.
+ *
+ * @param $nid
+ *   Unique identifier for the node.
+ * @param $count
+ *   Number of comments to return.
+ * @param $start
+ *   Which comment to start with. If present, $start and $count are used together
+ *   to create a LIMIT clause for selecting comments. This could be used to do paging.
+ * @return
+ *   An array of comment objects.
+ */
+function _node_resource_load_node_comments($nid, $count = 0, $start = 0) {
+  $comments = array();
+
+  $query = db_select('comment', 'c');
+  $query->innerJoin('node', 'n', 'n.nid = c.nid');
+  $query->addTag('node_access');
+  $query->fields('c', array('cid'))
+    ->condition('c.nid', $nid);
+
+  if ($count) {
+    $query->range($start, $count);
+  }
+
+  $result = $query->execute()
+    ->fetchAll();
+
+  foreach ($result as $record) {
+    $comments[] = comment_load($record->cid);
+  }
+
+  return $comments;
+}
-- 
1.7.3.4

