Skip to content

Commit

Permalink
fix mongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
friendbear committed Sep 14, 2024
1 parent df5f100 commit 55c3c8c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions practice_collection/mongodatabase/src/product_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Repository<Product, i32, bool> for ProductRepository {
async fn select_all(&self) -> Result<Vec<Product>> {
// let doc = doc! {"price", 1 };
// let options = FindOptions::builder().sort(doc).build();
let mut cursor = self.collection.find(None, None).await?;
let mut cursor = self.collection.find(doc! {}).await?;
let mut products = Vec::new();
while let Some(product) = cursor.next().await {
products.push(product?);
Expand All @@ -35,19 +35,19 @@ impl Repository<Product, i32, bool> for ProductRepository {
}
async fn select_by_id(&self, id: i32) -> Result<Product> {
self.collection
.find_one(doc! {"product_id": id}, None)
.find_one(doc! {"product_id": id})
.await?
.ok_or(Error::msg("Not found"))
}
async fn insert(&self, row: Product) -> Result<bool> {
self.collection
.insert_one(row, None)
.insert_one(row)
.await
.map(|_| Ok(true))?
}
async fn insert_many(&self, cols: Vec<Product>) -> Result<bool> {
self.collection
.insert_many(cols.clone(), None)
.insert_many(cols.clone())
.await
.map(|result| {
if result.inserted_ids.iter().len() == cols.iter().len() {
Expand All @@ -63,7 +63,7 @@ impl Repository<Product, i32, bool> for ProductRepository {
doc! { "name": row.get_name(), "price": row.get_price() },
);
self.collection
.update_one(query, update, None)
.update_one(query, update)
.await
.map(|result| {
if result.modified_count == 1 {
Expand All @@ -77,7 +77,7 @@ impl Repository<Product, i32, bool> for ProductRepository {
async fn delete_by_id(&self, id: i32) -> Result<bool> {
let query = doc! { "product_id": id };
self.collection
.delete_one(query, None)
.delete_one(query)
.await
.map(|result| {
if result.deleted_count == 0 {
Expand Down

0 comments on commit 55c3c8c

Please sign in to comment.