Added player editing and player creating.

This commit is contained in:
Franklin 2023-03-04 22:21:30 -04:00
parent 9c083f632d
commit dc3ae517a8
7 changed files with 112 additions and 16 deletions

View File

@ -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<UserToken, Rus
Ok(user_token)
}
pub fn edit_player_profile(player: PlayerForUpdateDto) -> Result<Player, RustError> {
perform_request::<PlayerForUpdateDto, Player>(BASE_URL.to_string(), Method::PUT, "league/player".into(), Some(player), 200, vec![])
pub fn edit_player_profile(player: PlayerForUpdate) -> Result<dtos::player_dtos::Player, RustError> {
let persisted_player = perform_request::<PlayerForUpdateDto, Player>(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<UserToken, RustError> {
match perform_request::<UserForLoginDto, Token>(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::<UserForLoginDto, Token>(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<PlayerProfileDto, RustError> {

View File

@ -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<UserForAuthenticationDto, RustError> {
storage::read("user".into())
pub fn get_me() -> Result<UserToken, RustError> {
storage::read("token".into())
}
#[macro_export]
macro_rules! unwrap_rust_error {
($e:expr) => {

View File

@ -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<Sport> 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<Country> parse_countries_and_cities();
Country? find_country(sequence<Country> country_list, string country_name);

View File

@ -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<String>,
pub bio: Option<String>,
pub profile_picture_url: Option<String>,
pub id_verified: bool,
pub phone_number_verified: bool,
}
impl From<league_types::domain::player::Player> 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<PlayerForCreation> for PlayerForCreationDto {
city: value.city,
}
}
}
pub struct PlayerForUpdate {
pub name: Option<String>,
pub birth_date: Option<i64>,
pub country: Option<String>,
pub city: Option<String>,
pub identification_number: Option<String>,
pub bio: Option<String>,
pub profile_picture_url: Option<String>,
pub user_id: u32,
pub auth_token: String,
}
impl From<PlayerForUpdate> 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,
}
}
}

View File

@ -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<i64>,
pub last_updated: Option<i64>,
}
@ -16,6 +17,7 @@ impl From<Token> 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,

View File

@ -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();

View File

@ -2,8 +2,6 @@ use err::MessageResource;
use crate::{types::domain::country::Country, RustError, City};
pub fn parse_countries_and_cities() -> Result<Vec<Country>, RustError> {
let whole_json = include_str!("../../countries+cities.json");
match serde_json::from_str::<Vec<Country>>(whole_json) {