switch to mongodb, add drag&drop
This commit is contained in:
100
backend/src/repository/mongodb_repo.rs
Normal file
100
backend/src/repository/mongodb_repo.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use std::env;
|
||||
extern crate dotenv;
|
||||
use dotenv::dotenv;
|
||||
|
||||
use crate::models::user_model::User;
|
||||
use mongodb::{
|
||||
bson::{doc, extjson::de::Error, oid::ObjectId},
|
||||
results::{DeleteResult, InsertOneResult, UpdateResult},
|
||||
Client, Collection,
|
||||
};
|
||||
|
||||
pub struct MongoRepo {
|
||||
col: Collection<User>,
|
||||
}
|
||||
|
||||
impl MongoRepo {
|
||||
pub async fn init() -> Self {
|
||||
dotenv().ok();
|
||||
let uri = match env::var("MONGOURI") {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(_) => format!("Error loading env variable"),
|
||||
};
|
||||
let client = Client::with_uri_str(uri).await.unwrap();
|
||||
let db = client.database("photos");
|
||||
let col: Collection<User> = db.collection("User");
|
||||
MongoRepo { col }
|
||||
}
|
||||
|
||||
pub async fn create_user(&self, new_user: User) -> Result<InsertOneResult, Error> {
|
||||
let new_doc = User {
|
||||
id: None,
|
||||
username: new_user.username,
|
||||
email: new_user.email,
|
||||
password: new_user.password,
|
||||
};
|
||||
let user = self
|
||||
.col
|
||||
.insert_one(new_doc, None)
|
||||
.await
|
||||
.ok()
|
||||
.expect("Error creating user");
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn get_user(&self, id: &String) -> Result<User, Error> {
|
||||
let obj_id = ObjectId::parse_str(id).unwrap();
|
||||
let filter = doc! {"_id": obj_id};
|
||||
let user_detail = self
|
||||
.col
|
||||
.find_one(filter, None)
|
||||
.await
|
||||
.ok()
|
||||
.expect("Error getting user's detail");
|
||||
Ok(user_detail.unwrap())
|
||||
}
|
||||
|
||||
pub async fn get_user_from_email(&self, email: &String) -> Result<User, Error> {
|
||||
let filter = doc! {"email": email};
|
||||
let user_detail = self
|
||||
.col
|
||||
.find_one(filter, None)
|
||||
.await
|
||||
.ok()
|
||||
.expect("Error getting user's detail");
|
||||
Ok(user_detail.unwrap())
|
||||
}
|
||||
|
||||
pub async fn update_user(&self, id: &String, new_user: User) -> Result<UpdateResult, Error> {
|
||||
let obj_id = ObjectId::parse_str(id).unwrap();
|
||||
let filter = doc! {"_id": obj_id};
|
||||
let new_doc = doc! {
|
||||
"$set":
|
||||
{
|
||||
"id": new_user.id,
|
||||
"username": new_user.username,
|
||||
"email": new_user.email,
|
||||
"password": new_user.password,
|
||||
},
|
||||
};
|
||||
let updated_doc = self
|
||||
.col
|
||||
.update_one(filter, new_doc, None)
|
||||
.await
|
||||
.ok()
|
||||
.expect("Error updating user");
|
||||
Ok(updated_doc)
|
||||
}
|
||||
|
||||
pub async fn delete_user(&self, id: &String) -> Result<DeleteResult, Error> {
|
||||
let obj_id = ObjectId::parse_str(id).unwrap();
|
||||
let filter = doc! {"_id": obj_id};
|
||||
let user_detail = self
|
||||
.col
|
||||
.delete_one(filter, None)
|
||||
.await
|
||||
.ok()
|
||||
.expect("Error deleting user");
|
||||
Ok(user_detail)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user