use async_trait::async_trait;
use log::info;
use pib_client::PibClient;
use pib_service_api_types::{
Associated, ItemResponse, PagedResponse, Pagination, Search,
body::by_id_or_alias::location::{Location, ParentId, UpdateLocation},
};
use pib_service_core_types::LocationId;
use crate::item::{CreateableItem, GetableItem, PageableItem, UpdatableItem};
#[async_trait(?Send)]
impl GetableItem<ParentId, LocationId, Location> for Location {
async fn get<C: PibClient>(
client: &C,
ParentId { body_id }: ParentId,
location_id: LocationId,
associated: Associated,
) -> Result<ItemResponse<ParentId, LocationId, Location>, C::ClientError> {
Ok(client
.body_by_id_or_alias_location_by_id_get(body_id, location_id, associated)
.await?)
}
}
#[async_trait(?Send)]
impl PageableItem<ParentId, LocationId, Location> for Location {
async fn get_page<C: PibClient>(
client: &C,
ParentId { body_id }: ParentId,
pagination: Pagination,
search: Search,
associated: Associated,
) -> Result<PagedResponse<ParentId, LocationId, Location>, C::ClientError> {
Ok(client
.body_by_id_or_alias_location_get(body_id, pagination, search, associated)
.await?)
}
}
#[async_trait(?Send)]
impl CreateableItem<ParentId, LocationId, Location> for Location {
async fn create<C: PibClient>(
client: &C,
ParentId { body_id }: ParentId,
location: Location,
) -> Result<ItemResponse<ParentId, LocationId, Location>, C::ClientError> {
let created_item = client
.body_by_id_or_alias_location_post(body_id, location)
.await?;
info!("Created a new location: {created_item:?}");
Ok(created_item)
}
}
#[async_trait(?Send)]
impl UpdatableItem<ParentId, LocationId, Location, UpdateLocation> for Location {
async fn update<C: PibClient>(
client: &C,
ParentId { body_id }: ParentId,
location_id: LocationId,
update_item: UpdateLocation,
) -> Result<ItemResponse<ParentId, LocationId, Location>, C::ClientError> {
let updated_item = client
.body_by_id_or_alias_location_by_id_patch(body_id, location_id, update_item)
.await?;
info!("Updated location: {updated_item:?}");
Ok(updated_item)
}
}