1#![allow(clippy::too_many_arguments)]
13
14use crate::client::Ghl;
15use crate::error::Result;
16use ghl_models::v2::blogs as models;
17
18#[derive(Debug, Clone)]
21pub struct BlogsService {
22 pub(crate) client: Ghl,
23}
24
25impl BlogsService {
26 pub(crate) fn new(client: Ghl) -> Self {
27 Self { client }
28 }
29}
30
31#[derive(Debug, Clone, Default)]
33pub struct GetAllAuthorsParams {
34 pub location_id: String,
37 pub limit: f64,
40 pub offset: f64,
43}
44
45impl GetAllAuthorsParams {
46 pub fn new(location_id: impl Into<String>, limit: f64, offset: f64) -> Self {
48 Self {
49 location_id: location_id.into(),
50 limit,
51 offset,
52 }
53 }
54
55 fn to_query(&self) -> Vec<(String, String)> {
56 let q: Vec<(String, String)> = vec![
57 ("locationId".into(), self.location_id.clone()),
58 ("limit".into(), self.limit.to_string()),
59 ("offset".into(), self.offset.to_string()),
60 ];
61 q
62 }
63}
64
65#[derive(Debug, Clone, Default)]
67pub struct GetAllCategoriesParams {
68 pub location_id: String,
71 pub limit: f64,
74 pub offset: f64,
77}
78
79impl GetAllCategoriesParams {
80 pub fn new(location_id: impl Into<String>, limit: f64, offset: f64) -> Self {
82 Self {
83 location_id: location_id.into(),
84 limit,
85 offset,
86 }
87 }
88
89 fn to_query(&self) -> Vec<(String, String)> {
90 let q: Vec<(String, String)> = vec![
91 ("locationId".into(), self.location_id.clone()),
92 ("limit".into(), self.limit.to_string()),
93 ("offset".into(), self.offset.to_string()),
94 ];
95 q
96 }
97}
98
99#[derive(Debug, Clone, Default)]
101pub struct GetBlogPostsByBlogIdParams {
102 pub location_id: String,
105 pub blog_id: String,
108 pub limit: f64,
111 pub offset: f64,
114 pub search_term: Option<String>,
116 pub status: Option<String>,
119}
120
121impl GetBlogPostsByBlogIdParams {
122 pub fn new(
124 location_id: impl Into<String>,
125 blog_id: impl Into<String>,
126 limit: f64,
127 offset: f64,
128 ) -> Self {
129 Self {
130 location_id: location_id.into(),
131 blog_id: blog_id.into(),
132 limit,
133 offset,
134 ..Default::default()
135 }
136 }
137
138 pub fn search_term(mut self, v: impl Into<String>) -> Self {
140 self.search_term = Some(v.into());
141 self
142 }
143
144 pub fn status(mut self, v: impl Into<String>) -> Self {
146 self.status = Some(v.into());
147 self
148 }
149
150 fn to_query(&self) -> Vec<(String, String)> {
151 let mut q: Vec<(String, String)> = vec![
152 ("locationId".into(), self.location_id.clone()),
153 ("blogId".into(), self.blog_id.clone()),
154 ("limit".into(), self.limit.to_string()),
155 ("offset".into(), self.offset.to_string()),
156 ];
157 if let Some(v) = &self.search_term {
158 q.push(("searchTerm".into(), v.to_string()));
159 }
160 if let Some(v) = &self.status {
161 q.push(("status".into(), v.to_string()));
162 }
163 q
164 }
165}
166
167#[derive(Debug, Clone, Default)]
169pub struct CheckUrlSlugParams {
170 pub url_slug: String,
173 pub location_id: String,
176 pub post_id: Option<String>,
178}
179
180impl CheckUrlSlugParams {
181 pub fn new(url_slug: impl Into<String>, location_id: impl Into<String>) -> Self {
183 Self {
184 url_slug: url_slug.into(),
185 location_id: location_id.into(),
186 ..Default::default()
187 }
188 }
189
190 pub fn post_id(mut self, v: impl Into<String>) -> Self {
192 self.post_id = Some(v.into());
193 self
194 }
195
196 fn to_query(&self) -> Vec<(String, String)> {
197 let mut q: Vec<(String, String)> = vec![
198 ("urlSlug".into(), self.url_slug.clone()),
199 ("locationId".into(), self.location_id.clone()),
200 ];
201 if let Some(v) = &self.post_id {
202 q.push(("postId".into(), v.to_string()));
203 }
204 q
205 }
206}
207
208#[derive(Debug, Clone, Default)]
210pub struct GetBlogsByLocationIdParams {
211 pub location_id: String,
214 pub skip: f64,
217 pub limit: f64,
220 pub search_term: Option<String>,
222}
223
224impl GetBlogsByLocationIdParams {
225 pub fn new(location_id: impl Into<String>, skip: f64, limit: f64) -> Self {
227 Self {
228 location_id: location_id.into(),
229 skip,
230 limit,
231 ..Default::default()
232 }
233 }
234
235 pub fn search_term(mut self, v: impl Into<String>) -> Self {
237 self.search_term = Some(v.into());
238 self
239 }
240
241 fn to_query(&self) -> Vec<(String, String)> {
242 let mut q: Vec<(String, String)> = vec![
243 ("locationId".into(), self.location_id.clone()),
244 ("skip".into(), self.skip.to_string()),
245 ("limit".into(), self.limit.to_string()),
246 ];
247 if let Some(v) = &self.search_term {
248 q.push(("searchTerm".into(), v.to_string()));
249 }
250 q
251 }
252}
253
254impl BlogsService {
255 pub async fn get_all_authors(
264 &self,
265 params: &GetAllAuthorsParams,
266 ) -> Result<models::AuthorsResponseDTO> {
267 let query = params.to_query();
268 self.client
269 .send_versioned(
270 reqwest::Method::GET,
271 "/blogs/authors",
272 &query,
273 None::<&()>,
274 Some("2021-07-28"),
275 )
276 .await
277 }
278
279 pub async fn get_all_categories(
288 &self,
289 params: &GetAllCategoriesParams,
290 ) -> Result<models::CategoriesResponseDTO> {
291 let query = params.to_query();
292 self.client
293 .send_versioned(
294 reqwest::Method::GET,
295 "/blogs/categories",
296 &query,
297 None::<&()>,
298 Some("2021-07-28"),
299 )
300 .await
301 }
302
303 pub async fn create_blog_post(
312 &self,
313 body: &models::CreateBlogPostParams,
314 ) -> Result<models::BlogPostCreateResponseWrapperDTO> {
315 let query = Vec::new();
316 self.client
317 .send_versioned(
318 reqwest::Method::POST,
319 "/blogs/posts",
320 &query,
321 Some(body),
322 Some("2021-07-28"),
323 )
324 .await
325 }
326
327 pub async fn get_blog_posts_by_blog_id(
336 &self,
337 params: &GetBlogPostsByBlogIdParams,
338 ) -> Result<models::BlogPostGetResponseWrapperDTO> {
339 let query = params.to_query();
340 self.client
341 .send_versioned(
342 reqwest::Method::GET,
343 "/blogs/posts/all",
344 &query,
345 None::<&()>,
346 Some("2021-07-28"),
347 )
348 .await
349 }
350
351 pub async fn check_url_slug(
361 &self,
362 params: &CheckUrlSlugParams,
363 ) -> Result<models::UrlSlugCheckResponseDTO> {
364 let query = params.to_query();
365 self.client
366 .send_versioned(
367 reqwest::Method::GET,
368 "/blogs/posts/url-slug-exists",
369 &query,
370 None::<&()>,
371 Some("2021-07-28"),
372 )
373 .await
374 }
375
376 pub async fn update_blog_post(
385 &self,
386 post_id: &str,
387 body: &models::UpdateBlogPostParams,
388 ) -> Result<models::BlogPostUpdateResponseWrapperDTO> {
389 let path = format!("/blogs/posts/{}", crate::services::encode(post_id));
390 let query = Vec::new();
391 self.client
392 .send_versioned(
393 reqwest::Method::PUT,
394 &path,
395 &query,
396 Some(body),
397 Some("2021-07-28"),
398 )
399 .await
400 }
401
402 pub async fn get_blogs_by_location_id(
411 &self,
412 params: &GetBlogsByLocationIdParams,
413 ) -> Result<models::BlogGetResponseWrapperDTO> {
414 let query = params.to_query();
415 self.client
416 .send_versioned(
417 reqwest::Method::GET,
418 "/blogs/site/all",
419 &query,
420 None::<&()>,
421 Some("2021-07-28"),
422 )
423 .await
424 }
425}