diff --git a/src/data/http_error.rs b/src/api_data/http_error.rs similarity index 100% rename from src/data/http_error.rs rename to src/api_data/http_error.rs diff --git a/src/api_data/login_success.rs b/src/api_data/login_success.rs new file mode 100644 index 0000000..7db4834 --- /dev/null +++ b/src/api_data/login_success.rs @@ -0,0 +1,28 @@ +use serde::{Deserialize, Serialize}; + +/// Data sent in case of successful login +/// +/// @author Pierre Hubert +#[derive(Serialize, Deserialize)] +struct LoginTokens { + token1: String, + token2: String, +} + +#[derive(Serialize, Deserialize)] +pub struct LoginSuccess { + success: String, + tokens: LoginTokens, +} + +impl LoginSuccess { + pub fn new(token: &str) -> LoginSuccess { + LoginSuccess { + success: "Login successful".to_string(), + tokens: LoginTokens { + token1: token.to_string(), + token2: "dummy_data".to_string(), + }, + } + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs new file mode 100644 index 0000000..a0433ab --- /dev/null +++ b/src/api_data/mod.rs @@ -0,0 +1,9 @@ +///! # Presentation +///! This crate contains all the serializable structures that can be sent through the API to the +///! Clients +///! +///! ## Author +///! Pierre Hubert + +pub mod http_error; +pub mod login_success; \ No newline at end of file diff --git a/src/controllers/account_controller.rs b/src/controllers/account_controller.rs index 08d08a1..42bf5b2 100644 --- a/src/controllers/account_controller.rs +++ b/src/controllers/account_controller.rs @@ -1,23 +1,12 @@ -use serde::{Deserialize, Serialize}; -use crate::data::http_request_handler::HttpRequestHandler; +use crate::api_data::login_success::LoginSuccess; use crate::controllers::routes::RequestResult; +use crate::data::http_request_handler::HttpRequestHandler; use crate::helpers::account_helper; /// Account controller /// /// @author Pierre Hubert -#[derive(Serialize, Deserialize)] -struct LoginTokens { - token1: String, - token2: String, -} - -#[derive(Serialize, Deserialize)] -struct LoginSuccess { - tokens: LoginTokens -} - /// Sign in user pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult { let email = request.post_email("userMail")?; @@ -31,17 +20,12 @@ pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult { match token { Ok(t) => { - request.set_response(LoginSuccess { - tokens: LoginTokens { - token1: t, - token2: "dummy_data".to_string() - } - }) - }, + request.set_response(LoginSuccess::new(&t)) + } Err(e) => { // TODO : limit request println!("Error on login: {}", e); request.forbidden("Invalid email address / password!".to_string()) - }, + } } } \ No newline at end of file diff --git a/src/controllers/server.rs b/src/controllers/server.rs index 39d01e3..dedf696 100644 --- a/src/controllers/server.rs +++ b/src/controllers/server.rs @@ -2,7 +2,6 @@ use actix_web::{App, HttpResponse, HttpServer, web, http, FromRequest, HttpReque use crate::controllers::routes::{get_routes, Route, RequestResult}; use crate::data::config::Config; -use crate::data::http_error::HttpError; use crate::controllers::routes::Method::{GET, POST}; use crate::data::http_request_handler::{HttpRequestHandler, RequestValue}; use actix_web::dev::{PayloadStream, Payload, Decompress}; @@ -13,6 +12,7 @@ use actix_web::error::{ErrorBadRequest}; use encoding_rs::UTF_8; use std::collections::HashMap; use percent_encoding::{percent_decode_str}; +use crate::api_data::http_error::HttpError; /// Main server functions diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index fb9a486..14ab0d4 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -1,6 +1,5 @@ use actix_web::{web, HttpRequest, HttpResponse}; use crate::controllers::routes::RequestResult; -use crate::data::http_error::HttpError; use std::error::Error; use serde::Serialize; use crate::data::error::{ResultBoxError, ExecError}; @@ -10,6 +9,7 @@ use actix_web::http::{HeaderName, HeaderValue}; use std::str::FromStr; use crate::data::config::conf; use crate::data::api_client::APIClient; +use crate::api_data::http_error::HttpError; /// Http request handler /// diff --git a/src/data/mod.rs b/src/data/mod.rs index 07dc607..3ca7b88 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -1,7 +1,6 @@ pub mod error; pub mod config; -pub mod http_error; pub mod http_request_handler; pub mod api_client; diff --git a/src/lib.rs b/src/lib.rs index 97a73fa..0b18cbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ pub mod data; pub mod helpers; pub mod controllers; +pub mod api_data; + pub mod database_structure; \ No newline at end of file