axum_responses 0.1.0

A Simple way to use Axum responses
Documentation

Example


// Asume we have a mongodb database connection and a user model

// A simple service that returns a generic or an error

use bson::
use axum_responses::{ApiResult, Response, HttpResponse};
use axum::Json;

#[derive(Debug, Serialize, Deserialize)]
struct LoginData {
    email: String,
    password: String
}

async fn get_user_by_id(filter: doc) -> ApiResult<User> {

    let user = User::find_by_id(filter).await
        .map_err(|| Response::Standard(500, "Internal Server Error"))    
    ;

    Ok(user)
}

// And then we can use it in a simple login controller like this

async fn login_controller(Json(body): Json<LoginData>) -> HttpResponse {

    let filter = doc! { "email": body.email };

    let user = get_user_by_id(filter).await?;

    if user.password != body.password {
        return Response::Standard(401, "Unauthorized");
    }

    Response::Standard(200, "OK")
}