how to work with update one #294
Comments
@karasjoh000 you just need to insert a new document into your update document under the IMHO, it would be easiest to construct the $set doc first, and then once everything has been added to it, create the parent doc. That would allow you to build the update doc using the // setdoc should already include all of the conditional
// fields, per your example above.
let update = doc!{“$set”: setdoc}; Then pass that update doc to the update method. |
Ah ha! when inserting the hash, need to use |
Working now, but that was weird the fact that it creates a new user when hash is not in the enum, is this a bug? should it spit out an error instead? |
Sounds like an algorithmic issue. Please copy and paste the code as a formatted Rust code block instead of an image. The images are a bit hard to follow. Also, if you don’t mind, perhaps you can include an overview of what the algorithm is supposed to be, that will make it easier to diagnose. Sound good? |
Here is the api endpoint that will call the update function upon request. #[derive(Deserialize)]
pub struct UpdateUser {
email: String,
update: UpdateUserData,
}
#[derive(Deserialize)]
pub struct UpdateUserData {
email: Option<String>,
password: Option<String>,
user_type: Option<i32>,
}
#[put(
"/users/update",
format = "application/json",
data = "<jupdate>",
rank = 3
)]
pub fn put_admin_user_update(
jupdate: Json<UpdateUser>,
admin: guards::Admin,
conn: db::Conn,
) -> Json<Value> {
let update = jupdate.into_inner();
if let Some(user_type) = db::users::get_user_type(update.email.clone(), &conn) {
if user_type == 1 {
if let Some(new_user_type) = update.update.user_type {
return Json(
json!({"status": "fail", "description": "cannot update user_type for admin"}),
);
}
}
}
if let Some(user_type) = update.update.user_type {
if user_type == 1 {
return Json(
json!({"status": "fail", "description": "cannot make more than one admin"}),
);
}
}
match db::users::update_user(
update.email,
update.update.email,
update.update.password,
update.update.user_type,
&conn,
) {
Ok(res) => Json(json!({"status": "ok"})),
Err(mongodb::Error::DefaultError(s)) => Json(json!({"status": "fail", "description": s})),
Err(e) => Json(json!({"status": "fail", "description": "mongo error"})),
}
} Here is the database function that will attempt to update user info. The problem is when the pub fn update_user(
email: String,
new_email: Option<String>,
new_password: Option<String>,
new_user_type: Option<i32>,
db: &Database,
) -> Result<(), mongodb::Error> {
let mut search = doc!{"email": email};
match search_doc(search.clone(), &db) {
Ok(b) => {
if !b {
return Err(mongodb::Error::DefaultError("user not found".to_string()));
}
}
Err(e) => return Err(e),
}
if new_email == None && new_password == None && new_user_type == None {
return Err(mongodb::Error::DefaultError(
"no update fields supplied".to_string(),
));
}
let mut update = Document::new();
if let Some(em) = new_email {
update.insert("email", bson::Bson::String(em));
}
if let Some(ps) = new_password {
let hash: String = scrypt_simple(&ps, &ScryptParams::new(14, 8, 1)).expect("hash error");
update.insert("hash", bson::Bson::String(hash));
}
if let Some(ut) = new_user_type {
update.insert("user_type", bson::Bson::I32(ut));
}
match db.collection("users").update_one(search, doc!{"$set": update}, None) {
Ok(res) => {
println!("{:?}", res);
Ok(())
},
Err(e) => {
println!("{}", e);
println!("update failed");
Err(e)
}
}
} In the fn search_doc(doc: Document, db: &Database) -> Result<bool, mongodb::Error> {
match db.collection("users").find_one(Some(doc.clone()), None) {
Ok(res) => match res {
Some(doc) => Ok(true),
None => Ok(false),
},
Err(t) => {
println!("search failed");
Err(t)
}
}
} I will try to investigate this problem a little more once my finals finish next month, might be some weird thing that happened somewhere in the code. |
idk, but thanks for |
lol no problem. Here is the full list of the different types: https://docs.rs/bson/0.6.0/bson/enum.Bson.html |
Hey @karasjoh000, so according to the code snippets above, have you resolved the original issue and now you are having a problem with the hash value being unexpected? It looks like the issue you originally opened this ticket for is resolved and you are no longer having an issue with using |
Yes the initial problem is solved. You can close it if you think its appropriate but there is a wierd bug if str is just provided instead of Bson::String(str) |
Cool. Glad to hear the first issue is resolved. As far as the string issue ... maybe you should close this issue and open a new one for the string issue specifically. That would def be easier to track. What exactly is the problem that you are seeing (would be good to add that info to the new issue)? Also, check out the wither project for modeling your data and such. It’s an ODM. It may help with some aspects of managing your data. |
@karasjoh000 hey. Just wanted to follow up with you and see if everything has been working out. If so, mind closing this issue? |
Hello, I have three fields that either all, some or one of them can be updated. Idk know how to set it up in order to take this into account. Attached is what I have so far, but this does not work because need to have those $set keywords in there somewhere somehow.
The text was updated successfully, but these errors were encountered: