diff --git a/src/client/league/mod.rs b/src/client/league/mod.rs index 06ca943..8cbe228 100644 --- a/src/client/league/mod.rs +++ b/src/client/league/mod.rs @@ -3,7 +3,7 @@ use league_types::{domain::{sport::Sport, player::Player, league::League, place: use reqwest::Method; -use crate::{client::base::perform_request_without_client_sync as perform_request, RustError, types::dtos::user_token::{UserToken}, PlayerForCreation, store}; +use crate::{client::base::perform_request_without_client_sync as perform_request, RustError, types::dtos::{user_token::{UserToken}, self}, PlayerForCreation, store, PlayerForUpdate}; const BASE_URL: &str = "http://backend.blancoinfante.com/"; @@ -24,15 +24,16 @@ pub fn create_player_profile(player: PlayerForCreation) -> Result Result { - perform_request::(BASE_URL.to_string(), Method::PUT, "league/player".into(), Some(player), 200, vec![]) +pub fn edit_player_profile(player: PlayerForUpdate) -> Result { + let persisted_player = perform_request::(BASE_URL.to_string(), Method::PUT, "league/player".into(), Some(player.into()), 200, vec![])?; + store(&persisted_player, "player".into())?; //TODO: Move the path into a constant file + Ok(persisted_player.into()) } pub fn login_player(user: UserForLoginDto) -> Result { - match perform_request::(BASE_URL.to_string(), Method::POST, "league/player/login".into(), Some(user), 200, vec![]) { - Ok(token) => Ok(token.into()), - Err(error) => Err(error), - } + let token: UserToken = perform_request::(BASE_URL.to_string(), Method::POST, "league/player/login".into(), Some(user), 200, vec![])?.into(); + store(&token, "token".into())?; + Ok(token) } pub fn get_player_profile(player_id: u32) -> Result { diff --git a/src/lib.rs b/src/lib.rs index 7cfa21f..6848082 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,11 +25,10 @@ pub use types::dtos::player_dtos::*; pub use types::domain::country::*; pub use utils::world_parsing::*; -pub fn get_me() -> Result { - storage::read("user".into()) +pub fn get_me() -> Result { + storage::read("token".into()) } - #[macro_export] macro_rules! unwrap_rust_error { ($e:expr) => { diff --git a/src/network.udl b/src/network.udl index f987628..8d913a1 100644 --- a/src/network.udl +++ b/src/network.udl @@ -74,9 +74,26 @@ dictionary UserForLoginDto { dictionary UserToken { string auth_token; string refresh_token; + i32 user_id; i64? time_created; i64? last_updated; }; + + +dictionary Player { + u32 id; + i64 time_created; + i64 last_updated; + string name; + i64 birth_date; + string country; + string city; + string? identification_number; + string? bio; + string? profile_picture_url; + boolean id_verified; + boolean phone_number_verified; +}; dictionary PlayerForCreation { string phone_number; string password; @@ -85,6 +102,17 @@ dictionary PlayerForCreation { string country; string city; }; +dictionary PlayerForUpdate { + string? name; + i64? birth_date; + string? country; + string? city; + string? identification_number; + string? bio; + string? profile_picture_url; + u32 user_id; + string auth_token; +}; dictionary Country { i32 id; string name; @@ -122,19 +150,23 @@ interface WebsocketCaller { namespace network { [Throws=RustError] - UserForAuthenticationDto get_me(); + UserToken get_me(); [Throws=RustError] sequence get_all_sports(); void init_storage(string path); + [Throws=RustError] ChatRoom create_new_chat_room(UserForAuthenticationDto user, ChatRoomParticipants participants, string title); [Throws=RustError] void authenticate_user_token(UserForAuthenticationDto user); + [Throws=RustError] UserToken login_player(UserForLoginDto user); [Throws=RustError] UserToken create_player_profile(PlayerForCreation user); [Throws=RustError] + Player edit_player_profile(PlayerForUpdate player); + [Throws=RustError] sequence parse_countries_and_cities(); Country? find_country(sequence country_list, string country_name); diff --git a/src/types/dtos/player_dtos.rs b/src/types/dtos/player_dtos.rs index a130cd7..d954298 100644 --- a/src/types/dtos/player_dtos.rs +++ b/src/types/dtos/player_dtos.rs @@ -1,6 +1,38 @@ -use chrono::{Utc, TimeZone}; -use league_types::dto::player::PlayerForCreationDto; +use chrono::{Utc, TimeZone, NaiveTime}; +use league_types::dto::player::{PlayerForCreationDto, PlayerForUpdateDto}; +pub struct Player { + pub id: u32, + pub time_created: i64, + pub last_updated: i64, + pub name: String, + pub birth_date: i64, + pub country: String, + pub city: String, + pub identification_number: Option, + pub bio: Option, + pub profile_picture_url: Option, + pub id_verified: bool, + pub phone_number_verified: bool, +} +impl From for Player { + fn from(value: league_types::domain::player::Player) -> Self { + Self { + id: value.id, + time_created: value.time_created.timestamp_millis(), + last_updated: value.last_updated.timestamp_millis(), + name: value.name, + birth_date: value.birth_date.and_time(NaiveTime::default()).timestamp_millis(), + country: value.country, + city: value.city, + identification_number: value.identification_number, + bio: value.bio, + profile_picture_url: value.profile_picture_url, + id_verified: value.id_verified, + phone_number_verified: value.phone_number_verified, + } + } +} pub struct PlayerForCreation { pub phone_number: String, @@ -21,4 +53,35 @@ impl From for PlayerForCreationDto { city: value.city, } } +} + +pub struct PlayerForUpdate { + pub name: Option, + pub birth_date: Option, + pub country: Option, + pub city: Option, + pub identification_number: Option, + pub bio: Option, + pub profile_picture_url: Option, + pub user_id: u32, + pub auth_token: String, +} + +impl From for PlayerForUpdateDto { + fn from(value: PlayerForUpdate) -> Self { + Self { + name: value.name, + birth_date: match value.birth_date { + Some(birth_date) => Some(Utc.timestamp_millis_opt(birth_date).unwrap().date_naive()), + None => None, + }, + country: value.country, + city: value.city, + identification_number: value.identification_number, + bio: value.bio, + profile_picture_url: value.profile_picture_url, + user_id: value.user_id, + auth_token: value.auth_token, + } + } } \ No newline at end of file diff --git a/src/types/dtos/user_token.rs b/src/types/dtos/user_token.rs index c1c4366..4a0b9b1 100644 --- a/src/types/dtos/user_token.rs +++ b/src/types/dtos/user_token.rs @@ -7,6 +7,7 @@ use serde::{Serialize, Deserialize}; pub struct UserToken { pub auth_token: String, pub refresh_token: String, + pub user_id: i32, pub time_created: Option, pub last_updated: Option, } @@ -16,6 +17,7 @@ impl From for UserToken { Self { auth_token: value.auth_token, refresh_token: value.refresh_token, + user_id: value.user_id, time_created: match value.time_created { Some(time_created) => Some(time_created.timestamp_millis()), None => None, diff --git a/src/utils/storage.rs b/src/utils/storage.rs index 5ef6f9a..615cc06 100644 --- a/src/utils/storage.rs +++ b/src/utils/storage.rs @@ -14,7 +14,8 @@ pub fn init_storage(path: String) { *write = path; drop(write); - //delete("user".into()); + //delete("token".into()); + //delete("player".into()); // TESTING PURPOSES //TODO: Remove this line in prod //store(UserForAuthenticationDto{ app: "".into(), id: "3".into(), token: "/2uuNJG3Z2bT9VVd64xBeACPxg64GicloiXtG9uO87as5q5g46TtNu0sAVTACyR8R8uMVXoTBlBP4Q3JhcGB2Q==".to_string() }, "user".into()).unwrap(); diff --git a/src/utils/world_parsing.rs b/src/utils/world_parsing.rs index 1ea544e..9789834 100644 --- a/src/utils/world_parsing.rs +++ b/src/utils/world_parsing.rs @@ -2,8 +2,6 @@ use err::MessageResource; use crate::{types::domain::country::Country, RustError, City}; - - pub fn parse_countries_and_cities() -> Result, RustError> { let whole_json = include_str!("../../countries+cities.json"); match serde_json::from_str::>(whole_json) {