MCPcopy Create free account
hub / github.com/actix/examples / get_user

Function get_user

databases/mongodb/src/main.rs:27–37  ·  view source on GitHub ↗
(client: web::Data<Client>, username: web::Path<String>)

Source from the content-addressed store, hash-verified

25/// Gets the user with the supplied username.
26#[get("/get_user/{username}")]
27async fn get_user(client: web::Data<Client>, username: web::Path<String>) -> HttpResponse {
28 let username = username.into_inner();
29 let collection: Collection<User> = client.database(DB_NAME).collection(COLL_NAME);
30 match collection.find_one(doc! { "username": &username }).await {
31 Ok(Some(user)) => HttpResponse::Ok().json(user),
32 Ok(None) => {
33 HttpResponse::NotFound().body(format!("No user found with username {username}"))
34 }
35 Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
36 }
37}
38
39/// Creates an index on the "username" field to force the values to be unique.
40async fn create_username_index(client: &Client) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected