Fixed up enums instead of fromstr impls made sqlx::encode and decode impls. Plus, removed authentication from ANY DTO

This commit is contained in:
Franklin 2023-10-02 18:29:04 -04:00
parent 96f0ea15cf
commit a58fe6334f
13 changed files with 117 additions and 34 deletions

3
Cargo.lock generated
View File

@ -561,6 +561,8 @@ dependencies = [
"chrono", "chrono",
"err", "err",
"serde", "serde",
"serde_json",
"sqlx",
] ]
[[package]] [[package]]
@ -1083,6 +1085,7 @@ dependencies = [
"sha2", "sha2",
"sqlx-core", "sqlx-core",
"sqlx-mysql", "sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite", "sqlx-sqlite",
"syn 1.0.109", "syn 1.0.109",
"tempfile", "tempfile",

View File

@ -12,6 +12,7 @@ repository = "https://github.com/franklinblanco/league-types.git"
[dependencies] [dependencies]
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1" }
chrono = { version = "0.4", features = [ "serde" ] } chrono = { version = "0.4", features = [ "serde" ] }
sqlx = { version = "0.7", features = [ "postgres" ] }
err = { git = "https://git.franklinblanco.dev/franklinblanco/err.git" } err = { git = "https://git.franklinblanco.dev/franklinblanco/err.git" }

View File

@ -1,7 +1,7 @@
/// Enum to signal from the client how they want their data. /// Enum to signal from the client how they want their data.
/// Convention: "Name+Asc/Desc" Asc = Ascending order | Desc = Descending order. /// Convention: "Name+Asc/Desc" Asc = Ascending order | Desc = Descending order.
pub enum OrderBy{ pub enum OrderBy {
RelevanceAsc, RelevanceAsc,
AlphabeticalAsc, AlphabeticalAsc,
TimeCreatedAsc, TimeCreatedAsc,

View File

@ -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<Postgres> for LeagueState {
fn type_info() -> PgTypeInfo {
<&str as sqlx::Type<Postgres>>::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<Postgres>>::encode(&json_str, buf),
Err(_) => sqlx::encode::IsNull::Yes
}
}
}
impl sqlx::Decode<'_, Postgres> for LeagueState {
fn decode(value: PgValueRef<'_>) -> Result<Self, sqlx::error::BoxDynError> {
match <&str as sqlx::Decode<Postgres>>::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<Postgres> for LeagueVisibility {
fn type_info() -> PgTypeInfo {
<&str as sqlx::Type<Postgres>>::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<Postgres>>::encode(&json_str, buf),
Err(_) => sqlx::encode::IsNull::Yes
}
}
}
impl sqlx::Decode<'_, Postgres> for LeagueVisibility {
fn decode(value: PgValueRef<'_>) -> Result<Self, sqlx::error::BoxDynError> {
match <&str as sqlx::Decode<Postgres>>::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<Postgres> for LeaguePlayerStatus {
fn type_info() -> PgTypeInfo {
<&str as sqlx::Type<Postgres>>::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<Postgres>>::encode(&json_str, buf),
Err(_) => sqlx::encode::IsNull::Yes
}
}
}
impl sqlx::Decode<'_, Postgres> for LeaguePlayerStatus {
fn decode(value: PgValueRef<'_>) -> Result<Self, sqlx::error::BoxDynError> {
match <&str as sqlx::Decode<Postgres>>::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),
}
}
}

1
src/domain/impls/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod db_impls;

View File

@ -17,8 +17,8 @@ pub struct League {
pub time_created: DateTime<Utc>, pub time_created: DateTime<Utc>,
pub last_updated: DateTime<Utc>, pub last_updated: DateTime<Utc>,
/// State as in: Is the league open or closed? Not the geographical sense. /// State as in: Is the league open or closed? Not the geographical sense.
pub state: String, pub state: LeagueState,
pub visibility: String, pub visibility: LeagueVisibility,
/// When is the league happening? /// When is the league happening?
pub date_and_time: DateTime<Utc>, pub date_and_time: DateTime<Utc>,
/// This will be stored as a Decimal in the database but the actual input from the user /// This will be stored as a Decimal in the database but the actual input from the user
@ -31,13 +31,14 @@ pub struct League {
pub description: Option<String> pub description: Option<String>
} }
impl From<LeagueForCreationDto> for League { #[allow(unused)]
fn from(league_dto: LeagueForCreationDto) -> Self { impl League {
fn from_league_creation_dto(league_dto: LeagueForCreationDto, owner_id: i32) -> Self {
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 { visibility: match league_dto.visibility {
Some(visibility) => visibility.to_string(), Some(visibility) => visibility,
None => LeagueVisibility::Public.to_string(), 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 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
} }

View File

@ -12,10 +12,10 @@ pub struct LeaguePlayer {
pub player_id: i32, pub player_id: i32,
pub time_created: DateTime<Utc>, pub time_created: DateTime<Utc>,
pub last_updated: DateTime<Utc>, pub last_updated: DateTime<Utc>,
pub status: String pub status: LeaguePlayerStatus
} }
impl From<JoinRequest> for LeaguePlayer { impl LeaguePlayer {
fn from(join_req: JoinRequest) -> Self { pub fn from_join_req(join_req: JoinRequest, player_id: i32) -> 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() } Self { id: 0, league_id: join_req.league_id, player_id: player_id, time_created: Utc::now(), last_updated: Utc::now(), status: LeaguePlayerStatus::Requested }
} }
} }

View File

@ -8,3 +8,4 @@ pub mod enums;
pub mod league_player; pub mod league_player;
pub mod trust; pub mod trust;
pub mod dao_utils; pub mod dao_utils;
pub mod impls;

View File

@ -15,8 +15,9 @@ pub struct Trust {
pub last_updated: DateTime<Utc> pub last_updated: DateTime<Utc>
} }
impl From<TrustRequestDto> for Trust { #[allow(unused)]
fn from(trust_dto: TrustRequestDto) -> Self { impl Trust {
Trust { id: 0, truster_id: trust_dto.truster_id, trustee_id: trust_dto.trustee_id, time_created: Utc::now(), last_updated: Utc::now() } 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() }
} }
} }

View File

@ -4,10 +4,6 @@ use crate::domain::league::LeagueVisibility;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, PartialOrd, Default)] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, PartialOrd, Default)]
pub struct LeagueForCreationDto{ pub struct LeagueForCreationDto{
#[serde(rename = "userId")]
pub user_id: i32,
#[serde(rename = "authToken")]
pub auth_token: String,
#[serde(rename = "sportId")] #[serde(rename = "sportId")]
pub sport_id: i32, pub sport_id: i32,
#[serde(rename = "placeId")] #[serde(rename = "placeId")]

View File

@ -6,8 +6,4 @@ use serde::{Deserialize, Serialize};
pub struct JoinRequest { pub struct JoinRequest {
#[serde(rename = "leagueId")] #[serde(rename = "leagueId")]
pub league_id: i32, pub league_id: i32,
#[serde(rename = "userId")]
pub user_id: i32,
#[serde(rename = "authToken")]
pub auth_token: String,
} }

View File

@ -26,10 +26,13 @@ pub struct PlayerForUpdateDto {
pub bio: Option<String>, pub bio: Option<String>,
#[serde(rename = "profilePictureUrl")] #[serde(rename = "profilePictureUrl")]
pub profile_picture_url: Option<String>, pub profile_picture_url: Option<String>,
#[serde(rename = "userId")] }
pub user_id: i32,
#[serde(rename = "authToken")] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
pub auth_token: String, #[serde(rename_all = "camelCase")]
pub struct PlayerForLoginDto {
pub phone_number: String,
pub password: String,
} }
/*impl PlayerForCreationDto { /*impl PlayerForCreationDto {

View File

@ -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 /// 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)] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct TrustRequestDto { pub struct TrustRequestDto {
#[serde(rename = "authToken")]
pub auth_token: String,
#[serde(rename = "trusterId")]
pub truster_id: i32,
#[serde(rename = "trusteeId")] #[serde(rename = "trusteeId")]
pub trustee_id: i32, pub trustee_id: i32,
} }