use super::empty::EmptyType;
use super::link::LinkType;
use super::list_interests::{
CollectionListInterest, InterestParam, ListInterest, ListInterestBuilder,
};
use crate::api::MailchimpApi;
use crate::internal::error_type::MailchimpErrorType;
use crate::internal::request::MailchimpResult;
use crate::iter::{BuildIter, MailchimpCollection, MalchimpIter, ResourceFilter, SimpleFilter};
use log::error;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ListInterestCategory {
#[serde(default)]
pub list_id: String,
#[serde(default)]
pub id: String,
#[serde(default)]
pub title: String,
#[serde(default)]
pub display_order: u64,
#[serde(default, rename = "type")]
pub ic_type: String,
#[serde(default)]
pub _links: Vec<LinkType>,
#[serde(skip)]
_api: Rc<MailchimpApi>,
#[serde(default)]
_endpoint: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct InterestCategoryParam {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub display_order: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none", rename = "type")]
pub ic_type: Option<String>,
}
impl InterestCategoryParam {
pub fn new<'a>(title: &'a str, ic_type: &'a str, display_order: Option<u64>) -> Self {
InterestCategoryParam {
title: Some(title.to_string()),
display_order: display_order,
ic_type: Some(ic_type.to_string()),
}
}
}
impl ListInterestCategory {
pub fn set_api(&mut self, n_api: Rc<MailchimpApi>) {
self._api = n_api
}
pub fn set_endpoint<'a>(&mut self, n_endpoint: &'a str) {
self._endpoint = n_endpoint.to_string();
}
pub fn delete(&self) -> Option<MailchimpErrorType> {
let endpoint = self.get_base_endpoint();
match self._api.delete::<EmptyType>(&endpoint, HashMap::new()) {
Ok(_) => None,
Err(e) => Some(e),
}
}
pub fn update<'a>(
&self,
param: InterestCategoryParam,
) -> MailchimpResult<ListInterestCategory> {
let endpoint = self.get_base_endpoint();
self._api
.patch::<ListInterestCategory, InterestCategoryParam>(&endpoint, param)
}
pub fn get_interests(
&self,
filters: Option<SimpleFilter>,
) -> MalchimpIter<ListInterestBuilder> {
let mut endpoint = self.get_base_endpoint();
endpoint.push_str("/interests");
let filter_params = if let Some(f) = filters {
f
} else {
SimpleFilter::default()
};
match self
._api
.get::<CollectionListInterest>(&endpoint, filter_params.build_payload())
{
Ok(collection) => MalchimpIter {
builder: ListInterestBuilder {
endpoint: endpoint.clone(),
},
data: collection.interests,
cur_filters: filter_params.clone(),
cur_it: 0,
total_items: collection.total_items,
api: self._api.clone(),
endpoint: endpoint.clone(),
},
Err(e) => {
error!( target: "mailchimp", "Get List Members: Response Error details: {:?}", e);
MalchimpIter {
builder: ListInterestBuilder {
endpoint: endpoint.clone(),
},
data: Vec::new(),
cur_filters: filter_params.clone(),
cur_it: 0,
total_items: 0,
api: self._api.clone(),
endpoint: endpoint.clone(),
}
}
}
}
pub fn create_interest<'a>(&self, param: InterestParam) -> MailchimpResult<ListInterest> {
let mut endpoint = self.get_base_endpoint();
endpoint.push_str("/interest-categories");
self._api
.post::<ListInterest, InterestParam>(&endpoint, param)
}
pub fn get_specific_interest<'a>(
&self,
interest_id: &'a str,
) -> MailchimpResult<ListInterestCategory> {
let mut endpoint = self.get_base_endpoint();
endpoint.push_str("/interests/");
endpoint.push_str(interest_id);
self._api
.get::<ListInterestCategory>(&endpoint, HashMap::new())
}
fn get_base_endpoint(&self) -> String {
let mut endpoint = self._endpoint.clone();
endpoint.push_str("/");
endpoint.push_str(self.id.as_str());
endpoint
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CollectionListInterestCategories {
#[serde(default)]
pub categories: Vec<ListInterestCategory>,
#[serde(default)]
pub list_id: String,
#[serde(default)]
pub total_items: u64,
#[serde(default)]
pub _links: Vec<LinkType>,
}
impl MailchimpCollection<ListInterestCategory> for CollectionListInterestCategories {
fn get_total_items(&self) -> u64 {
self.total_items
}
fn get_values(&self) -> Vec<ListInterestCategory> {
self.categories.clone()
}
}
impl Default for CollectionListInterestCategories {
fn default() -> Self {
CollectionListInterestCategories {
list_id: "".to_string(),
categories: Vec::new(),
total_items: 0,
_links: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct ListInterestCategoryFilter {
pub fields: Option<String>,
pub exclude_fields: Option<String>,
pub count: Option<u64>,
pub offset: Option<u64>,
pub ic_type: Option<String>,
}
impl Default for ListInterestCategoryFilter {
fn default() -> Self {
ListInterestCategoryFilter {
fields: None,
exclude_fields: None,
count: Some(50),
offset: Some(0),
ic_type: None,
}
}
}
impl ResourceFilter for ListInterestCategoryFilter {
fn build_payload(&self) -> HashMap<String, String> {
let mut payload = HashMap::new();
if self.fields.is_some() {
payload.insert("fields".to_string(), self.fields.as_ref().unwrap().clone());
}
if self.exclude_fields.is_some() {
payload.insert(
"exclude_fields".to_string(),
self.exclude_fields.as_ref().unwrap().clone(),
);
}
if self.count.is_some() {
payload.insert(
"count".to_string(),
format!("{:}", self.count.as_ref().unwrap().clone()),
);
}
if self.offset.is_some() {
payload.insert(
"offset".to_string(),
format!("{:}", self.offset.as_ref().unwrap().clone()),
);
}
if self.ic_type.is_some() {
payload.insert(
"type".to_string(),
format!("{:}", self.ic_type.as_ref().unwrap().clone()),
);
}
payload
}
}
#[derive(Debug)]
pub struct ListInterestCategoryBuilder {
pub endpoint: String,
}
impl BuildIter for ListInterestCategoryBuilder {
type Item = ListInterestCategory;
type FilterItem = ListInterestCategoryFilter;
type Collection = CollectionListInterestCategories;
fn update_item(&self, data: &Self::Item, api: Rc<MailchimpApi>) -> Self::Item {
let mut in_data = data.clone();
in_data.set_api(api);
in_data.set_endpoint(&self.endpoint);
in_data
}
fn update_filter_offset(&self, filter: &Self::FilterItem) -> Self::FilterItem {
let mut f = filter.clone();
f.offset = Some(f.count.unwrap() + f.offset.unwrap());
f
}
}