Skip to content

Commit fc143c3

Browse files
committed
[FEATURE] Find all method for Mongo repository implemented.
1 parent b745e76 commit fc143c3

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition = "2021"
99
tokio = { version = "1", features = ["full"] }
1010
async-trait = "0.1.80"
1111
serde_json = "1.0.115"
12+
futures-util = "0.3.30"
1213
mongodb = "2.8.2"
1314

1415
crossterm = "0.27.0"

src/infrastructure/repository/i_db_repository.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub trait IDBRepository: Clone + Send + Sync {
1010
async fn list_collections(&self, query: DataBaseQuery) -> Result<Vec<String>, ConnectException>;
1111
fn info(&self) -> Vec<u8>;
1212
async fn find(&self, query: DataBaseQuery) -> Result<Vec<u8>, ConnectException>;
13-
fn find_all(&self, query: DataBaseQuery) -> Vec<String>;
13+
async fn find_all(&self, query: DataBaseQuery) -> Result<Vec<String>, ConnectException>;
1414
fn insert(&self, query: DataBaseQuery, value: String) -> Vec<u8>;
1515
fn update(&self, query: DataBaseQuery, value: String) -> Vec<u8>;
1616
fn delete(&self, query: DataBaseQuery) -> Vec<u8>;

src/infrastructure/repository/mongo_db_repository.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use async_trait::async_trait;
22
use mongodb::{bson::Document, options::{AggregateOptions, ClientOptions}, Client, Collection, Database};
33

4+
use futures_util::stream::StreamExt;
5+
46
use crate::{
57
commons::exception::connect_exception::ConnectException,
68
domain::{
@@ -104,8 +106,35 @@ impl IDBRepository for MongoDbRepository {
104106
todo!()
105107
}
106108

107-
fn find_all(&self, query: DataBaseQuery) -> Vec<String> {
108-
todo!()
109+
async fn find_all(&self, query: DataBaseQuery) -> Result<Vec<String>, ConnectException> {
110+
let collection = self.collection(&query);
111+
112+
let r_cursor = collection.find(None, None).await;
113+
if r_cursor.is_err() {
114+
let exception = ConnectException::new(r_cursor.err().unwrap().to_string());
115+
return Err(exception);
116+
}
117+
118+
let mut elements = Vec::<String>::new();
119+
120+
let mut cursor = r_cursor.ok().unwrap();
121+
while let Some(result) = cursor.next().await {
122+
if result.is_err() {
123+
let exception = ConnectException::new(result.err().unwrap().to_string());
124+
return Err(exception);
125+
}
126+
127+
let document = result.ok().unwrap();
128+
129+
let json = serde_json::to_string(&document);
130+
if json.is_err() {
131+
let exception = ConnectException::new(json.err().unwrap().to_string());
132+
return Err(exception);
133+
}
134+
elements.push(json.ok().unwrap());
135+
}
136+
137+
Ok(elements)
109138
}
110139

111140
fn insert(&self, query: DataBaseQuery, value: String) -> Vec<u8> {

0 commit comments

Comments
 (0)