From a58fe6334fcd286d89b918a5928f469e38b2ee03 Mon Sep 17 00:00:00 2001 From: Franklin Date: Mon, 2 Oct 2023 18:29:04 -0400 Subject: [PATCH] Fixed up enums instead of fromstr impls made sqlx::encode and decode impls. Plus, removed authentication from ANY DTO --- Cargo.lock | 3 ++ Cargo.toml | 3 +- src/domain/enums/order.rs | 2 +- src/domain/impls/db_impls.rs | 84 ++++++++++++++++++++++++++++++++++++ src/domain/impls/mod.rs | 1 + src/domain/league.rs | 17 ++++---- src/domain/league_player.rs | 8 ++-- src/domain/mod.rs | 3 +- src/domain/trust.rs | 7 +-- src/dto/league.rs | 4 -- src/dto/league_player.rs | 4 -- src/dto/player.rs | 11 +++-- src/dto/trust.rs | 4 -- 13 files changed, 117 insertions(+), 34 deletions(-) create mode 100644 src/domain/impls/db_impls.rs create mode 100644 src/domain/impls/mod.rs diff --git a/Cargo.lock b/Cargo.lock index d26be23..3e458d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -561,6 +561,8 @@ dependencies = [ "chrono", "err", "serde", + "serde_json", + "sqlx", ] [[package]] @@ -1083,6 +1085,7 @@ dependencies = [ "sha2", "sqlx-core", "sqlx-mysql", + "sqlx-postgres", "sqlx-sqlite", "syn 1.0.109", "tempfile", diff --git a/Cargo.toml b/Cargo.toml index 325fef8..0f05a8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ repository = "https://github.com/franklinblanco/league-types.git" [dependencies] serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1" } chrono = { version = "0.4", features = [ "serde" ] } - +sqlx = { version = "0.7", features = [ "postgres" ] } err = { git = "https://git.franklinblanco.dev/franklinblanco/err.git" } diff --git a/src/domain/enums/order.rs b/src/domain/enums/order.rs index 9a2cfe2..92d9205 100644 --- a/src/domain/enums/order.rs +++ b/src/domain/enums/order.rs @@ -1,7 +1,7 @@ /// Enum to signal from the client how they want their data. /// Convention: "Name+Asc/Desc" Asc = Ascending order | Desc = Descending order. -pub enum OrderBy{ +pub enum OrderBy { RelevanceAsc, AlphabeticalAsc, TimeCreatedAsc, diff --git a/src/domain/impls/db_impls.rs b/src/domain/impls/db_impls.rs new file mode 100644 index 0000000..135a22d --- /dev/null +++ b/src/domain/impls/db_impls.rs @@ -0,0 +1,84 @@ +use sqlx::{Postgres, postgres::{PgArgumentBuffer, PgTypeInfo, PgValueRef}}; + +use crate::domain::{league::{LeagueState, LeagueVisibility}, enums::league_player_status::LeaguePlayerStatus}; + +impl sqlx::Type for LeagueState { + fn type_info() -> PgTypeInfo { + <&str as sqlx::Type>::type_info() + } +} + +impl sqlx::Encode<'_, Postgres> for LeagueState { + fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> sqlx::encode::IsNull { + match serde_json::to_string(self) { + Ok(json_str) => <&str as sqlx::Encode>::encode(&json_str, buf), + Err(_) => sqlx::encode::IsNull::Yes + } + } +} +impl sqlx::Decode<'_, Postgres> for LeagueState { + fn decode(value: PgValueRef<'_>) -> Result { + match <&str as sqlx::Decode>::decode(value).map(|value| value.to_string()) { + Ok(json_str) => match serde_json::from_str(json_str.as_str()) { + Ok(league_state) => Ok(league_state), + Err(error) => Err(Box::new(error)), + }, + Err(error) => Err(error), + } + } +} + + +impl sqlx::Type for LeagueVisibility { + fn type_info() -> PgTypeInfo { + <&str as sqlx::Type>::type_info() + } +} + +impl sqlx::Encode<'_, Postgres> for LeagueVisibility { + fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> sqlx::encode::IsNull { + match serde_json::to_string(self) { + Ok(json_str) => <&str as sqlx::Encode>::encode(&json_str, buf), + Err(_) => sqlx::encode::IsNull::Yes + } + } +} +impl sqlx::Decode<'_, Postgres> for LeagueVisibility { + fn decode(value: PgValueRef<'_>) -> Result { + match <&str as sqlx::Decode>::decode(value).map(|value| value.to_string()) { + Ok(json_str) => match serde_json::from_str(json_str.as_str()) { + Ok(league_state) => Ok(league_state), + Err(error) => Err(Box::new(error)), + }, + Err(error) => Err(error), + } + } +} + + + +impl sqlx::Type for LeaguePlayerStatus { + fn type_info() -> PgTypeInfo { + <&str as sqlx::Type>::type_info() + } +} + +impl sqlx::Encode<'_, Postgres> for LeaguePlayerStatus { + fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> sqlx::encode::IsNull { + match serde_json::to_string(self) { + Ok(json_str) => <&str as sqlx::Encode>::encode(&json_str, buf), + Err(_) => sqlx::encode::IsNull::Yes + } + } +} +impl sqlx::Decode<'_, Postgres> for LeaguePlayerStatus { + fn decode(value: PgValueRef<'_>) -> Result { + match <&str as sqlx::Decode>::decode(value).map(|value| value.to_string()) { + Ok(json_str) => match serde_json::from_str(json_str.as_str()) { + Ok(league_state) => Ok(league_state), + Err(error) => Err(Box::new(error)), + }, + Err(error) => Err(error), + } + } +} \ No newline at end of file diff --git a/src/domain/impls/mod.rs b/src/domain/impls/mod.rs new file mode 100644 index 0000000..2ce55c7 --- /dev/null +++ b/src/domain/impls/mod.rs @@ -0,0 +1 @@ +pub mod db_impls; \ No newline at end of file diff --git a/src/domain/league.rs b/src/domain/league.rs index bd47006..0ba6698 100644 --- a/src/domain/league.rs +++ b/src/domain/league.rs @@ -17,8 +17,8 @@ pub struct League { pub time_created: DateTime, pub last_updated: DateTime, /// State as in: Is the league open or closed? Not the geographical sense. - pub state: String, - pub visibility: String, + pub state: LeagueState, + pub visibility: LeagueVisibility, /// When is the league happening? pub date_and_time: DateTime, /// This will be stored as a Decimal in the database but the actual input from the user @@ -31,14 +31,15 @@ pub struct League { pub description: Option } -impl From for League { - fn from(league_dto: LeagueForCreationDto) -> Self { +#[allow(unused)] +impl League { + fn from_league_creation_dto(league_dto: LeagueForCreationDto, owner_id: i32) -> Self { Self { - id: 0, owner_id: league_dto.user_id, sport_id: league_dto.sport_id, place_id:league_dto.place_id, time_created: Utc::now(), last_updated: Utc::now(), state: LeagueState::Open.to_string(), + id: 0, owner_id, sport_id: league_dto.sport_id, place_id:league_dto.place_id, time_created: Utc::now(), last_updated: Utc::now(), state: LeagueState::Open, visibility: match league_dto.visibility { - Some(visibility) => visibility.to_string(), - None => LeagueVisibility::Public.to_string(), - }, + Some(visibility) => visibility, + None => LeagueVisibility::Public, + }, date_and_time: league_dto.date_and_time, cost_to_join: league_dto.cost_to_join, currency: league_dto.currency, max_players: league_dto.max_players, description: league_dto.description } } diff --git a/src/domain/league_player.rs b/src/domain/league_player.rs index 01986f9..084a50b 100644 --- a/src/domain/league_player.rs +++ b/src/domain/league_player.rs @@ -12,10 +12,10 @@ pub struct LeaguePlayer { pub player_id: i32, pub time_created: DateTime, pub last_updated: DateTime, - pub status: String + pub status: LeaguePlayerStatus } -impl From for LeaguePlayer { - fn from(join_req: JoinRequest) -> Self { - Self { id: 0, league_id: join_req.league_id, player_id: join_req.user_id, time_created: Utc::now(), last_updated: Utc::now(), status: LeaguePlayerStatus::Requested.to_string() } +impl LeaguePlayer { + pub fn from_join_req(join_req: JoinRequest, player_id: i32) -> Self { + Self { id: 0, league_id: join_req.league_id, player_id: player_id, time_created: Utc::now(), last_updated: Utc::now(), status: LeaguePlayerStatus::Requested } } } \ No newline at end of file diff --git a/src/domain/mod.rs b/src/domain/mod.rs index f3f0de1..59745f3 100644 --- a/src/domain/mod.rs +++ b/src/domain/mod.rs @@ -7,4 +7,5 @@ pub mod sport; pub mod enums; pub mod league_player; pub mod trust; -pub mod dao_utils; \ No newline at end of file +pub mod dao_utils; +pub mod impls; \ No newline at end of file diff --git a/src/domain/trust.rs b/src/domain/trust.rs index c842e8e..a49bc71 100644 --- a/src/domain/trust.rs +++ b/src/domain/trust.rs @@ -15,8 +15,9 @@ pub struct Trust { pub last_updated: DateTime } -impl From for Trust { - fn from(trust_dto: TrustRequestDto) -> Self { - Trust { id: 0, truster_id: trust_dto.truster_id, trustee_id: trust_dto.trustee_id, time_created: Utc::now(), last_updated: Utc::now() } +#[allow(unused)] +impl Trust { + fn from_trust_dto(trust_dto: TrustRequestDto, player_id: i32) -> Self { + Trust { id: 0, truster_id: player_id, trustee_id: trust_dto.trustee_id, time_created: Utc::now(), last_updated: Utc::now() } } } \ No newline at end of file diff --git a/src/dto/league.rs b/src/dto/league.rs index 39e66ca..8eb25b7 100644 --- a/src/dto/league.rs +++ b/src/dto/league.rs @@ -4,10 +4,6 @@ use crate::domain::league::LeagueVisibility; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, PartialOrd, Default)] pub struct LeagueForCreationDto{ - #[serde(rename = "userId")] - pub user_id: i32, - #[serde(rename = "authToken")] - pub auth_token: String, #[serde(rename = "sportId")] pub sport_id: i32, #[serde(rename = "placeId")] diff --git a/src/dto/league_player.rs b/src/dto/league_player.rs index 3765906..d9db828 100644 --- a/src/dto/league_player.rs +++ b/src/dto/league_player.rs @@ -6,8 +6,4 @@ use serde::{Deserialize, Serialize}; pub struct JoinRequest { #[serde(rename = "leagueId")] pub league_id: i32, - #[serde(rename = "userId")] - pub user_id: i32, - #[serde(rename = "authToken")] - pub auth_token: String, } \ No newline at end of file diff --git a/src/dto/player.rs b/src/dto/player.rs index 2fbce56..2f26499 100644 --- a/src/dto/player.rs +++ b/src/dto/player.rs @@ -26,10 +26,13 @@ pub struct PlayerForUpdateDto { pub bio: Option, #[serde(rename = "profilePictureUrl")] pub profile_picture_url: Option, - #[serde(rename = "userId")] - pub user_id: i32, - #[serde(rename = "authToken")] - pub auth_token: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] +#[serde(rename_all = "camelCase")] +pub struct PlayerForLoginDto { + pub phone_number: String, + pub password: String, } /*impl PlayerForCreationDto { diff --git a/src/dto/trust.rs b/src/dto/trust.rs index 53118ea..2488329 100644 --- a/src/dto/trust.rs +++ b/src/dto/trust.rs @@ -4,10 +4,6 @@ use serde::{Serialize, Deserialize}; /// This DTO can be used to add to trusted list or to remove. Depends on the endpoint #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct TrustRequestDto { - #[serde(rename = "authToken")] - pub auth_token: String, - #[serde(rename = "trusterId")] - pub truster_id: i32, #[serde(rename = "trusteeId")] pub trustee_id: i32, } \ No newline at end of file