From dadf4566415008256cf680d43049e6bb72e4f0e1 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 23 Mar 2023 13:05:14 +0200 Subject: [PATCH] feat(api): get /api/user for public user details --- api/src/actions/user/info.rs | 29 +++++++++++++++++++++++++ api/src/actions/user/mod.rs | 3 +++ api/src/handlers/user/info.rs | 41 +++++++++++++++++++++++++++++++++++ api/src/handlers/user/mod.rs | 3 +++ api/src/main.rs | 1 + 5 files changed, 77 insertions(+) create mode 100644 api/src/actions/user/info.rs create mode 100644 api/src/handlers/user/info.rs diff --git a/api/src/actions/user/info.rs b/api/src/actions/user/info.rs new file mode 100644 index 0000000..de0ba8d --- /dev/null +++ b/api/src/actions/user/info.rs @@ -0,0 +1,29 @@ +/* + * This file is part of laurelin/api + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl}; +use laurelin_schema::schema::users; +use laurelin_shared::{ + error::api::APIError, + types::user::{User, UserPub}, +}; +use uuid::Uuid; + +pub(crate) fn info(conn: &mut PgConnection, user_id: &str) -> Result { + let user = match users::table + .filter(users::id.eq(&Uuid::try_parse(user_id).unwrap())) // TODO: handle + .first::(conn) + { + Err(_) => { + return Err(APIError::UserNotFound); + } + Ok(user) => user, + }; + + Ok(UserPub::from_user(&user)) +} diff --git a/api/src/actions/user/mod.rs b/api/src/actions/user/mod.rs index cdab66c..a27e383 100644 --- a/api/src/actions/user/mod.rs +++ b/api/src/actions/user/mod.rs @@ -6,6 +6,9 @@ * See LICENSE for licensing information. */ +mod info; +pub(crate) use info::info; + mod create; pub(crate) use create::create; diff --git a/api/src/handlers/user/info.rs b/api/src/handlers/user/info.rs new file mode 100644 index 0000000..ab39053 --- /dev/null +++ b/api/src/handlers/user/info.rs @@ -0,0 +1,41 @@ +/* + * This file is part of laurelin/api + * Copyright (C) 2023 Jonni Liljamo + * + * 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")] +pub(crate) async fn info(pool: web::Data, session: Session) -> 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, &user_id) + }) + .await; + match user_details { + Err(_err) => { + return 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), + }, + } + } + } +} diff --git a/api/src/handlers/user/mod.rs b/api/src/handlers/user/mod.rs index 4beb783..c5bd910 100644 --- a/api/src/handlers/user/mod.rs +++ b/api/src/handlers/user/mod.rs @@ -9,6 +9,9 @@ use actix_session::Session; use actix_web::{get, HttpResponse, Responder}; +mod info; +pub(crate) use info::*; + mod create; pub(crate) use create::*; diff --git a/api/src/main.rs b/api/src/main.rs index 96170ab..f609178 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -96,6 +96,7 @@ async fn main() -> std::io::Result<()> { .service(ping) .service(ping_sec) .service(handlers::info) + .service(handlers::user::info) .service(handlers::user::create) .service(handlers::user::login) .service(handlers::user::logout) -- 2.44.1