1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::{
    client::{ClientInner, ClientResponse},
    paginator::Paginator,
    user::UserSummary,
};

#[derive(Serialize, Deserialize, Debug)]
pub struct Donation {
    pub id: u64,
    pub amount: f64,
    pub name: String,
    pub comment: Option<String>,

    #[serde(rename = "completedAt")]
    pub completed_at: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Avatar {
    pub alt: String,
    pub src: String,
    pub height: u64,
    pub width: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Campaign {
    pub id: u64,
    pub name: String,
    pub slug: String,
    pub status: String,
    #[serde(rename = "amountRaised")]
    pub amount_raised: f64,
    #[serde(rename = "totalAmountRaised")]
    pub total_amount_raised: f64,

    pub supportable: bool,
    #[serde(rename = "supportingAmountRaised")]
    pub supporting_amount_raised: f64,

    pub avatar: Avatar,
    pub user: UserSummary,

    #[serde(rename = "causeCurrency")]
    pub cause_currency: String,

    pub description: String,

    #[serde(rename = "startsAt")]
    pub starts_at: u64,

    #[serde(rename = "endsAt")]
    pub ends_at: Option<u64>,

    #[serde(rename = "fundraiserGoalAmount")]
    pub fundraiser_goal_amount: f64,

    pub r#type: String,
}

pub struct CampaignBuilder {
    campaign_id: String,
    client: Arc<ClientInner>,
}

impl CampaignBuilder {
    pub(crate) fn new(client: Arc<ClientInner>, campaign_id: String) -> Self {
        Self {
            client,
            campaign_id,
        }
    }

    pub async fn get(&self) -> crate::Result<Campaign> {
        let url = format!("/api/v3/campaigns/{}", &self.campaign_id);
        let response = self
            .client
            .get(&url, None)
            .await?
            .json::<ClientResponse<Campaign>>()
            .await?;
        Ok(response.data)
    }

    pub async fn donations(&self) -> crate::Result<Paginator<Donation>> {
        let endpoint = format!("/api/v3/campaigns/{}/donations", &self.campaign_id);
        Ok(Paginator::get(self.client.clone(), &endpoint).await?)
    }
}