use crate::api_errors::{APIErrorNoContent, APIErrorNotModified};
use crate::api_objects::ApiObject;
use crate::pagination::{
ApiCountPage, ApiCountPagination, ApiItem, ApiItemQuery, ApiPage, ApiPagination,
};
use crate::DfStDatabase;
use crate::ServerInfo;
use df_st_core::item_count::ItemCount;
use df_st_db::{id_filter, DBObject};
use rocket::{get, State};
use rocket_contrib::json::Json;
use df_rocket_okapi_codegen::openapi;
impl ApiObject for df_st_core::HistoricalEvent {
fn get_type() -> String {
"historical_event".to_owned()
}
fn get_item_link(&self, base_url: &str) -> String {
format!("{}/historical_events/{}", base_url, self.id)
}
fn get_page_link(base_url: &str) -> String {
format!("{}/historical_events", base_url)
}
fn get_count_link(base_url: &str) -> String {
format!("{}/historical_events/count", base_url)
}
}
#[openapi]
#[get("/historical_events/<historical_event_id>?<item_query..>")]
pub fn get_historical_event(
conn: DfStDatabase,
historical_event_id: i32,
item_query: ApiItemQuery,
server_info: State<ServerInfo>,
) -> Result<Json<ApiItem<df_st_core::HistoricalEvent>>, APIErrorNoContent> {
let mut api_page = ApiItem::<df_st_core::HistoricalEvent>::new(&server_info, &item_query);
let item_search = df_st_db::HistoricalEvent::get_from_db(
&*conn,
id_filter!["id" => historical_event_id, "world_id" => server_info.world_id],
api_page.get_nested_items(),
)?;
if let Some(item) = item_search {
api_page.wrap(item);
return Ok(Json(api_page));
}
Err(APIErrorNoContent::new())
}
#[openapi]
#[get("/historical_events?<pagination..>")]
pub fn list_historical_events(
conn: DfStDatabase,
pagination: ApiPagination,
server_info: State<ServerInfo>,
) -> Result<Json<ApiPage<df_st_core::HistoricalEvent>>, APIErrorNotModified> {
let mut api_page = ApiPage::<df_st_core::HistoricalEvent>::new(&pagination, &server_info);
api_page.match_fields::<df_st_db::HistoricalEvent, _, _>()?;
api_page.total_item_count = df_st_db::HistoricalEvent::get_count_from_db(
&*conn,
api_page.add_int_filter(id_filter!["world_id" => server_info.world_id]),
api_page.get_string_filter(),
0,
1,
None,
None,
)?
.get(0)
.unwrap()
.count;
let result_list = df_st_db::HistoricalEvent::get_list_from_db(
&*conn,
api_page.add_int_filter(id_filter!["world_id" => server_info.world_id]),
api_page.get_string_filter(),
api_page.page_start,
api_page.max_page_size,
api_page.get_db_order(),
api_page.order_by.clone(),
None,
api_page.get_nested_items(),
)?;
if api_page.wrap(result_list) {
return Err(APIErrorNotModified::new());
}
Ok(Json(api_page))
}
#[openapi]
#[get("/historical_events/count?<group_by>&<pagination..>")]
pub fn get_historical_event_count(
conn: DfStDatabase,
group_by: Option<String>,
pagination: ApiCountPagination,
server_info: State<ServerInfo>,
) -> Result<Json<ApiCountPage<ItemCount, df_st_core::HistoricalEvent>>, APIErrorNotModified> {
let mut api_page =
ApiCountPage::<ItemCount, df_st_core::HistoricalEvent>::new(&pagination, &server_info);
api_page.group_by = group_by;
api_page.match_fields::<df_st_db::HistoricalEvent, _, _>()?;
api_page.total_item_count = df_st_db::HistoricalEvent::get_count_from_db(
&*conn,
api_page.add_int_filter(id_filter!["world_id" => server_info.world_id]),
api_page.get_string_filter(),
0,
u32::MAX,
api_page.group_by.clone(),
None,
)?
.len() as u32;
let result_list = df_st_db::HistoricalEvent::get_count_from_db(
&*conn,
api_page.add_int_filter(id_filter!["world_id" => server_info.world_id]),
api_page.get_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))
}