fishpi_sdk/api/
comment.rs1use serde_json::{Value, json};
52
53use crate::{
54 model::article::CommentPost,
55 utils::{ResponseResult, error::Error, post, put},
56};
57
58pub struct Comment {
59 api_key: String,
60}
61
62impl Comment {
63 pub fn new(api_key: String) -> Self {
64 Self { api_key }
65 }
66
67 pub async fn send(&self, data: &CommentPost) -> Result<ResponseResult, Error> {
73 let url = "comment".to_string();
74
75 let mut data_json = data.to_value()?;
76 data_json["apiKey"] = Value::String(self.api_key.clone());
77
78 let rsp = post(&url, Some(data_json)).await?;
79
80 ResponseResult::from_value(&rsp)
81 }
82
83 pub async fn update(&self, id: &str, data: &CommentPost) -> Result<String, Error> {
90 let url = format!("comment/{}", id);
91
92 let mut data_json = data.to_value()?;
93 data_json["apiKey"] = Value::String(self.api_key.clone());
94
95 let rsp = put(&url, Some(data_json)).await?;
96
97 if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
98 return Err(Error::Api(
99 rsp["msg"].as_str().unwrap_or("API error").to_string(),
100 ));
101 }
102
103 Ok(rsp["commentContent"].as_str().unwrap_or("").to_string())
104 }
105
106 pub async fn vote(&self, id: &str, like: bool) -> Result<bool, Error> {
113 let action = if like { "up" } else { "down" };
114 let url = format!("vote/{}/comment", action);
115
116 let data_json = json!({
117 "dataId": id,
118 "apiKey": self.api_key,
119 });
120
121 let rsp = post(&url, Some(data_json)).await?;
122
123 if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
124 return Err(Error::Api(
125 rsp["msg"].as_str().unwrap_or("API error").to_string(),
126 ));
127 }
128
129 Ok(rsp["type"].as_i64().unwrap_or(-1) == 0)
130 }
131
132 pub async fn thank(&self, id: &str) -> Result<ResponseResult, Error> {
138 let url = "comment/thank".to_string();
139
140 let data_json = json!({
141 "apiKey": self.api_key,
142 "commentId": id,
143 });
144
145 let rsp = post(&url, Some(data_json)).await?;
146
147 ResponseResult::from_value(&rsp)
148 }
149
150 pub async fn remove(&self, id: &str) -> Result<String, Error> {
156 let url = format!("comment/{}/remove", id);
157
158 let data_json = json!({
159 "apiKey": self.api_key,
160 });
161
162 let rsp = post(&url, Some(data_json)).await?;
163
164 if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
165 return Err(Error::Api(
166 rsp["msg"].as_str().unwrap_or("API error").to_string(),
167 ));
168 }
169
170 Ok(rsp["commentId"].as_str().unwrap_or("").to_string())
171 }
172}