Added player editing and player creating.
This commit is contained in:
parent
9c083f632d
commit
dc3ae517a8
@ -3,7 +3,7 @@ use league_types::{domain::{sport::Sport, player::Player, league::League, place:
|
|||||||
|
|
||||||
use reqwest::Method;
|
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/";
|
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)
|
Ok(user_token)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn edit_player_profile(player: PlayerForUpdateDto) -> Result<Player, RustError> {
|
pub fn edit_player_profile(player: PlayerForUpdate) -> Result<dtos::player_dtos::Player, RustError> {
|
||||||
perform_request::<PlayerForUpdateDto, Player>(BASE_URL.to_string(), Method::PUT, "league/player".into(), Some(player), 200, vec![])
|
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> {
|
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![]) {
|
let token: UserToken = perform_request::<UserForLoginDto, Token>(BASE_URL.to_string(), Method::POST, "league/player/login".into(), Some(user), 200, vec![])?.into();
|
||||||
Ok(token) => Ok(token.into()),
|
store(&token, "token".into())?;
|
||||||
Err(error) => Err(error),
|
Ok(token)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_player_profile(player_id: u32) -> Result<PlayerProfileDto, RustError> {
|
pub fn get_player_profile(player_id: u32) -> Result<PlayerProfileDto, RustError> {
|
||||||
|
@ -25,11 +25,10 @@ pub use types::dtos::player_dtos::*;
|
|||||||
pub use types::domain::country::*;
|
pub use types::domain::country::*;
|
||||||
pub use utils::world_parsing::*;
|
pub use utils::world_parsing::*;
|
||||||
|
|
||||||
pub fn get_me() -> Result<UserForAuthenticationDto, RustError> {
|
pub fn get_me() -> Result<UserToken, RustError> {
|
||||||
storage::read("user".into())
|
storage::read("token".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! unwrap_rust_error {
|
macro_rules! unwrap_rust_error {
|
||||||
($e:expr) => {
|
($e:expr) => {
|
||||||
|
@ -74,9 +74,26 @@ dictionary UserForLoginDto {
|
|||||||
dictionary UserToken {
|
dictionary UserToken {
|
||||||
string auth_token;
|
string auth_token;
|
||||||
string refresh_token;
|
string refresh_token;
|
||||||
|
i32 user_id;
|
||||||
i64? time_created;
|
i64? time_created;
|
||||||
i64? last_updated;
|
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 {
|
dictionary PlayerForCreation {
|
||||||
string phone_number;
|
string phone_number;
|
||||||
string password;
|
string password;
|
||||||
@ -85,6 +102,17 @@ dictionary PlayerForCreation {
|
|||||||
string country;
|
string country;
|
||||||
string city;
|
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 {
|
dictionary Country {
|
||||||
i32 id;
|
i32 id;
|
||||||
string name;
|
string name;
|
||||||
@ -122,19 +150,23 @@ interface WebsocketCaller {
|
|||||||
|
|
||||||
namespace network {
|
namespace network {
|
||||||
[Throws=RustError]
|
[Throws=RustError]
|
||||||
UserForAuthenticationDto get_me();
|
UserToken get_me();
|
||||||
[Throws=RustError]
|
[Throws=RustError]
|
||||||
sequence<Sport> get_all_sports();
|
sequence<Sport> get_all_sports();
|
||||||
void init_storage(string path);
|
void init_storage(string path);
|
||||||
|
|
||||||
[Throws=RustError]
|
[Throws=RustError]
|
||||||
ChatRoom create_new_chat_room(UserForAuthenticationDto user, ChatRoomParticipants participants, string title);
|
ChatRoom create_new_chat_room(UserForAuthenticationDto user, ChatRoomParticipants participants, string title);
|
||||||
[Throws=RustError]
|
[Throws=RustError]
|
||||||
void authenticate_user_token(UserForAuthenticationDto user);
|
void authenticate_user_token(UserForAuthenticationDto user);
|
||||||
|
|
||||||
[Throws=RustError]
|
[Throws=RustError]
|
||||||
UserToken login_player(UserForLoginDto user);
|
UserToken login_player(UserForLoginDto user);
|
||||||
[Throws=RustError]
|
[Throws=RustError]
|
||||||
UserToken create_player_profile(PlayerForCreation user);
|
UserToken create_player_profile(PlayerForCreation user);
|
||||||
[Throws=RustError]
|
[Throws=RustError]
|
||||||
|
Player edit_player_profile(PlayerForUpdate player);
|
||||||
|
[Throws=RustError]
|
||||||
sequence<Country> parse_countries_and_cities();
|
sequence<Country> parse_countries_and_cities();
|
||||||
|
|
||||||
Country? find_country(sequence<Country> country_list, string country_name);
|
Country? find_country(sequence<Country> country_list, string country_name);
|
||||||
|
@ -1,6 +1,38 @@
|
|||||||
use chrono::{Utc, TimeZone};
|
use chrono::{Utc, TimeZone, NaiveTime};
|
||||||
use league_types::dto::player::PlayerForCreationDto;
|
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 struct PlayerForCreation {
|
||||||
pub phone_number: String,
|
pub phone_number: String,
|
||||||
@ -21,4 +53,35 @@ impl From<PlayerForCreation> for PlayerForCreationDto {
|
|||||||
city: value.city,
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -7,6 +7,7 @@ use serde::{Serialize, Deserialize};
|
|||||||
pub struct UserToken {
|
pub struct UserToken {
|
||||||
pub auth_token: String,
|
pub auth_token: String,
|
||||||
pub refresh_token: String,
|
pub refresh_token: String,
|
||||||
|
pub user_id: i32,
|
||||||
pub time_created: Option<i64>,
|
pub time_created: Option<i64>,
|
||||||
pub last_updated: Option<i64>,
|
pub last_updated: Option<i64>,
|
||||||
}
|
}
|
||||||
@ -16,6 +17,7 @@ impl From<Token> for UserToken {
|
|||||||
Self {
|
Self {
|
||||||
auth_token: value.auth_token,
|
auth_token: value.auth_token,
|
||||||
refresh_token: value.refresh_token,
|
refresh_token: value.refresh_token,
|
||||||
|
user_id: value.user_id,
|
||||||
time_created: match value.time_created {
|
time_created: match value.time_created {
|
||||||
Some(time_created) => Some(time_created.timestamp_millis()),
|
Some(time_created) => Some(time_created.timestamp_millis()),
|
||||||
None => None,
|
None => None,
|
||||||
|
@ -14,7 +14,8 @@ pub fn init_storage(path: String) {
|
|||||||
*write = path;
|
*write = path;
|
||||||
drop(write);
|
drop(write);
|
||||||
|
|
||||||
//delete("user".into());
|
//delete("token".into());
|
||||||
|
//delete("player".into());
|
||||||
// TESTING PURPOSES
|
// TESTING PURPOSES
|
||||||
//TODO: Remove this line in prod
|
//TODO: Remove this line in prod
|
||||||
//store(UserForAuthenticationDto{ app: "".into(), id: "3".into(), token: "/2uuNJG3Z2bT9VVd64xBeACPxg64GicloiXtG9uO87as5q5g46TtNu0sAVTACyR8R8uMVXoTBlBP4Q3JhcGB2Q==".to_string() }, "user".into()).unwrap();
|
//store(UserForAuthenticationDto{ app: "".into(), id: "3".into(), token: "/2uuNJG3Z2bT9VVd64xBeACPxg64GicloiXtG9uO87as5q5g46TtNu0sAVTACyR8R8uMVXoTBlBP4Q3JhcGB2Q==".to_string() }, "user".into()).unwrap();
|
||||||
|
@ -2,8 +2,6 @@ use err::MessageResource;
|
|||||||
|
|
||||||
use crate::{types::domain::country::Country, RustError, City};
|
use crate::{types::domain::country::Country, RustError, City};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn parse_countries_and_cities() -> Result<Vec<Country>, RustError> {
|
pub fn parse_countries_and_cities() -> Result<Vec<Country>, RustError> {
|
||||||
let whole_json = include_str!("../../countries+cities.json");
|
let whole_json = include_str!("../../countries+cities.json");
|
||||||
match serde_json::from_str::<Vec<Country>>(whole_json) {
|
match serde_json::from_str::<Vec<Country>>(whole_json) {
|
||||||
|
Loading…
Reference in New Issue
Block a user