65 lines
1.8 KiB
Rust

use gamenight_api_client_rs::{apis::default_api::{game_get, owned_games_get}, models::{GameId, UserId}};
use inquire::Select;
use uuid::Uuid;
use crate::{domain::game::Game, flows::{disown::Disown, exit::Exit, own::Own, rename_game::RenameGame}};
use super::*;
#[derive(Clone)]
pub struct ViewGame {
game: Game
}
impl ViewGame {
pub fn new(game: Game) -> Self {
Self {
game
}
}
}
#[async_trait]
impl<'a> Flow<'a> for ViewGame {
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
let game_id = GameId{ game_id: self.game.id.to_string() };
let game: Game = game_get(&state.api_configuration, Some(game_id)).await?.into();
println!("{}", game);
let my_uid = state.get_user_id()?;
let request = UserId{ user_id: my_uid.to_string() };
let owned_games: Vec<Uuid> = owned_games_get(&state.api_configuration, Some(request)).await?
.iter().map(|x| -> Result<Uuid, FlowError> {
Ok(Uuid::parse_str(x)?)
}).collect::<Result<Vec<Uuid>, FlowError>>()?;
let own_or_disown: Box<dyn Flow<'a> + Send> =
if owned_games.into_iter().any(|x| x == game.id) {
Box::new(Disown::new(self.game.id))
}
else {
Box::new(Own::new(self.game.id))
};
let options: Vec<Box<dyn Flow<'a> + Send>> = vec![
own_or_disown,
Box::new(RenameGame::new(game.clone())),
Box::new(Exit::new())
];
let choice = Select::new("What do you want to do:", options)
.prompt_skippable()?;
self.continue_choice(state, &choice).await
}
}
impl Display for ViewGame {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.game.name)
}
}