From 4bf8f83ba8d76b155c05c610f34ccb34fb2af573 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Fri, 22 Mar 2024 14:57:11 +0100 Subject: [PATCH] refactor(api): promoted endpoints module to a directory --- src/api/endpoints.rs | 41 ----------------------- src/api/endpoints/account.rs | 64 ++++++++++++++++++++++++++++++++++++ src/api/endpoints/mod.rs | 1 + src/api/mod.rs | 2 +- 4 files changed, 66 insertions(+), 42 deletions(-) delete mode 100644 src/api/endpoints.rs create mode 100644 src/api/endpoints/account.rs create mode 100644 src/api/endpoints/mod.rs diff --git a/src/api/endpoints.rs b/src/api/endpoints.rs deleted file mode 100644 index 9dfea2c..0000000 --- a/src/api/endpoints.rs +++ /dev/null @@ -1,41 +0,0 @@ -use crate::backend::{error::AccountRegisterError, Backend}; -use actix_web::{post, web, HttpResponse, Responder}; -use log::error; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Deserialize)] -struct AccountRegisterRequest { - token: String, - password: String, -} - -#[derive(Debug, Serialize)] -struct AccountRegisterResponse { - uuid: String, -} - -#[post("/account/register")] -pub async fn account_register( - backend: web::Data, - body: web::Json, -) -> impl Responder { - let body = body.into_inner(); - match backend.account_register(body.token, body.password).await { - Err(e) => { - error!("{e}"); - HttpResponse::InternalServerError().finish() - } - Ok(res) => match res { - Err(e) => match e { - AccountRegisterError::InvalidToken => HttpResponse::Unauthorized().finish(), - AccountRegisterError::SqlError(e) => { - error!("{e}"); - HttpResponse::InternalServerError().finish() - } - }, - Ok(uuid) => HttpResponse::Ok().json(AccountRegisterResponse { - uuid: uuid.to_string(), - }), - }, - } -} diff --git a/src/api/endpoints/account.rs b/src/api/endpoints/account.rs new file mode 100644 index 0000000..d80aec9 --- /dev/null +++ b/src/api/endpoints/account.rs @@ -0,0 +1,64 @@ +use crate::backend::error::AccountRegisterError; +use crate::backend::Backend; +use actix_web::{post, web, HttpResponse, Responder}; +use log::error; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize)] +struct RegisterRequest { + token: String, + password: String, +} + +#[derive(Debug, Serialize)] +struct RegisterResponse { + uuid: String, +} + +#[post("/account/register")] +pub async fn register( + backend: web::Data, + body: web::Json, +) -> impl Responder { + let body = body.into_inner(); + match backend.account_register(body.token, body.password).await { + Err(e) => { + error!("{e}"); + HttpResponse::InternalServerError().finish() + } + Ok(res) => match res { + Err(e) => match e { + AccountRegisterError::InvalidToken => HttpResponse::Unauthorized().finish(), + AccountRegisterError::SqlError(e) => { + error!("{e}"); + HttpResponse::InternalServerError().finish() + } + }, + Ok(uuid) => HttpResponse::Ok().json(RegisterResponse { + uuid: uuid.to_string(), + }), + }, + } +} + +#[post("/account/new_token")] +pub async fn new_token(backend: web::Data) -> impl Responder { + match backend.account_register(body.token, body.password).await { + Err(e) => { + error!("{e}"); + HttpResponse::InternalServerError().finish() + } + Ok(res) => match res { + Err(e) => match e { + AccountRegisterError::InvalidToken => HttpResponse::Unauthorized().finish(), + AccountRegisterError::SqlError(e) => { + error!("{e}"); + HttpResponse::InternalServerError().finish() + } + }, + Ok(uuid) => HttpResponse::Ok().json(RegisterResponse { + uuid: uuid.to_string(), + }), + }, + } +} diff --git a/src/api/endpoints/mod.rs b/src/api/endpoints/mod.rs new file mode 100644 index 0000000..b0edc6c --- /dev/null +++ b/src/api/endpoints/mod.rs @@ -0,0 +1 @@ +pub mod account; diff --git a/src/api/mod.rs b/src/api/mod.rs index 02869b6..7e1e8a4 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -10,7 +10,7 @@ pub async fn start(config: &Config, backend: Backend) -> Result<()> { let server = HttpServer::new(move || { App::new() .app_data(web::Data::new(backend.clone())) - .service(endpoints::account_register) + .service(endpoints::account::register) }) .bind((config.addr.as_str(), config.port))?;