Added Location and location ownership/rights to gamenight.
This commit is contained in:
65
gamenight-cli/src/flows/add_location.rs
Normal file
65
gamenight-cli/src/flows/add_location.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use std::{ffi::OsStr, fmt::Display};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use gamenight_api_client_rs::{apis::default_api::{location_authorize_post, location_post}, 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 = location_post(&state.api_configuration, 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
|
||||
};
|
||||
|
||||
location_authorize_post(&state.api_configuration, 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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user