pib-client-cached 0.28.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, Search,
    body::by_id_or_alias::paper::{Paper, PaperParentId, UpdatePaper},
};
use pib_service_core_types::{DataItem, DataPage, PaperId};

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

#[async_trait(?Send)]
impl GetableItem<PaperParentId, PaperId, Paper> for Paper {
    async fn get<C: PibClient>(
        client: &C,
        PaperParentId { body_id }: PaperParentId,
        paper_id: PaperId,
    ) -> Result<DataItem<PaperParentId, PaperId, Paper>, C::ClientError> {
        Ok(client
            .body_by_id_or_alias_paper_by_id_get(body_id, paper_id)
            .await?)
    }
}

#[async_trait(?Send)]
impl PageableItem<PaperParentId, PaperId, Paper> for Paper {
    async fn get_page<C: PibClient>(
        client: &C,
        PaperParentId { body_id }: PaperParentId,
        pagination: Pagination,
        search: Search,
    ) -> Result<DataPage<PaperParentId, PaperId, Paper>, C::ClientError> {
        Ok(client
            .body_by_id_or_alias_paper_get(body_id, pagination, search)
            .await?)
    }
}

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

#[async_trait(?Send)]
impl UpdatableItem<PaperParentId, PaperId, Paper, UpdatePaper> for Paper {
    async fn update<C: PibClient>(
        client: &C,
        PaperParentId { body_id }: PaperParentId,
        paper_id: PaperId,
        update_item: UpdatePaper,
    ) -> Result<DataItem<PaperParentId, PaperId, Paper>, C::ClientError> {
        let updated_item = client
            .body_by_id_or_alias_paper_by_id_patch(body_id, paper_id, update_item)
            .await?;
        info!("Updated paper: {updated_item:?}");
        Ok(updated_item)
    }
}