Skip to content

Commit

Permalink
Add infra::firestore_store mod
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 10, 2023
1 parent e48c61d commit cf04dc9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions rust/crates/web/src/infra.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod firestore;
pub mod firestore_store;
pub mod store;
2 changes: 1 addition & 1 deletion rust/crates/web/src/infra/firestore/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub enum Error {
ValueType,
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Client {
client: FirestoreClient<GoogleAuthz<Channel>>,
root_path: RootPath,
Expand Down
54 changes: 54 additions & 0 deletions rust/crates/web/src/infra/firestore_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::sync::Arc;

use axum::async_trait;

use crate::{
model::{self, CheckList},
use_case::{self, StoreTrait},
};

use super::firestore::client::Client;

#[derive(serde::Deserialize, serde::Serialize)]
pub struct CheckListDocumentData {
pub date: String,
pub id: String,
}

impl From<CheckListDocumentData> for model::CheckList {
fn from(CheckListDocumentData { date, id }: CheckListDocumentData) -> Self {
Self { date, id }
}
}

#[derive(Clone, Debug)]
pub struct FirestoreStore {
client: Arc<tokio::sync::Mutex<Client>>,
}

#[async_trait]
impl StoreTrait for FirestoreStore {
async fn find_all_check_lists(&self) -> Result<Vec<model::CheckList>, use_case::Error> {
let mut client = self.client.lock().await;
let collection_path = client.collection("check_lists").unwrap();
// TODO: unwrap
// TODO: pagination
Ok(client
.list::<CheckListDocumentData>(&collection_path)
.await
.unwrap()
.0
.into_iter()
.map(|doc| doc.data())
.map(CheckList::from)
.collect())
}

async fn find_all_checks(&self) -> Result<Vec<model::Check>, use_case::Error> {
todo!()
}

async fn find_all_items(&self) -> Result<Vec<model::Item>, use_case::Error> {
todo!()
}
}

0 comments on commit cf04dc9

Please sign in to comment.