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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use crate::api_errors::{APIErrorNoContent, APIErrorNotModified};
use crate::api_objects::{
    ApiCountPage, ApiItem, ApiMinimalPagination, ApiObject, ApiPage, ApiPagination,
};
use crate::DfStDatabase;
use crate::ServerInfo;
use df_st_core::item_count::ItemCount;
use df_st_db::{id_filter, string_filter, DBObject};
use rocket::http::ContentType;
use rocket::{get, State};
use rocket::{response, Response};
use rocket_contrib::json::Json;
use rocket_okapi_codegen::openapi;

impl ApiObject for df_st_core::MapImage {
    fn get_type() -> String {
        "map_image".to_owned()
    }
    fn get_item_link(&self, base_url: &str) -> String {
        format!("{}/map_images/{}", base_url, self.id)
    }
    fn get_page_link(base_url: &str) -> String {
        format!("{}/map_images", base_url)
    }
    fn get_count_link(base_url: &str) -> String {
        format!("{}/map_images/count", base_url)
    }
}

/// Request a `MapImage` by id.
///
/// ( Since = "0.1.0" )
#[openapi]
#[get("/map_images/<map_image_id>")]
pub fn get_map_image(
    conn: DfStDatabase,
    map_image_id: i32,
    server_info: State<ServerInfo>,
) -> Result<Json<ApiItem<df_st_core::MapImage>>, APIErrorNoContent> {
    let mut api_page = ApiItem::<df_st_core::MapImage>::new(&server_info);
    let item_search = df_st_db::MapImage::get_from_db(
        &*conn,
        id_filter!["id" => map_image_id, "world_id" => server_info.world_id],
        true,
    )?;
    if let Some(item) = item_search {
        api_page.wrap(item);
        return Ok(Json(api_page));
    }
    Err(APIErrorNoContent::new())
}

/// Request a `MapImage` by id.
///
/// ( Since = "0.1.0" )
#[openapi]
#[get("/map_images/<map_image_id>/image.png")]
pub fn get_map_image_png<'r>(
    conn: DfStDatabase,
    map_image_id: i32,
    server_info: State<ServerInfo>,
) -> Result<Response<'r>, APIErrorNoContent> {
    let item_search = df_st_db::MapImage::get_from_db(
        &*conn,
        id_filter!["id" => map_image_id, "world_id" => server_info.world_id],
        true,
    )?;
    if let Some(item) = item_search {
        let data = item.data;
        return Ok(response::Response::build()
            .header(ContentType::PNG)
            .sized_body(std::io::Cursor::new(data))
            .finalize());
    }
    Err(APIErrorNoContent::new())
}

/// Request a list of all `MapImage` in the world.
/// List is not ordered and some id's might be missing!
///
/// ( Since = "0.1.0" )
#[openapi]
#[get("/map_images?<pagination..>")]
pub fn list_map_images(
    conn: DfStDatabase,
    pagination: ApiPagination,
    server_info: State<ServerInfo>,
) -> Result<Json<ApiPage<df_st_core::MapImage>>, APIErrorNotModified> {
    let mut api_page = ApiPage::<df_st_core::MapImage>::new(&pagination, &server_info);
    api_page.order_by = df_st_db::MapImage::match_field_by_opt(api_page.order_by);
    api_page.total_item_count = df_st_db::MapImage::get_count_from_db(
        &*conn,
        id_filter!["world_id" => server_info.world_id],
        string_filter![],
        0,
        1,
        None,
        None,
    )?
    .get(0)
    .unwrap()
    .count;

    let result_list = df_st_db::MapImage::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(),
        None,
        true,
    )?;

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

/// Request a counts about `MapImage` grouped by a field.
///
/// ( Since = "0.1.0" )
#[openapi]
#[get("/map_images/count?<group_by>&<pagination..>")]
pub fn get_map_image_count(
    conn: DfStDatabase,
    group_by: Option<String>,
    pagination: ApiMinimalPagination,
    server_info: State<ServerInfo>,
) -> Result<Json<ApiCountPage<ItemCount, df_st_core::MapImage>>, APIErrorNotModified> {
    let mut api_page =
        ApiCountPage::<ItemCount, df_st_core::MapImage>::new(&pagination, &server_info);
    api_page.group_by = df_st_db::MapImage::match_field_by_opt(group_by);
    api_page.total_item_count = df_st_db::MapImage::get_count_from_db(
        &*conn,
        id_filter!["world_id" => server_info.world_id],
        string_filter![],
        0,
        u32::MAX,
        api_page.group_by.clone(),
        None,
    )?
    .len() as u32;

    let result_list = df_st_db::MapImage::get_count_from_db(
        &*conn,
        id_filter!["world_id" => server_info.world_id],
        string_filter![],
        api_page.page_start,
        api_page.max_page_size,
        api_page.group_by.clone(),
        None,
    )?;

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