From ac3dec3b188c1ce27a7df5f4ab6f76f2d501fe36 Mon Sep 17 00:00:00 2001 From: Franklin Date: Sat, 8 Oct 2022 18:09:12 -0400 Subject: [PATCH] Implemented most traits and made some corrections --- src/domain/dao_utils.rs | 3 +++ src/domain/enums/league_player_status.rs | 3 ++- src/domain/field.rs | 2 +- src/domain/league.rs | 17 ++++++++--------- src/domain/league_player.rs | 11 ++++------- src/domain/place.rs | 9 ++------- src/domain/player.rs | 14 +++++++------- src/domain/sport.rs | 7 +------ src/domain/sport_category.rs | 7 +------ src/domain/trust.rs | 10 ++++------ src/dto/league.rs | 4 ++-- src/dto/league_player.rs | 2 +- src/dto/player.rs | 6 +++--- src/dto/trust.rs | 2 +- 14 files changed, 40 insertions(+), 57 deletions(-) diff --git a/src/domain/dao_utils.rs b/src/domain/dao_utils.rs index 14a8c39..3318fe2 100644 --- a/src/domain/dao_utils.rs +++ b/src/domain/dao_utils.rs @@ -1,3 +1,6 @@ +use serde::{Serialize, Deserialize}; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct Count { pub count: i64, } \ No newline at end of file diff --git a/src/domain/enums/league_player_status.rs b/src/domain/enums/league_player_status.rs index 62f8684..2854cfc 100644 --- a/src/domain/enums/league_player_status.rs +++ b/src/domain/enums/league_player_status.rs @@ -3,10 +3,11 @@ use std::{fmt::Display, str::FromStr}; use serde::{Serialize, Deserialize}; use err::Error; -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub enum LeaguePlayerStatus { Denied, Joined, + #[default] Requested, Kicked } diff --git a/src/domain/field.rs b/src/domain/field.rs index 2a24e01..9fcaf8e 100644 --- a/src/domain/field.rs +++ b/src/domain/field.rs @@ -3,7 +3,7 @@ use rust_decimal::Decimal; use serde::{Serialize, Deserialize}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct Field { pub id: u32, pub place_id: u32, diff --git a/src/domain/league.rs b/src/domain/league.rs index 2a2d6a6..efddfd5 100644 --- a/src/domain/league.rs +++ b/src/domain/league.rs @@ -9,7 +9,7 @@ use rust_decimal::Decimal; use crate::dto::league::LeagueForCreationDto; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct League { pub id: u32, pub owner_id: u32, @@ -32,12 +32,9 @@ pub struct League { pub description: Option } -impl League { - pub fn new() -> League { - League { id: 0, owner_id: 0, sport_id: 0, place_id: 0, time_created: Utc::now().naive_utc(), last_updated: Utc::now().naive_utc(), state: "".to_string(), visibility: "".to_string(), date_and_time: Utc::now().naive_utc(), cost_to_join: Decimal::new(0, 0), currency: None, max_players: 0, description: None } - } - pub fn new_from_league_for_creation_dto(league_dto: LeagueForCreationDto) -> League { - League { +impl From for League { + fn from(league_dto: LeagueForCreationDto) -> 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().naive_utc(), last_updated: Utc::now().naive_utc(), state: LeagueState::Open.to_string(), visibility: match league_dto.visibility { Some(visibility) => visibility.to_string(), @@ -48,9 +45,10 @@ impl League { } } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub enum LeagueState { /// Taking new players + #[default] Open, /// No more people Closed @@ -64,9 +62,10 @@ impl Display for LeagueState { } } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub enum LeagueVisibility { /// Open to anyone, anyone can join + #[default] Public, /// People request to join Private, diff --git a/src/domain/league_player.rs b/src/domain/league_player.rs index 0835336..5c98fd2 100644 --- a/src/domain/league_player.rs +++ b/src/domain/league_player.rs @@ -5,7 +5,7 @@ use crate::dto::league_player::JoinRequest; use super::enums::league_player_status::LeaguePlayerStatus; -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct LeaguePlayer { pub id: u32, pub league_id: u32, @@ -14,11 +14,8 @@ pub struct LeaguePlayer { pub last_updated: NaiveDateTime, pub status: String } -impl LeaguePlayer { - pub fn new_empty() -> LeaguePlayer { - LeaguePlayer { id: 0, league_id: 0, player_id: 0, time_created: Utc::now().naive_utc(), last_updated: Utc::now().naive_utc(), status: "".to_string()} - } - pub fn new_from_join_request(join_req: JoinRequest) -> LeaguePlayer { - LeaguePlayer { id: 0, league_id: join_req.league_id, player_id: join_req.user_id, time_created: Utc::now().naive_utc(), last_updated: Utc::now().naive_utc(), status: LeaguePlayerStatus::Requested.to_string() } +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().naive_utc(), last_updated: Utc::now().naive_utc(), status: LeaguePlayerStatus::Requested.to_string() } } } \ No newline at end of file diff --git a/src/domain/place.rs b/src/domain/place.rs index 7fe5d31..4eeed3f 100644 --- a/src/domain/place.rs +++ b/src/domain/place.rs @@ -1,7 +1,7 @@ -use chrono::{NaiveDateTime, Utc}; +use chrono::{NaiveDateTime}; use serde::{Serialize, Deserialize}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct Place{ pub id: u32, pub time_created: NaiveDateTime, @@ -15,9 +15,4 @@ pub struct Place{ pub maps_url: Option, pub contact_number: Option, pub picture_url: Option -} -impl Place { - pub fn new() -> Place{ - Place { id: 0, time_created: Utc::now().naive_utc(), last_updated: Utc::now().naive_utc(), name: "".to_string(), sport_id: 0, country: "".to_string(), state: None, city: "".to_string(), address: "".to_string(), maps_url: None, contact_number: None, picture_url: None } - } } \ No newline at end of file diff --git a/src/domain/player.rs b/src/domain/player.rs index c0ea06b..f9ea495 100644 --- a/src/domain/player.rs +++ b/src/domain/player.rs @@ -4,7 +4,7 @@ use serde::{Serialize, Deserialize}; use crate::dto::player::PlayerForCreationDto; //TODO: Remove sensitive information from player struct -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct Player { pub id: u32, pub time_created: NaiveDateTime, @@ -20,13 +20,13 @@ pub struct Player { pub phone_number_verified: bool, } +impl From for Player { + fn from(player_dto: PlayerForCreationDto) -> Self { + Player { id: Default::default(), time_created: Utc::now().naive_utc(), last_updated: Utc::now().naive_utc(), name: player_dto.name.clone(), birth_date: player_dto.birth_date.clone(), country: player_dto.country.clone(), city: player_dto.city.clone(), identification_number: None, bio: None, profile_picture_url: None, id_verified: false, phone_number_verified: false } + } +} + impl Player{ - pub fn new() -> Player { - Player { id: 0, time_created: Utc::now().naive_utc(), last_updated: Utc::now().naive_utc(), name: "".to_string(), birth_date: Utc::now().date_naive(), country: "".to_string(), city: "".to_string(), identification_number: None, bio: None, profile_picture_url: None, id_verified: false, phone_number_verified: false } - } - pub fn new_from_creation_dto(player_dto: &PlayerForCreationDto, id: &u32) -> Player { - Player { id: *id, time_created: Utc::now().naive_utc(), last_updated: Utc::now().naive_utc(), name: player_dto.name.clone(), birth_date: player_dto.birth_date.clone(), country: player_dto.country.clone(), city: player_dto.city.clone(), identification_number: None, bio: None, profile_picture_url: None, id_verified: false, phone_number_verified: false } - } pub fn clear_all_sensitive_fields(mut self) -> Self { self.birth_date = Utc::now().date_naive(); self.city = "".to_string(); diff --git a/src/domain/sport.rs b/src/domain/sport.rs index 72746f9..c1938c0 100644 --- a/src/domain/sport.rs +++ b/src/domain/sport.rs @@ -1,13 +1,8 @@ use serde::{Serialize, Deserialize}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct Sport{ pub id: u32, pub name: String, pub category_id: u32 -} -impl Sport{ - pub fn new() -> Sport{ - Sport { id: 0, name: "".to_string(), category_id: 0 } - } } \ No newline at end of file diff --git a/src/domain/sport_category.rs b/src/domain/sport_category.rs index 894c664..357c643 100644 --- a/src/domain/sport_category.rs +++ b/src/domain/sport_category.rs @@ -1,12 +1,7 @@ use serde::{Serialize, Deserialize}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct SportCategory { pub id: u32, pub name: String, -} -impl SportCategory { - pub fn new() -> SportCategory { - SportCategory { id: 0, name: "".to_string() } - } } \ No newline at end of file diff --git a/src/domain/trust.rs b/src/domain/trust.rs index 642afcd..61d8c7c 100644 --- a/src/domain/trust.rs +++ b/src/domain/trust.rs @@ -4,7 +4,7 @@ use serde::{Serialize, Deserialize}; use crate::dto::trust::TrustRequestDto; -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct Trust { pub id: u32, /// The player who is trusting (sending the trust request) @@ -14,11 +14,9 @@ pub struct Trust { pub time_created: NaiveDateTime, pub last_updated: NaiveDateTime } -impl Trust { - pub fn new_empty() -> Trust { - Trust { id: 0, truster_id: 0, trustee_id: 0, time_created: Utc::now().naive_utc(), last_updated: Utc::now().naive_utc() } - } - pub fn new_from_join_request(trust_dto: &TrustRequestDto) -> Trust { + +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().naive_utc(), last_updated: Utc::now().naive_utc() } } } \ No newline at end of file diff --git a/src/dto/league.rs b/src/dto/league.rs index 70941cc..65e8ed6 100644 --- a/src/dto/league.rs +++ b/src/dto/league.rs @@ -1,9 +1,9 @@ use chrono::NaiveDateTime; -use serde::{Deserialize}; +use serde::{Deserialize, Serialize}; use rust_decimal::Decimal; use crate::domain::league::LeagueVisibility; -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct LeagueForCreationDto{ #[serde(rename = "userId")] pub user_id: u32, diff --git a/src/dto/league_player.rs b/src/dto/league_player.rs index 6a10ee8..144b1a4 100644 --- a/src/dto/league_player.rs +++ b/src/dto/league_player.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct JoinRequest { #[serde(rename = "leagueId")] pub league_id: u32, diff --git a/src/dto/player.rs b/src/dto/player.rs index 110028c..bdca2a7 100644 --- a/src/dto/player.rs +++ b/src/dto/player.rs @@ -4,7 +4,7 @@ use serde::{Serialize, Deserialize}; use crate::{domain::player::Player, APP_NAME}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct PlayerForCreationDto { #[serde(rename = "phoneNumber")] pub phone_number: String, @@ -15,7 +15,7 @@ pub struct PlayerForCreationDto { pub country: String, pub city: String, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct PlayerForUpdateDto { pub name: Option, #[serde(rename = "birthDate")] @@ -39,7 +39,7 @@ impl PlayerForCreationDto { } } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct PlayerProfileDto { pub name: String, pub country: String, diff --git a/src/dto/trust.rs b/src/dto/trust.rs index d89ef1e..ccd196d 100644 --- a/src/dto/trust.rs +++ b/src/dto/trust.rs @@ -2,7 +2,7 @@ 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)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct TrustRequestDto { #[serde(rename = "authToken")] pub auth_token: String,