Skip to content

Commit

Permalink
refactor: add more semantic names to services
Browse files Browse the repository at this point in the history
  • Loading branch information
ParzivalEugene committed Aug 10, 2024
1 parent f14e229 commit 85ff9ce
Show file tree
Hide file tree
Showing 45 changed files with 73 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/guards/auth/access_token_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
config::ENV,
dto::auth::internal::AccessToken,
enums::errors::external::{Auth, Error},
services::device_service::check_logged_in_device,
services::device::check_logged_in_device,
state::AppState,
};

Expand Down
2 changes: 1 addition & 1 deletion src/guards/auth/refresh_token_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
config::ENV,
dto::auth::internal::RefreshToken,
enums::errors::external::{Auth, Error},
services::device_service::check_logged_in_device,
services::device::check_logged_in_device,
state::AppState,
};

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/auth/authorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use crate::{
},
enums::errors::external::Result,
extractors::Json,
services::user_service,
services::user,
state::AppState,
};

pub async fn authorize(
State(state): State<AppState>,
Json(payload): Json<AuthorizeRequest>,
) -> Result<Response<Tokens>> {
let tokens = user_service::authorize(
let tokens = user::authorize(
&state,
&OAuthCode {
code: payload.code.clone(),
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/auth/change_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use crate::{
},
enums::errors::external::Result,
extractors::Json,
services::user_service,
services::user,
};

pub async fn change_password(
claims: AccessToken,
State(pool): State<Pool>,
Json(payload): Json<ChangePasswordRequest>,
) -> Result<Response<String>> {
user_service::change_password(&pool, &claims.user_id, &payload.password).await?;
user::change_password(&pool, &claims.user_id, &payload.password).await?;

info!("Password changed successfully for user: {}", claims.user_id);

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/auth/logout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use tracing::info;
use crate::{
dto::{auth::internal::AccessToken, response::success::Response},
enums::errors::external::Result,
services::device_service,
services::device,
};

pub async fn logout(
State(pool): State<Pool>,
access_token: AccessToken,
) -> Result<Response<String>> {
device_service::logout_device(&pool, &access_token.device_id).await?;
device::logout(&pool, &access_token.device_id).await?;

info!("Device logged out: {:?}", access_token.device_id);

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/auth/sign_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use crate::{
},
enums::errors::external::Result,
extractors::Json,
services::user_service,
services::user,
};

pub async fn sign_in(
State(pool): State<Pool>,
Json(payload): Json<SignInRequest>,
) -> Result<Response<Tokens>> {
let tokens = user_service::sign_in(
let tokens = user::sign_in(
&pool,
&NewUser {
email: payload.email.clone(),
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/auth/sign_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use crate::{
},
enums::errors::external::Result,
extractors::Json,
services::user_service,
services::user,
};

pub async fn sign_up(
State(pool): State<Pool>,
Json(payload): Json<SignUpRequest>,
) -> Result<Response<Tokens>> {
let tokens = user_service::sign_up(
let tokens = user::sign_up(
&pool,
&NewUser {
email: payload.email.clone(),
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/device/get_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use tracing::info;
use crate::{
dto::{auth::internal::AccessToken, device::response::Device, response::success::Response},
enums::errors::external::Result,
services::device_service,
services::device,
};

pub async fn get_devices(
claims: AccessToken,
State(pool): State<Pool>,
) -> Result<Response<Vec<Device>>> {
let devices: Vec<Device> = device_service::get_devices(&pool, &claims.user_id)
let devices: Vec<Device> = device::get_many(&pool, &claims.user_id)
.await?
.into_iter()
.map(Device::from)
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/session/close_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use tracing::info;
use crate::{
dto::{auth::internal::AccessToken, response::success::Response},
enums::errors::external::Result,
services::session_service,
services::session,
};

pub async fn close_session(
claims: AccessToken,
State(pool): State<Pool>,
) -> Result<Response<String>> {
let session_id = session_service::close_session(&pool, &claims.device_id).await?;
let session_id = session::close(&pool, &claims.device_id).await?;

info!("Closed session successfully: {}", session_id);

Expand Down
5 changes: 2 additions & 3 deletions src/handlers/session/create_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ use crate::{
},
enums::errors::external::Result,
extractors::Json,
services::session_service,
services::session,
};

pub async fn create_session(
claims: AccessToken,
State(pool): State<Pool>,
Json(payload): Json<CreateSession>,
) -> Result<Response<Session>> {
let session =
session_service::create_session(&pool, &claims.device_id, &payload.country).await?;
let session = session::create(&pool, &claims.device_id, &payload.country).await?;

info!("Session created successfully: {}", session.session_id);

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/session/get_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
enums::errors::external::Result,
extractors::Json,
services::session_service,
services::session,
};

pub async fn get_history(
Expand All @@ -20,7 +20,7 @@ pub async fn get_history(
) -> Result<Response<Vec<SessionHistory>>> {
info!("Getting session history");

let history = session_service::get_history(&pool, &claims.user_id, &payload).await?;
let history = session::get_history(&pool, &claims.user_id, &payload).await?;

Ok(Response::new(StatusCode::OK, "Successfully got session history").with_data(history))
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
enums::errors::internal::Result,
};

pub async fn get_config_by_country(
pub async fn get_by_country(
pool: &deadpool_diesel::postgres::Pool,
country: &Country,
) -> Result<(Config, Server)> {
Expand Down
3 changes: 3 additions & 0 deletions src/services/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod get_by_country;

pub use get_by_country::get_by_country;
3 changes: 0 additions & 3 deletions src/services/config_service/mod.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
use diesel::prelude::*;
use tracing::info;

use super::get_device;
use super::get;
use crate::{
data::{enums::DeviceStatus, models::Device, schema},
dto::device::internal::NewDevice,
enums::errors::internal::Result,
};

pub async fn add_device(
pool: &deadpool_diesel::postgres::Pool,
new_device: &NewDevice,
) -> Result<Device> {
pub async fn add(pool: &deadpool_diesel::postgres::Pool, new_device: &NewDevice) -> Result<Device> {
let conn = pool.get().await?;
let new_device = new_device.clone();

if let Ok(device) = get_device(pool, &new_device).await {
if let Ok(device) = get(pool, &new_device).await {
conn.interact(move |conn| {
diesel::update(schema::device::table)
.filter(schema::device::name.eq(new_device.name))
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use crate::{
enums::errors::internal::Result,
};

pub async fn get_device(
pool: &deadpool_diesel::postgres::Pool,
device: &NewDevice,
) -> Result<Device> {
pub async fn get(pool: &deadpool_diesel::postgres::Pool, device: &NewDevice) -> Result<Device> {
let conn = pool.get().await?;
let device = device.clone();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
enums::errors::internal::Result,
};

pub async fn get_devices(
pub async fn get_many(
pool: &deadpool_diesel::postgres::Pool,
user_id: &Uuid,
) -> Result<Vec<Device>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
enums::errors::internal::Result,
};

pub async fn logout_device(pool: &deadpool_diesel::postgres::Pool, device_id: &Uuid) -> Result<()> {
pub async fn logout(pool: &deadpool_diesel::postgres::Pool, device_id: &Uuid) -> Result<()> {
let conn = pool.get().await?;
let device_id = *device_id;

Expand Down
11 changes: 11 additions & 0 deletions src/services/device/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod add;
mod check_logged_in;
mod get;
mod get_many;
mod logout;

pub use add::add;
pub use check_logged_in::check_logged_in_device;
pub use get::get;
pub use get_many::get_many;
pub use logout::logout;
11 changes: 0 additions & 11 deletions src/services/device_service/mod.rs

This file was deleted.

10 changes: 5 additions & 5 deletions src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod config_service;
pub mod device_service;
pub mod oauth_providers_service;
pub mod session_service;
pub mod user_service;
pub mod config;
pub mod device;
pub mod oauth_providers;
pub mod session;
pub mod user;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use uuid::Uuid;

use super::close_session_by_id;
use super::close_by_id;
use crate::{
dto::session::{interface::get_session, query::ActiveSessionAndDevice},
enums::errors::internal::Result,
};

pub async fn close_session(
pool: &deadpool_diesel::postgres::Pool,
device_id: &Uuid,
) -> Result<Uuid> {
pub async fn close(pool: &deadpool_diesel::postgres::Pool, device_id: &Uuid) -> Result<Uuid> {
let (session, _, _, _) = get_session(
pool,
ActiveSessionAndDevice {
device_id: *device_id,
},
)
.await?;
let session_id = close_session_by_id(pool, &session.id).await?;
let session_id = close_by_id(pool, &session.id).await?;
Ok(session_id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
enums::errors::internal::{self, Error, Result},
};

pub async fn close_session_by_id(
pub async fn close_by_id(
pool: &deadpool_diesel::postgres::Pool,
session_id: &Uuid,
) -> Result<Uuid> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use diesel::prelude::*;
use tracing::info;
use uuid::Uuid;

use super::close_session_by_id;
use super::close_by_id;
use crate::{
data::{
enums::{ConfigStatus, Country, SessionStatus},
Expand All @@ -16,10 +16,10 @@ use crate::{
response,
},
enums::errors::internal::Result,
services::config_service::get_config_by_country,
services::config::get_by_country,
};

pub async fn create_session(
pub async fn create(
pool: &deadpool_diesel::postgres::Pool,
device_id: &Uuid,
country: &Country,
Expand All @@ -40,10 +40,10 @@ pub async fn create_session(
}

if let Ok((session, _, _, _)) = get_session(pool, ActiveSessionAndDevice { device_id }).await {
let _ = close_session_by_id(pool, &session.id).await?;
let _ = close_by_id(pool, &session.id).await?;
}

let (config, server) = get_config_by_country(pool, &country).await?;
let (config, server) = get_by_country(pool, &country).await?;
let new_session = NewSession {
status: SessionStatus::Active,
device_id,
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions src/services/session/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod close;
mod close_by_id;
mod create;
mod get_history;

pub use close::close;
pub use close_by_id::close_by_id;
pub use create::create;
pub use get_history::get_history;
9 changes: 0 additions & 9 deletions src/services/session_service/mod.rs

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use crate::{
device::internal::{DeviceInfo, NewDevice},
},
enums::errors::internal::Result,
services::{device_service, oauth_providers_service},
services::{device, oauth_providers},
state::AppState,
utils::token::generate_tokens,
};

pub async fn authorize(state: &AppState, code: &OAuthCode, device: &DeviceInfo) -> Result<Tokens> {
let oauth_user = oauth_providers_service::authorize_user(state, code).await?;
let oauth_user = oauth_providers::authorize_user(state, code).await?;

let current_user = get_by_email(&state.pool, &oauth_user.email).await;
let user: FullUser;
Expand All @@ -40,7 +40,7 @@ pub async fn authorize(state: &AppState, code: &OAuthCode, device: &DeviceInfo)
user_id: user.user.id,
};

let device = device_service::add_device(&state.pool, &device).await?;
let device = device::add(&state.pool, &device).await?;

let tokens = generate_tokens(&user.user.id.to_string(), &device.id.to_string()).await?;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 85ff9ce

Please sign in to comment.