1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use bson::oid::ObjectId;
use bson::{doc, Bson::Document as BsonDocument, Document};
use log::trace;
use mongodb::error::Error;
use mongodb::options::FindOptions;
use mongodb::Database;
use serde::{Deserialize, Serialize};

#[cfg(feature = "write")]
use mongodb::{
    options::{DeleteOptions, UpdateModifications, UpdateOptions},
    results::{DeleteResult, InsertOneResult, UpdateResult},
};
#[cfg(feature = "write")]
use std::fmt::Debug;

#[cfg(feature = "read")]
pub async fn find_one<T>(
    filter_document: Document,
    collection_name: &str,
    db: &Database,
) -> Result<Option<T>, Error>
where
    for<'a> T: Serialize + Deserialize<'a>,
{
    trace!("find_one");
    let coll = db.collection(collection_name);
    let result = coll.find_one(filter_document, None)?;
    if let Some(document) = result {
        let t = bson::from_bson::<T>(BsonDocument(document))?;
        return Ok(Some(t));
    } else {
        return Ok(None);
    }
}

#[cfg(feature = "read")]
pub async fn find_by_id<T>(
    id: &ObjectId,
    collection_name: &str,
    db: &Database,
) -> Result<Option<T>, Error>
where
    for<'a> T: Serialize + Deserialize<'a>,
{
    trace!("find_by_id");
    let filter_document = doc! {"_id":  id};
    find_one(filter_document, &collection_name, &db).await
}

#[cfg(feature = "read")]
pub async fn find_by_string_id<T>(
    id: &String,
    collection_name: &str,
    db: &Database,
) -> Result<Option<T>, Error>
where
    for<'a> T: Serialize + Deserialize<'a>,
{
    trace!("find_by_string_id");
    let filter_document = doc! {"_id": id};
    find_one(filter_document, &collection_name, &db).await
}

#[cfg(feature = "read")]
pub async fn find_one_by_string_field<T>(
    name: &str,
    value: &String,
    collection_name: &str,
    db: &Database,
) -> Result<Option<T>, Error>
where
    for<'a> T: Serialize + Deserialize<'a>,
{
    trace!("find_by_string_id");
    let filter_document = doc! {name: value};
    find_one(filter_document, &collection_name, &db).await
}

#[cfg(feature = "read")]
pub async fn find_by_string_field<T>(
    name: &str,
    value: &String,
    collection_name: &str,
    db: &Database,
) -> Result<Vec<T>, Error>
where
    for<'a> T: Serialize + Deserialize<'a>,
{
    trace!("find_by_string_id");
    let filter_document = doc! {name: value};
    find(filter_document, &collection_name, &db).await
}

#[cfg(feature = "read")]
pub async fn find<T>(
    filter_document: Document,
    collection_name: &str,
    db: &Database,
) -> Result<Vec<T>, Error>
where
    for<'a> T: Serialize + Deserialize<'a>,
{
    trace!("find");
    find_generic(filter_document, None, collection_name, db).await
}

#[cfg(feature = "read")]
pub async fn find_with_sort<T>(
    filter_document: Document,
    sort_document: Document,
    collection_name: &str,
    db: &Database,
) -> Result<Vec<T>, Error>
where
    for<'a> T: Serialize + Deserialize<'a>,
{
    trace!("find_with_sort");
    find_generic(filter_document, Some(sort_document), collection_name, db).await
}

async fn find_generic<T>(
    filter_document: Document,
    sort_document_option: Option<Document>,
    collection_name: &str,
    db: &Database,
) -> Result<Vec<T>, Error>
where
    for<'a> T: Serialize + Deserialize<'a>,
{
    trace!("find_generic");
    let coll = db.collection(collection_name);
    let find_options = get_sort_find_option(sort_document_option);
    let mut cursor = coll.find(filter_document, find_options)?;
    let mut items = Vec::<T>::new();
    while let Some(result) = cursor.next() {
        match result {
            Ok(document) => {
                let item = bson::from_bson::<T>(BsonDocument(document))?;
                items.push(item);
            }
            Err(err) => {
                return Err(err);
            }
        }
    }

    Ok(items)
}

fn get_sort_find_option(sort_document_option: Option<Document>) -> Option<FindOptions> {
    if let Some(sort_document) = sort_document_option {
        Some(FindOptions::builder().sort(sort_document).build())
    } else {
        None
    }
}

#[cfg(feature = "read")]
pub async fn _find_one_by_field<T>(
    field_name: String,
    value: String,
    collection_name: &str,
    db: &Database,
) -> Result<Option<T>, Error>
where
    for<'a> T: Serialize + Deserialize<'a>,
{
    self::find_one(doc! {field_name: value}, collection_name, db).await
}

#[cfg(feature = "write")]
pub async fn add<T>(t: &T, collection_name: &str, db: &Database) -> Result<InsertOneResult, Error>
where
    for<'a> T: Debug + Serialize + Deserialize<'a>,
{
    let serialized_item = bson::to_bson(&t)?;

    if let BsonDocument(document) = serialized_item {
        let coll = db.collection(collection_name);
        coll.insert_one(document, None)
    } else {
        panic!("Error converting the BSON object into a MongoDB document");
    }
}

#[cfg(feature = "write")]
pub async fn update_one(
    query: Document,
    update: impl Into<UpdateModifications>,
    options: impl Into<Option<UpdateOptions>>,
    collection_name: &str,
    db: &Database,
) -> Result<UpdateResult, Error> {
    let coll = db.collection(collection_name);
    coll.update_one(query, update, options)
}

#[cfg(feature = "write")]
pub async fn delete_one(
    query: Document,
    options: impl Into<Option<DeleteOptions>>,
    collection_name: &str,
    db: &Database,
) -> Result<DeleteResult, Error> {
    let coll = db.collection(collection_name);
    coll.delete_one(query, options)
}