polyoxide_gamma/api/
comments.rs1use polyoxide_core::{HttpClient, QueryBuilder, Request};
2
3use crate::{error::GammaError, types::Comment};
4
5#[derive(Clone)]
7pub struct Comments {
8 pub(crate) http_client: HttpClient,
9}
10
11impl Comments {
12 pub fn list(&self) -> ListComments {
14 ListComments {
15 request: Request::new(self.http_client.clone(), "/comments"),
16 }
17 }
18
19 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 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
39pub struct ListComments {
41 request: Request<Vec<Comment>, GammaError>,
42}
43
44impl ListComments {
45 pub fn limit(mut self, limit: u32) -> Self {
47 self.request = self.request.query("limit", limit);
48 self
49 }
50
51 pub fn offset(mut self, offset: u32) -> Self {
53 self.request = self.request.query("offset", offset);
54 self
55 }
56
57 pub fn order(mut self, order: impl Into<String>) -> Self {
59 self.request = self.request.query("order", order.into());
60 self
61 }
62
63 pub fn ascending(mut self, ascending: bool) -> Self {
65 self.request = self.request.query("ascending", ascending);
66 self
67 }
68
69 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 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 pub fn get_positions(mut self, include: bool) -> Self {
83 self.request = self.request.query("get_positions", include);
84 self
85 }
86
87 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 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}