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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use crate::api_errors::APIErrorNotModified;
use crate::api_objects::{ApiObject, ApiPage, ApiPagination};
use crate::DfStDatabase;
use crate::ServerInfo;
use df_st_db::{id_filter, string_filter, DBObject};
use rocket::{get, State};
use rocket_contrib::json::Json;
use rocket_okapi_codegen::openapi;

impl ApiObject for df_st_core::LinkHEHF {
    fn get_type() -> String {
        "link_he_hf".to_owned()
    }
    fn get_item_link(&self, base_url: &str) -> String {
        format!(
            "{}/link_he_hf?he_id={}&hf_id={}",
            base_url, self.he_id, self.hf_id
        )
    }
    fn get_page_link(base_url: &str) -> String {
        format!("{}/link_he_hf", base_url)
    }
    fn get_count_link(base_url: &str) -> String {
        format!("{}/link_he_hf/count", base_url)
    }
}

/// Request a list of all `LinkHEHF` in the world.
/// You can filter on `he_id` or `hf_id`, but not at the same time.
///
/// ( Since = "0.1.2" )
#[openapi]
#[get("/link_he_hf?<he_id>&<hf_id>&<pagination..>")]
pub fn list_link_he_hf(
    conn: DfStDatabase,
    pagination: ApiPagination,
    server_info: State<ServerInfo>,
    he_id: Option<i32>,
    hf_id: Option<i32>,
) -> Result<Json<ApiPage<df_st_core::LinkHEHF>>, APIErrorNotModified> {
    let mut api_page = ApiPage::<df_st_core::LinkHEHF>::new(&pagination, &server_info);
    api_page.order_by = df_st_db::LinkHEHF::match_field_by_opt(api_page.order_by);
    // Add optional items to id_filter.
    let mut id_filter = id_filter!["world_id" => server_info.world_id];
    if let Some(he_id) = he_id {
        id_filter.insert("he_id".to_owned(), he_id);
    }
    if let Some(hf_id) = hf_id {
        id_filter.insert("hf_id".to_owned(), hf_id);
    }
    api_page.total_item_count = df_st_db::LinkHEHF::get_count_from_db(
        &*conn,
        id_filter.clone(),
        string_filter![],
        0,
        1,
        None,
        None,
    )
    .unwrap() // TODO These errors should be better handled
    .get(0)
    .unwrap()
    .count;

    let result_list = df_st_db::LinkHEHF::get_list_from_db(
        &*conn,
        id_filter,
        string_filter![],
        api_page.page_start,
        api_page.max_page_size,
        api_page.get_db_order(),
        api_page.order_by.clone(),
        None,
        true,
    )
    .unwrap(); // TODO These errors should be better handled

    if api_page.wrap(result_list) {
        return Err(APIErrorNotModified::new());
    }
    Ok(Json(api_page))
}

/// Request a list of all `HistoricalEvent` here a `HistoricalFigure` was mentioned.
///
/// ( Since = "0.1.2" )
#[openapi]
#[get("/link_he_hf/<hf_id>?<pagination..>")]
pub fn list_he_from_hf(
    conn: DfStDatabase,
    pagination: ApiPagination,
    server_info: State<ServerInfo>,
    hf_id: i32,
) -> Result<Json<ApiPage<df_st_core::HistoricalEvent>>, APIErrorNotModified> {
    let mut api_page = ApiPage::<df_st_core::HistoricalEvent>::new(&pagination, &server_info);
    api_page.order_by = df_st_db::HistoricalEvent::match_field_by_opt(api_page.order_by);

    // Request all Links
    let result_links = df_st_db::LinkHEHF::get_list_from_db(
        &*conn,
        id_filter!["world_id" => server_info.world_id, "hf_id" => hf_id],
        string_filter![],
        api_page.page_start,
        api_page.max_page_size,
        api_page.get_db_order(),
        api_page.order_by.clone(),
        None,
        true,
    )
    .unwrap();
    // Put all the id in a list
    let id_list: Vec<i32> = result_links.iter().map(|item| item.he_id).collect();

    // Request the correct HistoricalEvents.
    api_page.total_item_count = df_st_db::HistoricalEvent::get_count_from_db(
        &*conn,
        id_filter!["world_id" => server_info.world_id],
        string_filter![],
        0,
        1,
        None,
        Some(id_list.clone()),
    )
    .unwrap() // TODO These errors should be better handled
    .get(0)
    .unwrap()
    .count;

    let result_list = df_st_db::HistoricalEvent::get_list_from_db(
        &*conn,
        id_filter!["world_id" => server_info.world_id],
        string_filter![],
        api_page.page_start,
        api_page.max_page_size,
        api_page.get_db_order(),
        api_page.order_by.clone(),
        Some(id_list),
        true,
    )
    .unwrap(); // TODO These errors should be better handled

    if api_page.wrap(result_list) {
        return Err(APIErrorNotModified::new());
    }
    Ok(Json(api_page))
}