download pictures in the frontend and then create data-urls from blobs

This commit is contained in:
Johannes Heuel
2023-03-08 15:03:36 +01:00
parent fbcea9e77b
commit db2bf1994e
14 changed files with 500 additions and 165 deletions

View File

@@ -2,15 +2,19 @@ use std::env;
extern crate dotenv;
use dotenv::dotenv;
use crate::models::photo_model::Photo;
use crate::models::user_model::User;
use futures::stream::TryStreamExt;
use mongodb::{
bson::{doc, extjson::de::Error, oid::ObjectId},
bson::{doc, oid::ObjectId},
error::Error,
results::{DeleteResult, InsertOneResult, UpdateResult},
Client, Collection,
};
pub struct MongoRepo {
col: Collection<User>,
user_col: Collection<User>,
photo_col: Collection<Photo>,
}
impl MongoRepo {
@@ -22,8 +26,12 @@ impl MongoRepo {
};
let client = Client::with_uri_str(uri).await.unwrap();
let db = client.database("photos");
let col: Collection<User> = db.collection("User");
MongoRepo { col }
let user_col: Collection<User> = db.collection("User");
let photo_col: Collection<Photo> = db.collection("Photo");
MongoRepo {
user_col,
photo_col,
}
}
pub async fn create_user(&self, new_user: User) -> Result<InsertOneResult, Error> {
@@ -34,7 +42,7 @@ impl MongoRepo {
password: new_user.password,
};
let user = self
.col
.user_col
.insert_one(new_doc, None)
.await
.ok()
@@ -46,7 +54,7 @@ impl MongoRepo {
let obj_id = ObjectId::parse_str(id).unwrap();
let filter = doc! {"_id": obj_id};
let user_detail = self
.col
.user_col
.find_one(filter, None)
.await
.ok()
@@ -57,7 +65,7 @@ impl MongoRepo {
pub async fn get_user_from_email(&self, email: &String) -> Result<User, Error> {
let filter = doc! {"email": email};
let user_detail = self
.col
.user_col
.find_one(filter, None)
.await
.ok()
@@ -78,7 +86,7 @@ impl MongoRepo {
},
};
let updated_doc = self
.col
.user_col
.update_one(filter, new_doc, None)
.await
.ok()
@@ -90,11 +98,44 @@ impl MongoRepo {
let obj_id = ObjectId::parse_str(id).unwrap();
let filter = doc! {"_id": obj_id};
let user_detail = self
.col
.user_col
.delete_one(filter, None)
.await
.ok()
.expect("Error deleting user");
Ok(user_detail)
}
pub async fn create_photo(&self, new_photo: Photo) -> Result<InsertOneResult, Error> {
let new_doc = Photo {
id: None,
user_id: new_photo.user_id,
key: new_photo.key,
width: new_photo.width,
height: new_photo.height,
};
let photo = self
.photo_col
.insert_one(new_doc, None)
.await
.ok()
.expect("Error creating user");
Ok(photo)
}
pub async fn get_photos(&self, id: &String) -> Result<Vec<Photo>, Error> {
let obj_id = ObjectId::parse_str(id).unwrap();
let filter = doc! {"user_id": obj_id};
let mut cursor = self
.photo_col
.find(filter, None)
.await
.ok()
.expect("Error getting photos cursor");
let mut photos = Vec::new();
while let Some(photo) = cursor.try_next().await? {
photos.push(photo);
}
Ok(photos)
}
}