1
0
mirror of https://github.com/actix/examples synced 2025-06-28 18:00:37 +02:00

refactor: update mongodb to 3

This commit is contained in:
Rob Ede
2024-07-07 02:03:51 +01:00
parent 0bdd5479ff
commit dca1d50430
6 changed files with 100 additions and 91 deletions

View File

@ -15,7 +15,7 @@ const COLL_NAME: &str = "users";
#[post("/add_user")]
async fn add_user(client: web::Data<Client>, form: web::Form<User>) -> HttpResponse {
let collection = client.database(DB_NAME).collection(COLL_NAME);
let result = collection.insert_one(form.into_inner(), None).await;
let result = collection.insert_one(form.into_inner()).await;
match result {
Ok(_) => HttpResponse::Ok().body("user added"),
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
@ -27,10 +27,7 @@ async fn add_user(client: web::Data<Client>, form: web::Form<User>) -> HttpRespo
async fn get_user(client: web::Data<Client>, username: web::Path<String>) -> HttpResponse {
let username = username.into_inner();
let collection: Collection<User> = client.database(DB_NAME).collection(COLL_NAME);
match collection
.find_one(doc! { "username": &username }, None)
.await
{
match collection.find_one(doc! { "username": &username }).await {
Ok(Some(user)) => HttpResponse::Ok().json(user),
Ok(None) => {
HttpResponse::NotFound().body(format!("No user found with username {username}"))
@ -49,7 +46,7 @@ async fn create_username_index(client: &Client) {
client
.database(DB_NAME)
.collection::<User>(COLL_NAME)
.create_index(model, None)
.create_index(model)
.await
.expect("creating an index should succeed");
}

View File

@ -16,7 +16,7 @@ async fn test() {
client
.database(DB_NAME)
.collection::<User>(COLL_NAME)
.drop(None)
.drop()
.await
.expect("drop collection should succeed");