A api/src/actions/user/info.rs => api/src/actions/user/info.rs +29 -0
@@ 0,0 1,29 @@
+/*
+ * 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 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<UserPub, APIError> {
+ let user = match users::table
+ .filter(users::id.eq(&Uuid::try_parse(user_id).unwrap())) // TODO: handle
+ .first::<User>(conn)
+ {
+ Err(_) => {
+ return Err(APIError::UserNotFound);
+ }
+ Ok(user) => user,
+ };
+
+ Ok(UserPub::from_user(&user))
+}
M api/src/actions/user/mod.rs => api/src/actions/user/mod.rs +3 -0
@@ 6,6 6,9 @@
* See LICENSE for licensing information.
*/
+mod info;
+pub(crate) use info::info;
+
mod create;
pub(crate) use create::create;
A api/src/handlers/user/info.rs => api/src/handlers/user/info.rs +41 -0
@@ 0,0 1,41 @@
+/*
+ * 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")]
+pub(crate) async fn info(pool: web::Data<PgPool>, 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),
+ },
+ }
+ }
+ }
+}
M api/src/handlers/user/mod.rs => api/src/handlers/user/mod.rs +3 -0
@@ 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::*;
M api/src/main.rs => api/src/main.rs +1 -0
@@ 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)