/*
* This file is part of laurelin/api
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use actix_session::Session;
use actix_web::{get, web, HttpResponse, Responder};
use laurelin_shared::error::api::APIError;
use crate::{actions, session, PgPool};
#[get("/api/user/{id}")]
pub(crate) async fn info(
pool: web::Data<PgPool>,
session: Session,
id: web::Path<String>,
) -> impl Responder {
let session_validation = session::validate_session(&session);
match session_validation {
Err(err) => err,
Ok(_user_id) => {
let user_details = web::block(move || {
let mut conn = match pool.get() {
Err(_) => return Err(APIError::DatabasePoolGetFailed),
Ok(conn) => conn,
};
actions::user::info(&mut conn, &id)
})
.await;
match user_details {
Err(_err) => HttpResponse::InternalServerError().json(APIError::Undefined),
Ok(user_details_res) => match user_details_res {
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(user_details) => HttpResponse::Ok().json(user_details),
},
}
}
}
}