mangadex_api/v5/statistics/group/
get.rs1use derive_builder::Builder;
34use serde::Serialize;
35use uuid::Uuid;
36
37use crate::HttpClientRef;
38use mangadex_api_schema::v5::GroupStatisticsResponse;
39
40#[cfg_attr(
41 feature = "deserializable-endpoint",
42 derive(serde::Deserialize, getset::Getters, getset::Setters)
43)]
44#[derive(Debug, Serialize, Clone, Builder, Default)]
45#[serde(rename_all = "camelCase")]
46#[builder(
47 setter(into, strip_option),
48 build_fn(error = "mangadex_api_types::error::BuilderError"),
49 default
50)]
51#[cfg_attr(feature = "non_exhaustive", non_exhaustive)]
52pub struct FindGroupStatistics {
53 #[doc(hidden)]
54 #[serde(skip)]
55 #[builder(pattern = "immutable")]
56 #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))]
57 pub http_client: HttpClientRef,
58
59 #[builder(setter(each = "group_id"))]
60 pub group: Vec<Uuid>,
61}
62
63endpoint! {
64 GET "/statistics/group",
65 #[query] FindGroupStatistics,
68 #[flatten_result] GroupStatisticsResponse,
69 FindGroupStatisticsBuilder
70}
71
72#[cfg(test)]
73mod tests {
74 use serde_json::json;
75 use url::Url;
76 use uuid::Uuid;
77 use wiremock::matchers::{method, path};
78 use wiremock::{Mock, MockServer, ResponseTemplate};
79
80 use crate::{HttpClient, MangaDexClient};
81
82 #[tokio::test]
83 async fn find_group_statistics_fires_a_request_to_base_url() -> anyhow::Result<()> {
84 let mock_server = MockServer::start().await;
85 let http_client = HttpClient::builder()
86 .base_url(Url::parse(&mock_server.uri())?)
87 .build()?;
88 let mangadex_client = MangaDexClient::new_with_http_client(http_client);
89
90 let manga_id = Uuid::new_v4();
91
92 let thread_id = 4756728;
93 let replies_count = 12;
94
95 let response_body = json!({
96 "result": "ok",
97 "statistics": {
98 manga_id.to_string(): {
99 "comments": {
100 "threadId": thread_id,
101 "repliesCount": replies_count
102 }
103 }
104 }
105 });
106
107 Mock::given(method("GET"))
108 .and(path("/statistics/group"))
109 .respond_with(ResponseTemplate::new(200).set_body_json(response_body))
110 .expect(1)
111 .mount(&mock_server)
112 .await;
113
114 let res = mangadex_client
115 .statistics()
116 .group()
117 .get()
118 .group_id(&manga_id)
119 .send()
120 .await?;
121 let ctt = res.statistics.get(&manga_id).ok_or(std::io::Error::new(
122 std::io::ErrorKind::NotFound,
123 "This id is not found",
124 ))?;
125 let comments = ctt.comments.ok_or(std::io::Error::new(
126 std::io::ErrorKind::NotFound,
127 "The comment is not found",
128 ))?;
129 assert_eq!(comments.thread_id, thread_id);
130 assert_eq!(comments.replies_count, replies_count);
131 Ok(())
132 }
133}