Jelajahi Sumber

Add function to retrieve accounts by user.

Lee Morgan 1 tahun lalu
induk
melakukan
1b2a192fb0
3 mengubah file dengan 29 tambahan dan 0 penghapusan
  1. 19 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 9 0
      src/models/account.rs

+ 19 - 0
Cargo.lock

@@ -684,6 +684,21 @@ version = "2.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
 
+[[package]]
+name = "futures"
+version = "0.3.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
+dependencies = [
+ "futures-channel",
+ "futures-core",
+ "futures-executor",
+ "futures-io",
+ "futures-sink",
+ "futures-task",
+ "futures-util",
+]
+
 [[package]]
 name = "futures-channel"
 version = "0.3.31"
@@ -691,6 +706,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
 dependencies = [
  "futures-core",
+ "futures-sink",
 ]
 
 [[package]]
@@ -745,9 +761,11 @@ version = "0.3.31"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
 dependencies = [
+ "futures-channel",
  "futures-core",
  "futures-io",
  "futures-macro",
+ "futures-sink",
  "futures-task",
  "memchr",
  "pin-project-lite",
@@ -1839,6 +1857,7 @@ dependencies = [
  "actix-files",
  "actix-web",
  "bson",
+ "futures",
  "mongodb",
  "regex",
  "serde",

+ 1 - 0
Cargo.toml

@@ -14,3 +14,4 @@ bson = "2.8.0"
 uuid = {version = "1", features = ["v4"]}
 regex = "1"
 thiserror = "1.0"
+futures = "0.3.31"

+ 9 - 0
src/models/account.rs

@@ -1,6 +1,7 @@
 use serde::{Serialize, Deserialize};
 use bson::{oid::ObjectId, DateTime, Bson, doc};
 use mongodb::Collection;
+use futures::stream::TryStreamExt;
 
 use crate::app_error::AppError;
 use crate::dto::account::CreateInput;
@@ -39,6 +40,14 @@ impl Account {
         }
     }
 
+    pub async fn find_by_user(collection: &Collection<Account>, user_id: ObjectId) -> Result<Vec<Account>, AppError> {
+        Ok(collection
+            .find(doc!{"_id": user_id}, None)
+            .await?
+            .try_collect::<Vec<_>>()
+            .await?)
+    }
+
     pub async fn find_by_id(collection: &Collection<Account>, account_id: ObjectId) -> Result<Account, AppError> {
         match collection.find_one(doc!{"_id": account_id}, None).await {
             Ok(Some(a)) => Ok(a),