pib-client-cached 0.34.0

API client for pib-service - caching data storage
Documentation
// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

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, NewLocation, 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, ParentId, NewLocation> for Location {
    async fn create<C: PibClient>(
        client: &C,
        ParentId { body_id }: ParentId,
        location: NewLocation,
    ) -> 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)
    }
}