use crate::error::PitchforkError;
use crate::pitchfork::{DomoRequest, PagesRequestBuilder};
use log::debug;
use reqwest::Method;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PageInfo {
pub name: String,
pub id: String,
#[serde(rename = "parentId")]
pub parent_id: u64,
#[serde(rename = "ownerId")]
pub owner_id: u64,
pub locked: bool,
#[serde(rename = "collectionIds")]
pub collection_ids: Vec<u64>,
#[serde(rename = "cardIds")]
pub card_ids: Vec<u64>,
pub children: Vec<Page>,
pub visibility: PageVisibility,
#[serde(rename = "userIds")]
pub user_ids: Vec<u64>,
#[serde(rename = "pageIds")]
pub page_ids: Vec<u64>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Page {
pub id: u64,
pub name: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PageVisibility {
#[serde(rename = "userIds")]
pub user_ids: Vec<u64>,
#[serde(rename = "pageIds")]
pub page_ids: Vec<u64>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PageCollection {
pub id: u64,
pub title: String,
pub description: String,
#[serde(rename = "cardIds")]
pub card_ids: Vec<u64>,
}
impl<'t> PagesRequestBuilder<'t, PageInfo> {
pub fn info(mut self, page_id: u64) -> Result<PageInfo, PitchforkError> {
self.url.push_str(&page_id.to_string());
let req = Self {
method: Method::GET,
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: None,
};
req.retrieve_and_deserialize_json()
}
pub fn list(mut self, limit: u32, offset: u32) -> Result<Vec<PageInfo>, PitchforkError> {
self.url
.push_str(&format!("?limit={}&offset={}", limit, offset));
let req = Self {
method: Method::GET,
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: None,
};
let ds_list = serde_json::from_reader(req.send_json()?)?;
Ok(ds_list)
}
pub fn create(self, page: &PageInfo) -> Result<PageInfo, PitchforkError> {
let body = serde_json::to_string(page)?;
debug!("body: {}", body);
let req = Self {
method: Method::POST,
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: Some(body),
};
req.retrieve_and_deserialize_json()
}
pub fn delete(mut self, page_id: u64) -> Result<(), PitchforkError> {
self.url.push_str(&page_id.to_string());
let req = Self {
method: Method::DELETE,
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: None,
};
req.send_json()?;
Ok(())
}
pub fn modify(mut self, page_id: u64, page: &PageInfo) -> Result<PageInfo, PitchforkError> {
self.url.push_str(&page_id.to_string());
let body = serde_json::to_string(page)?;
debug!("body: {}", body);
let req = Self {
method: Method::PUT,
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: Some(body),
};
let ds = serde_json::from_reader(req.send_json()?)?;
Ok(ds)
}
pub fn collections(mut self, page_id: u64) -> Result<Vec<PageCollection>, PitchforkError> {
self.url.push_str(&format!("{}/collections", page_id));
let req = Self {
method: Method::GET,
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: None,
};
let ds = serde_json::from_reader(req.send_json()?)?;
Ok(ds)
}
pub fn create_collection(
mut self,
page_id: u64,
collection: &PageCollection,
) -> Result<(), PitchforkError> {
self.url.push_str(&format!("{}/collections", page_id));
let body = serde_json::to_string(collection)?;
debug!("body: {}", body);
let req = Self {
method: Method::POST,
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: Some(body),
};
let ds = serde_json::from_reader(req.send_json()?)?;
Ok(ds)
}
pub fn modify_collection(
mut self,
page_id: u64,
collection_id: u64,
collection: &PageCollection,
) -> Result<(), PitchforkError> {
self.url
.push_str(&format!("{}/collections/{}", page_id, collection_id));
let body = serde_json::to_string(collection)?;
debug!("body: {}", body);
let req = Self {
method: Method::PUT,
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: Some(body),
};
req.send_json()?;
Ok(())
}
pub fn delete_collection(
mut self,
page_id: u64,
collection_id: u64,
) -> Result<(), PitchforkError> {
self.url
.push_str(&format!("{}/collections/{}", page_id, collection_id));
let req = Self {
method: Method::DELETE,
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: None,
};
req.send_json()?;
Ok(())
}
}