pib-client-cached 0.22.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::{
    Pagination,
    body::by_id_or_alias::person::{Person, PersonParentId, UpdatePerson},
};
use pib_service_core_types::{DataItem, DataPage, PersonId};

use crate::item::{CreateableItem, GetableItem, PageableItem, UpdatableItem};

#[async_trait(?Send)]
impl GetableItem<PersonParentId, PersonId, Person> for Person {
    async fn get<C: PibClient>(
        client: &C,
        PersonParentId { body_id }: PersonParentId,
        person_id: PersonId,
    ) -> Result<DataItem<PersonParentId, PersonId, Person>, C::ClientError> {
        Ok(client
            .body_by_id_or_alias_person_by_id_get(body_id, person_id)
            .await?)
    }
}

#[async_trait(?Send)]
impl PageableItem<PersonParentId, PersonId, Person> for Person {
    async fn get_page<C: PibClient>(
        client: &C,
        PersonParentId { body_id }: PersonParentId,
        pagination: Pagination,
    ) -> Result<DataPage<PersonParentId, PersonId, Person>, C::ClientError> {
        Ok(client
            .body_by_id_or_alias_person_get(body_id, pagination)
            .await?)
    }
}

#[async_trait(?Send)]
impl CreateableItem<PersonParentId, PersonId, Person> for Person {
    async fn create<C: PibClient>(
        client: &C,
        PersonParentId { body_id }: PersonParentId,
        person: Person,
    ) -> Result<DataItem<PersonParentId, PersonId, Person>, C::ClientError> {
        let created_item = client
            .body_by_id_or_alias_person_post(body_id, person)
            .await?;
        info!("Created a new person: {created_item:?}");
        Ok(created_item)
    }
}

#[async_trait(?Send)]
impl UpdatableItem<PersonParentId, PersonId, Person, UpdatePerson> for Person {
    async fn update<C: PibClient>(
        client: &C,
        PersonParentId { body_id }: PersonParentId,
        person_id: PersonId,
        update_item: UpdatePerson,
    ) -> Result<DataItem<PersonParentId, PersonId, Person>, C::ClientError> {
        let updated_item = client
            .body_by_id_or_alias_person_by_id_patch(body_id, person_id, update_item)
            .await?;
        info!("Updated person: {updated_item:?}");
        Ok(updated_item)
    }
}