Skip to main content

polyoxide_gamma/api/
comments.rs

1use polyoxide_core::{HttpClient, QueryBuilder, Request};
2
3use crate::{error::GammaError, types::Comment};
4
5/// Comments namespace for comment-related operations
6#[derive(Clone)]
7pub struct Comments {
8    pub(crate) http_client: HttpClient,
9}
10
11impl Comments {
12    /// List comments with optional filtering
13    pub fn list(&self) -> ListComments {
14        ListComments {
15            request: Request::new(self.http_client.clone(), "/comments"),
16        }
17    }
18
19    /// Get a comment by ID
20    pub fn get(&self, id: impl Into<String>) -> Request<Comment, GammaError> {
21        Request::new(
22            self.http_client.clone(),
23            format!("/comments/{}", urlencoding::encode(&id.into())),
24        )
25    }
26
27    /// Get comments by user address
28    pub fn by_user(&self, address: impl Into<String>) -> Request<Vec<Comment>, GammaError> {
29        Request::new(
30            self.http_client.clone(),
31            format!(
32                "/comments/user_address/{}",
33                urlencoding::encode(&address.into())
34            ),
35        )
36    }
37}
38
39/// Request builder for listing comments
40pub struct ListComments {
41    request: Request<Vec<Comment>, GammaError>,
42}
43
44impl ListComments {
45    /// Set maximum number of results (minimum: 0)
46    pub fn limit(mut self, limit: u32) -> Self {
47        self.request = self.request.query("limit", limit);
48        self
49    }
50
51    /// Set pagination offset (minimum: 0)
52    pub fn offset(mut self, offset: u32) -> Self {
53        self.request = self.request.query("offset", offset);
54        self
55    }
56
57    /// Set order fields (comma-separated list)
58    pub fn order(mut self, order: impl Into<String>) -> Self {
59        self.request = self.request.query("order", order.into());
60        self
61    }
62
63    /// Set sort direction
64    pub fn ascending(mut self, ascending: bool) -> Self {
65        self.request = self.request.query("ascending", ascending);
66        self
67    }
68
69    /// Filter by parent entity type (Event, Series, market)
70    pub fn parent_entity_type(mut self, entity_type: impl Into<String>) -> Self {
71        self.request = self.request.query("parent_entity_type", entity_type.into());
72        self
73    }
74
75    /// Filter by parent entity ID
76    pub fn parent_entity_id(mut self, id: i64) -> Self {
77        self.request = self.request.query("parent_entity_id", id);
78        self
79    }
80
81    /// Include position data in response
82    pub fn get_positions(mut self, include: bool) -> Self {
83        self.request = self.request.query("get_positions", include);
84        self
85    }
86
87    /// Restrict results to position holders only
88    pub fn holders_only(mut self, holders_only: bool) -> Self {
89        self.request = self.request.query("holders_only", holders_only);
90        self
91    }
92
93    /// Execute the request
94    pub async fn send(self) -> Result<Vec<Comment>, GammaError> {
95        self.request.send().await
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use crate::Gamma;
102
103    fn gamma() -> Gamma {
104        Gamma::new().unwrap()
105    }
106
107    #[test]
108    fn test_get_comment_accepts_str_and_string() {
109        let _req1 = gamma().comments().get("c-123");
110        let _req2 = gamma().comments().get(String::from("c-123"));
111    }
112
113    #[test]
114    fn test_by_user_accepts_str_and_string() {
115        let _req1 = gamma().comments().by_user("0xabc");
116        let _req2 = gamma().comments().by_user(String::from("0xabc"));
117    }
118
119    #[test]
120    fn test_list_comments_full_chain() {
121        let _req = gamma()
122            .comments()
123            .list()
124            .limit(10)
125            .offset(0)
126            .order("createdAt")
127            .ascending(false)
128            .parent_entity_type("Event")
129            .parent_entity_id(42)
130            .get_positions(true)
131            .holders_only(false);
132    }
133}