diff --git a/src/data/enums/country.rs b/src/data/enums/country.rs index 3f6b1e4..7ad3969 100644 --- a/src/data/enums/country.rs +++ b/src/data/enums/country.rs @@ -1,13 +1,13 @@ -use crate::enums::errors::internal::{CountryError, InternalError}; use diesel::{ deserialize::{self, FromSql, FromSqlRow}, expression::AsExpression, pg::{Pg, PgValue}, serialize::{self, IsNull, Output, ToSql}, }; +use serde::{Deserialize, Serialize}; use std::io::Write; -#[derive(Debug, AsExpression, FromSqlRow, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, AsExpression, FromSqlRow, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] #[diesel(sql_type = crate::data::schema::sql_types::Country)] #[allow(clippy::upper_case_acronyms)] pub enum Country { @@ -16,17 +16,6 @@ pub enum Country { Germany, } -impl Country { - pub fn from_str(s: &str) -> Result { - match s { - "UK" => Ok(Country::UK), - "USA" => Ok(Country::USA), - "Germany" => Ok(Country::Germany), - _ => Err(InternalError::CountryError(CountryError::CountryNotFound)), - } - } -} - impl ToSql for Country { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result { match *self { diff --git a/src/data/enums/oauth_provider.rs b/src/data/enums/oauth_provider.rs index 59f00d6..baeb380 100644 --- a/src/data/enums/oauth_provider.rs +++ b/src/data/enums/oauth_provider.rs @@ -7,8 +7,6 @@ use diesel::{ use serde::{Deserialize, Serialize}; use std::io::Write; -use crate::enums::errors::internal::{AuthError, InternalError}; - #[derive(Debug, AsExpression, FromSqlRow, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] #[diesel(sql_type = crate::data::schema::sql_types::OAuthProvider)] pub enum OAuthProvider { @@ -17,17 +15,6 @@ pub enum OAuthProvider { Discord, } -impl OAuthProvider { - pub fn from_str(s: &str) -> Result { - match s { - "Github" => Ok(OAuthProvider::Github), - "Google" => Ok(OAuthProvider::Google), - "Discord" => Ok(OAuthProvider::Discord), - _ => Err(InternalError::AuthError(AuthError::UnknownOAuthProvider)), - } - } -} - impl ToSql for OAuthProvider { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result { match *self { diff --git a/src/data/enums/os.rs b/src/data/enums/os.rs index d11c78b..40a9232 100644 --- a/src/data/enums/os.rs +++ b/src/data/enums/os.rs @@ -27,19 +27,6 @@ pub enum OS { Unknown, } -impl OS { - pub fn from_str(s: &str) -> Self { - match s { - "Windows" => OS::Windows, - "Linux" => OS::Linux, - "MacOS" => OS::MacOS, - "Android" => OS::Android, - "IOS" => OS::IOS, - _ => OS::Unknown, - } - } -} - impl ToSql for OS { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result { match *self { diff --git a/src/dto/auth/request/authorize.rs b/src/dto/auth/request/authorize.rs index 34f8e44..dfb62c5 100644 --- a/src/dto/auth/request/authorize.rs +++ b/src/dto/auth/request/authorize.rs @@ -1,10 +1,10 @@ -use crate::dto::device::internal::DeviceInfo; +use crate::{data::enums::OAuthProvider, dto::device::internal::DeviceInfo}; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; #[derive(Debug, Serialize, Deserialize, Clone, ToSchema)] pub struct AuthorizeRequest { pub code: String, - pub provider: String, + pub provider: OAuthProvider, pub device: DeviceInfo, } diff --git a/src/dto/device/internal/device_info.rs b/src/dto/device/internal/device_info.rs index cfe4ad2..47cf345 100644 --- a/src/dto/device/internal/device_info.rs +++ b/src/dto/device/internal/device_info.rs @@ -1,10 +1,11 @@ use serde::{Deserialize, Serialize}; use utoipa::ToSchema; +use crate::data::enums::OS; #[derive(Debug, Serialize, Deserialize, ToSchema, Clone)] pub struct DeviceInfo { #[schema(example = "Android")] - pub os: String, + pub os: OS, #[schema(example = "Vitya Phone")] pub name: String, } diff --git a/src/dto/session/request/create_session.rs b/src/dto/session/request/create_session.rs index c8ee9d9..7e79023 100644 --- a/src/dto/session/request/create_session.rs +++ b/src/dto/session/request/create_session.rs @@ -1,7 +1,8 @@ +use crate::data::enums::Country; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; #[derive(Debug, Serialize, Deserialize, ToSchema, Clone)] pub struct CreateSession { - pub country: String, + pub country: Country, } diff --git a/src/handlers/auth/authorize.rs b/src/handlers/auth/authorize.rs index b8b9aa7..963c8af 100644 --- a/src/handlers/auth/authorize.rs +++ b/src/handlers/auth/authorize.rs @@ -1,5 +1,4 @@ use crate::{ - data::enums::OAuthProvider, dto::{ auth::{internal::OAuthCode, request::AuthorizeRequest, response::Tokens}, device::internal::DeviceInfo, @@ -21,7 +20,7 @@ pub async fn authorize( &state, &OAuthCode { code: payload.code.clone(), - provider: OAuthProvider::from_str(&payload.provider)?, + provider: payload.provider, }, &DeviceInfo { os: payload.device.os, diff --git a/src/handlers/session/create_session.rs b/src/handlers/session/create_session.rs index 7531c3d..ac9f5d9 100644 --- a/src/handlers/session/create_session.rs +++ b/src/handlers/session/create_session.rs @@ -1,5 +1,4 @@ use crate::{ - data::enums::Country, dto::{ auth::internal::AccessToken, response::success::Response, @@ -61,8 +60,7 @@ pub async fn create_session( State(pool): State, Json(payload): Json, ) -> Result, ExternalError> { - let country = Country::from_str(&payload.country)?; - let session = session_service::create_session(&pool, &claims.device_id, &country).await?; + let session = session_service::create_session(&pool, &claims.device_id, &payload.country).await?; info!("Session created successfully: {}", session.session_id); diff --git a/src/services/user_service.rs b/src/services/user_service.rs index 0de7bde..0d294ca 100644 --- a/src/services/user_service.rs +++ b/src/services/user_service.rs @@ -249,7 +249,7 @@ pub async fn sign_in( let device = NewDevice { name: device.name.clone(), - os: OS::from_str(&device.os), + os: device.os, user_id: user_db.user.id, }; let device = device_service::add_device(pool, &device).await?; @@ -297,7 +297,7 @@ pub async fn sign_up( let device = NewDevice { name: device.name.clone(), - os: OS::from_str(&device.os), + os: device.os, user_id: current_user.user.id, }; @@ -335,7 +335,7 @@ pub async fn authorize( let device = NewDevice { name: device.name.clone(), - os: OS::from_str(&device.os), + os: device.os, user_id: user.user.id, };