Added countries and cities

This commit is contained in:
Franklin 2023-03-03 18:01:51 -04:00
parent 36438e1e3b
commit 9c083f632d
14 changed files with 917099 additions and 12 deletions

916930
countries+cities.json Normal file

File diff suppressed because it is too large Load Diff

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};
use crate::{client::base::perform_request_without_client_sync as perform_request, RustError, types::dtos::user_token::{UserToken}, PlayerForCreation, store};
const BASE_URL: &str = "http://backend.blancoinfante.com/";
@ -18,16 +18,21 @@ pub fn get_all_sports() -> Result<Vec<Sport>, RustError> {
// #############
// PLAYER ROUTES
// #############
pub fn create_player_profile(player: PlayerForCreationDto) -> Result<Token, RustError> {
perform_request::<PlayerForCreationDto, Token>(BASE_URL.to_string(), Method::POST, "league/player".into(), Some(player), 200, vec![])
pub fn create_player_profile(player: PlayerForCreation) -> Result<UserToken, RustError> {
let user_token: UserToken = perform_request::<PlayerForCreationDto, Token>(BASE_URL.to_string(), Method::POST, "league/player".into(), Some(player.into()), 200, vec![])?.into();
store(&user_token, "token".into())?; //TODO: Move the path into a constant file
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 login(user: UserForLoginDto) -> Result<Token, RustError> {
perform_request::<UserForLoginDto, Token>(BASE_URL.to_string(), Method::POST, "league/player/login".into(), Some(user), 200, vec![])
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),
}
}
pub fn get_player_profile(player_id: u32) -> Result<PlayerProfileDto, RustError> {

View File

@ -8,7 +8,7 @@ use tokio::runtime::Runtime;
use crate::{RustError};
//const BASE_URL: &str = "http://backend.blancoinfante.com/";
pub fn authenticate_user(user: UserForAuthenticationDto) -> Result<(), RustError> {
pub fn authenticate_user_token(user: UserForAuthenticationDto) -> Result<(), RustError> {
let rt = Runtime::new().unwrap();
let client = Client::new();
match rt.block_on(

View File

@ -9,7 +9,8 @@ use utils::storage;
pub use chat_types::client_types::chat_room::ChatRoom;
pub use chat_types::dto::chat::ChatRoomParticipants;
pub use dev_dtos::dtos::user::user_dtos::{UserForAuthenticationDto};
pub use dev_dtos::dtos::user::user_dtos::{UserForAuthenticationDto, UserForLoginDto};
pub use dev_dtos::domain::user::credential_type::CredentialType;
pub use utils::storage::*;
pub use callbacks::chat::*;
@ -19,6 +20,10 @@ pub use client::chat::http::*;
pub use client::league::*;
pub use client::user::*;
pub use chat_types::client_types::chat_message::*;
pub use types::dtos::user_token::UserToken;
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())

View File

@ -61,6 +61,41 @@ enum ClientError {
"Two",
"Three",
};
enum CredentialType {
"PhoneNumber",
"Email",
};
dictionary UserForLoginDto {
string app = "";
string credential;
CredentialType credential_type;
string password;
};
dictionary UserToken {
string auth_token;
string refresh_token;
i64? time_created;
i64? last_updated;
};
dictionary PlayerForCreation {
string phone_number;
string password;
string name;
i64 birth_date;
string country;
string city;
};
dictionary Country {
i32 id;
string name;
string emoji;
string phone_code;
sequence<City> cities;
};
dictionary City {
i32 id;
string name;
};
callback interface WebsocketFfi {
[Throws=ForeignError]
@ -94,5 +129,15 @@ namespace network {
[Throws=RustError]
ChatRoom create_new_chat_room(UserForAuthenticationDto user, ChatRoomParticipants participants, string title);
[Throws=RustError]
void authenticate_user(UserForAuthenticationDto user);
void authenticate_user_token(UserForAuthenticationDto user);
[Throws=RustError]
UserToken login_player(UserForLoginDto user);
[Throws=RustError]
UserToken create_player_profile(PlayerForCreation user);
[Throws=RustError]
sequence<Country> parse_countries_and_cities();
Country? find_country(sequence<Country> country_list, string country_name);
City? find_city(sequence<City> city_list, string city_name);
};

View File

@ -0,0 +1,21 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct Country {
pub id: i32,
pub name: String,
pub emoji: String,
pub phone_code: String,
pub cities: Vec<City>
}
impl Country {
pub fn new() -> Self {
Self::default()
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct City {
pub id: i32,
pub name: String,
}

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

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

2
src/types/dtos/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod user_token;
pub mod player_dtos;

View File

@ -0,0 +1,24 @@
use chrono::{Utc, TimeZone};
use league_types::dto::player::PlayerForCreationDto;
pub struct PlayerForCreation {
pub phone_number: String,
pub password: String,
pub name: String,
pub birth_date: i64,
pub country: String,
pub city: String,
}
impl From<PlayerForCreation> for PlayerForCreationDto {
fn from(value: PlayerForCreation) -> Self {
Self {
phone_number: value.phone_number,
password: value.password,
name: value.name,
birth_date: Utc.timestamp_millis_opt(value.birth_date).unwrap().date_naive(),
country: value.country,
city: value.city,
}
}
}

View File

@ -0,0 +1,29 @@
use dev_dtos::domain::user::token::Token;
use serde::{Serialize, Deserialize};
/// The client only cares about the tokens and their date
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct UserToken {
pub auth_token: String,
pub refresh_token: String,
pub time_created: Option<i64>,
pub last_updated: Option<i64>,
}
impl From<Token> for UserToken {
fn from(value: Token) -> Self {
Self {
auth_token: value.auth_token,
refresh_token: value.refresh_token,
time_created: match value.time_created {
Some(time_created) => Some(time_created.timestamp_millis()),
None => None,
},
last_updated: match value.last_updated {
Some(last_updated) => Some(last_updated.timestamp_millis()),
None => None,
}
}
}
}

View File

@ -1 +1,3 @@
pub mod error;
pub mod error;
pub mod dtos;
pub mod domain;

View File

@ -1 +1,2 @@
pub mod storage;
pub mod storage;
pub mod world_parsing;

View File

@ -1,5 +1,6 @@
use std::{sync::RwLock, fs};
#[allow(unused)]
use dev_dtos::dtos::user::user_dtos::UserForAuthenticationDto;
use serde::{Serialize, de::DeserializeOwned};
@ -13,6 +14,7 @@ pub fn init_storage(path: String) {
*write = path;
drop(write);
//delete("user".into());
// TESTING PURPOSES
//TODO: Remove this line in prod
//store(UserForAuthenticationDto{ app: "".into(), id: "3".into(), token: "/2uuNJG3Z2bT9VVd64xBeACPxg64GicloiXtG9uO87as5q5g46TtNu0sAVTACyR8R8uMVXoTBlBP4Q3JhcGB2Q==".to_string() }, "user".into()).unwrap();
@ -20,8 +22,8 @@ pub fn init_storage(path: String) {
// These functions won't be exposed to the clients, just used by the functions they call internally
pub fn store<T: Serialize>(ty: T, path: String) -> Result<(), RustError>{
let serialized_ty = match bincode::serialize(&ty) {
pub fn store<T: Serialize>(ty: &T, path: String) -> Result<(), RustError>{
let serialized_ty = match bincode::serialize(ty) {
Ok(bytes) => bytes,
Err(e) => return Err(RustError::SerdeError { error: MessageResource { key: "STORAGE.OBJECT_SERIALIZATION".into(), message: Some(e.to_string()) }, serde_error_str: None }),
};

View File

@ -0,0 +1,20 @@
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) {
Ok(countries) => Ok(countries.into_iter().filter(|country| country.cities.len() > 0).collect()),
Err(error) => Err(RustError::SerdeError { error: MessageResource::new_from_str("Failed deserializing countries.json file into a country list.").into(), serde_error_str: Some(error.to_string()) }),
}
}
pub fn find_country(country_list: Vec<Country>, country_name: String) -> Option<Country> {
country_list.into_iter().find(|country| country.name == country_name)
}
pub fn find_city(city_list: Vec<City>, city_name: String) -> Option<City> {
city_list.into_iter().find(|city| city.name == city_name)
}