user: add a route to fetch by ID

Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2020-09-26 15:21:09 +05:30
parent 97f66956ba
commit beaca24246
No known key found for this signature in database
GPG Key ID: F475143EDEDEBA3C
2 changed files with 15 additions and 5 deletions

View File

@ -1,16 +1,17 @@
use rocket::response::status::Custom;
use anyhow::Result;
use rocket::{
http::{Status},
http::Status,
response::status,
Rocket,
};
use rocket::response::status::Custom;
use rocket_contrib::json::Json;
use serde_json::Value;
use crate::api::catchers::{internal_server_error, unprocessable_entity};
use crate::api::guards::db::DbConn;
use crate::core::users::entity::User;
use crate::core::users::service::{activate_user, create_or_fetch_user};
use crate::core::users::service::{activate_user, create_or_fetch_user, fetch_user};
#[post("/", data = "<user>")]
fn activate(mut user: Json<User>, conn: DbConn) -> Custom<Json<Value>> {
@ -39,6 +40,11 @@ fn activate(mut user: Json<User>, conn: DbConn) -> Custom<Json<Value>> {
}
}
pub fn fuel(rocket: Rocket) -> Rocket {
rocket.mount("/api/user", routes![activate])
#[get("/<id>")]
fn fetch(id: String, conn: DbConn) -> Result<Json<User>> {
Ok(Json(fetch_user(id, &conn)?))
}
pub fn fuel(rocket: Rocket) -> Rocket {
rocket.mount("/api/user", routes![activate, fetch])
}

View File

@ -33,6 +33,10 @@ pub fn create_or_fetch_user(id: String, conn: &PgConnection) -> Result<User> {
Ok(user)
}
pub fn fetch_user(id: String, conn: &PgConnection) -> Result<User> {
postgres::find_user(id, conn)
}
pub fn activate_user(user: &mut User, conn: &PgConnection) -> Result<User> {
let hashed_pass = hash(user.password.as_ref().unwrap().as_bytes(), DEFAULT_COST)?;
user.password = Some(hashed_pass);