forked from Roflin/gamenight
Adds Adding of Games to the system.
This commit is contained in:
44
gamenight-cli/src/flows/add_game.rs
Normal file
44
gamenight-cli/src/flows/add_game.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use gamenight_api_client_rs::{apis::default_api::game_post, models::{AddGameRequestBody, Game}};
|
||||
use inquire::{Confirm, Text};
|
||||
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AddGame {
|
||||
}
|
||||
|
||||
impl AddGame {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> Flow<'a> for AddGame {
|
||||
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
|
||||
if let Some(name) = Text::new("What is the game you want to add").prompt_skippable()? {
|
||||
let add_game_request = AddGameRequestBody {
|
||||
name
|
||||
};
|
||||
game_post(&state.api_configuration, Some(add_game_request)).await?;
|
||||
|
||||
if let Some(owned) = Confirm::new("Do you own this game?").prompt_skippable()? {
|
||||
if owned {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok((FlowOutcome::Cancelled, state))
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for AddGame {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Add Game")
|
||||
}
|
||||
}
|
||||
31
gamenight-cli/src/flows/edit_game.rs
Normal file
31
gamenight-cli/src/flows/edit_game.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
use gamenight_api_client_rs::models::Game;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EditGame {
|
||||
game: Game
|
||||
}
|
||||
|
||||
impl EditGame {
|
||||
pub fn new(game: Game) -> Self {
|
||||
Self {
|
||||
game
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> Flow<'a> for EditGame {
|
||||
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for EditGame {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
use inquire::{ui::RenderConfig, Select};
|
||||
|
||||
use crate::flows::{add_gamenight::AddGamenight, exit::Exit, list_gamenights::ListGamenights};
|
||||
use crate::flows::{add_gamenight::AddGamenight, exit::Exit, games::Games, list_gamenights::ListGamenights};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -26,6 +26,7 @@ impl<'a> Flow<'a> for GamenightMenu {
|
||||
let flows: Vec<Box<dyn Flow + Send>> = vec![
|
||||
Box::new(ListGamenights::new()),
|
||||
Box::new(AddGamenight::new()),
|
||||
Box::new(Games::new()),
|
||||
Box::new(Exit::new())
|
||||
];
|
||||
|
||||
|
||||
47
gamenight-cli/src/flows/games.rs
Normal file
47
gamenight-cli/src/flows/games.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use inquire::{ui::RenderConfig, Select};
|
||||
|
||||
use crate::flows::{add_game::AddGame, exit::Exit, list_games::ListGames};
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Games {
|
||||
}
|
||||
|
||||
impl Games {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> Flow<'a> for Games {
|
||||
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
|
||||
let flows: Vec<Box<dyn Flow + Send>> = vec![
|
||||
Box::new(ListGames::new()),
|
||||
Box::new(AddGame::new()),
|
||||
Box::new(Exit::new())
|
||||
];
|
||||
|
||||
let choice = Select::new("What would you like to do?", flows)
|
||||
.with_help_message("Select the action you want to take or quit the program")
|
||||
.with_render_config(RenderConfig {
|
||||
option_index_prefix: inquire::ui::IndexPrefix::Simple,
|
||||
..Default::default()
|
||||
})
|
||||
.prompt_skippable()?;
|
||||
|
||||
clear_screen::clear();
|
||||
handle_choice_option(&choice, self, state).await
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Games {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Games")
|
||||
}
|
||||
}
|
||||
51
gamenight-cli/src/flows/list_games.rs
Normal file
51
gamenight-cli/src/flows/list_games.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use gamenight_api_client_rs::apis::default_api::games_get;
|
||||
use inquire::{ui::RenderConfig, Select};
|
||||
|
||||
use crate::flows::{edit_game::EditGame, exit::Exit};
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ListGames {
|
||||
}
|
||||
|
||||
impl ListGames {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> Flow<'a> for ListGames {
|
||||
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
|
||||
let games = games_get(&state.api_configuration).await?;
|
||||
|
||||
let mut flows = games.into_iter().map(|game| -> Box<dyn Flow + Send> {
|
||||
Box::new(EditGame::new(game))
|
||||
}).collect::<Vec::<Box::<dyn Flow + Send>>>();
|
||||
|
||||
flows.push(Box::new(Exit::new()));
|
||||
|
||||
let choice = Select::new("What would you like to do?", flows)
|
||||
.with_help_message("Select the action you want to take or quit the program")
|
||||
.with_render_config(RenderConfig {
|
||||
option_index_prefix: inquire::ui::IndexPrefix::Simple,
|
||||
..Default::default()
|
||||
})
|
||||
.prompt_skippable()?;
|
||||
|
||||
clear_screen::clear();
|
||||
handle_choice_option(&choice, self, state).await
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ListGames {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "List all games")
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,10 @@ mod join;
|
||||
mod leave;
|
||||
mod connect;
|
||||
mod settings;
|
||||
mod games;
|
||||
mod list_games;
|
||||
mod add_game;
|
||||
mod edit_game;
|
||||
|
||||
pub struct GamenightState {
|
||||
api_configuration: Configuration,
|
||||
|
||||
Reference in New Issue
Block a user