use crate::utils::encode_path;
use crate::{FilesClient, Result};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FileCommentEntity {
pub id: Option<i64>,
pub body: Option<String>,
pub reactions: Option<Vec<FileCommentReactionEntity>>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FileCommentReactionEntity {
pub id: Option<i64>,
pub emoji: Option<String>,
}
#[derive(Debug, Clone)]
pub struct FileCommentHandler {
client: FilesClient,
}
impl FileCommentHandler {
pub fn new(client: FilesClient) -> Self {
Self { client }
}
pub async fn list(&self, path: &str) -> Result<Vec<FileCommentEntity>> {
let encoded_path = encode_path(path);
let endpoint = format!("/file_comments/files{}", encoded_path);
let response = self.client.get_raw(&endpoint).await?;
Ok(serde_json::from_value(response)?)
}
pub async fn create(&self, path: &str, body: &str) -> Result<FileCommentEntity> {
let body_json = json!({
"body": body,
"path": path,
});
let response = self.client.post_raw("/file_comments", body_json).await?;
Ok(serde_json::from_value(response)?)
}
pub async fn update(&self, id: i64, body: &str) -> Result<FileCommentEntity> {
let body_json = json!({
"body": body,
});
let endpoint = format!("/file_comments/{}", id);
let response = self.client.patch_raw(&endpoint, body_json).await?;
Ok(serde_json::from_value(response)?)
}
pub async fn delete(&self, id: i64) -> Result<()> {
let endpoint = format!("/file_comments/{}", id);
self.client.delete_raw(&endpoint).await?;
Ok(())
}
pub async fn add_reaction(
&self,
file_comment_id: i64,
emoji: &str,
) -> Result<FileCommentReactionEntity> {
let body = json!({
"file_comment_id": file_comment_id,
"emoji": emoji,
});
let response = self
.client
.post_raw("/file_comment_reactions", body)
.await?;
Ok(serde_json::from_value(response)?)
}
pub async fn delete_reaction(&self, id: i64) -> Result<()> {
let endpoint = format!("/file_comment_reactions/{}", id);
self.client.delete_raw(&endpoint).await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_handler_creation() {
let client = FilesClient::builder().api_key("test-key").build().unwrap();
let _handler = FileCommentHandler::new(client);
}
}