Skip to content

Commit

Permalink
Do not wrap the property in a json object
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zero committed Apr 28, 2022
1 parent 60c5599 commit 5bbb8ba
Showing 1 changed file with 6 additions and 26 deletions.
32 changes: 6 additions & 26 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,8 @@ async fn handle_get_property(req: HttpRequest, state: web::Data<AppState>) -> Ht
};

let thing = thing.read().unwrap();
if thing.has_property(&property_name.to_string()) {
HttpResponse::Ok()
.json(json!({property_name: thing.get_property(&property_name.to_string()).unwrap()}))
if let Some(property) = thing.get_property(property_name) {
HttpResponse::Ok().json(json!(property))
} else {
HttpResponse::NotFound().finish()
}
Expand All @@ -494,33 +493,14 @@ async fn handle_put_property(
None => return HttpResponse::NotFound().finish(),
};

let args = match body.as_object() {
Some(args) => args,
None => {
return HttpResponse::BadRequest().json(bad_request(
"Parsing request failed",
Some(body.into_inner()),
))
}
};

let arg = if let Some(arg) = args.get(property_name) {
arg
} else {
return HttpResponse::BadRequest().json(bad_request(
"Request does not contain property key",
Some(json!(args)),
));
};
let args = body.into_inner();

let mut thing = thing.write().unwrap();
if thing.has_property(&property_name.to_string()) {
let set_property_result = thing.set_property(property_name.to_string(), arg.clone());
if thing.has_property(property_name) {
let set_property_result = thing.set_property(property_name.to_string(), args.clone());

match set_property_result {
Ok(()) => HttpResponse::Ok().json(
json!({property_name: thing.get_property(&property_name.to_string()).unwrap()}),
),
Ok(()) => HttpResponse::Ok().json(json!(thing.get_property(property_name).unwrap())),
Err(err) => HttpResponse::BadRequest().json(bad_request(err, Some(json!(args)))),
}
} else {
Expand Down

0 comments on commit 5bbb8ba

Please sign in to comment.