Files
gamenight/gamenight-cli/src/flows/own.rs

57 lines
1.9 KiB
Rust

use std::fmt::Display;
use super::{Flow, FlowError, FlowOutcome, FlowResult, GamenightState};
use crate::flows::flow_helpers::LocationSelectData;
use async_trait::async_trait;
use gamenight_api_client_rs::apis::default_api::locations_get;
use gamenight_api_client_rs::models::OwnGameRequestBody;
use gamenight_api_client_rs::apis::default_api::own_post;
use inquire::{Confirm, Select};
use uuid::Uuid;
#[derive(Clone)]
pub struct Own {
game_id: Uuid
}
impl Own {
pub fn new(game_id: Uuid) -> Self {
Self {
game_id
}
}
}
#[async_trait]
impl<'a> Flow<'a> for Own {
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
let mut own_game_request = OwnGameRequestBody {
game_id: self.game_id.to_string(),
location_id: None
};
if let Some(owned) = Confirm::new("Do you own this game?").prompt_skippable()? {
if owned {
if let Some(willing_to_travel) = Confirm::new("Are you willing to travel with this game?").prompt_skippable()? {
if !willing_to_travel {
let locations = locations_get(&state.api_configuration).await?.iter().map(|x| { x.try_into() }).collect::<Result<Vec<LocationSelectData>, FlowError>>()?;
if let Some(location) = Select::new("What location can this game be played?", locations).prompt_skippable()? {
own_game_request.location_id = Some(location.id.to_string());
}
}
}
let _ = own_post(&state.api_configuration, Some(own_game_request)).await?;
}
}
clear_screen::clear();
Ok((FlowOutcome::Successful, state))
}
}
impl Display for Own {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Own")
}
}