use std::{ffi::OsStr, fmt::Display}; use async_trait::async_trait; use gamenight_api_client_rs::models::{authorize_location_request_body::Op::Grant, AddLocationRequestBody, AuthorizeLocationRequestBody}; use inquire::{Editor, Text}; use super::*; #[derive(Clone)] pub struct AddLocation { } impl AddLocation { pub fn new() -> Self { Self {} } } #[async_trait] impl<'a> Flow<'a> for AddLocation { async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> { if let Some(name) = Text::new("What is the name of the location you want to add?").prompt_skippable()? { let address; let note; { address = Text::new("Optional: What is the address?").prompt_skippable()?; let mut editor_prompt = Editor::new("What is the name of the location you want to add?"); let editor_command; if let Some(editor) = &state.gamenight_configuration.editor { editor_command = editor.clone(); editor_prompt = editor_prompt.with_editor_command(OsStr::new(&editor_command)) } note = editor_prompt.prompt_skippable()?; } let add_location_request = AddLocationRequestBody { name, address, note }; let location_id = state.api.location_post(Some(add_location_request)).await?; let add_authorize_request = AuthorizeLocationRequestBody { location_id: location_id.location_id.to_string(), user_id: state.get_user_id()?.to_string(), op: Grant }; state.api.location_authorize_post(Some(add_authorize_request)).await?; } Ok((FlowOutcome::Cancelled, state)) } } impl Display for AddLocation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Add Location") } }