Implemented deleting games as admin.

This commit is contained in:
2026-01-02 15:24:38 +01:00
parent 0c256846f6
commit 5c928b30f3
26 changed files with 344 additions and 133 deletions

View File

@@ -1,12 +1,10 @@
use std::fmt::Display;
use async_trait::async_trait;
use gamenight_api_client_rs::{apis::default_api::game_post, models::AddGameRequestBody, models::OwnGameRequestBody};
use gamenight_api_client_rs::apis::default_api::{locations_get, own_post};
use inquire::{Confirm, Select, Text};
use crate::flows::flow_helpers::LocationSelectData;
use crate::flows::own::Own;
use super::*;
use crate::flows::own::Own;
use async_trait::async_trait;
use gamenight_api_client_rs::{apis::default_api::game_post, models::AddGameRequestBody};
use inquire::Text;
#[derive(Clone)]

View File

@@ -1,25 +0,0 @@
use std::fmt::{Display, Formatter};
use gamenight_api_client_rs::models::Location;
use uuid::Uuid;
use crate::flows::FlowError;
pub struct LocationSelectData {
pub id: Uuid,
pub name: String,
}
impl Display for LocationSelectData {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.name) }
}
impl TryFrom<&Location> for LocationSelectData {
type Error = FlowError;
fn try_from(value: &Location) -> Result<Self, Self::Error> {
let uuid = Uuid::parse_str(&value.id)?;
Ok(LocationSelectData {
id: uuid,
name: value.name.clone(),
})
}
}

View File

@@ -4,7 +4,7 @@ use gamenight_api_client_rs::apis::default_api::get_gamenights;
use inquire::Select;
use uuid::Uuid;
use crate::{domain::{gamenight::Gamenight}, flows::view_gamenight::ViewGamenight};
use crate::{domain::{gamenight_select_data::GamenightSelectData}, flows::view_gamenight::ViewGamenight};
use super::{exit::Exit, *};
@@ -27,7 +27,7 @@ impl<'a> Flow<'a> for ListGamenights {
let mut view_flows: Vec<Box<dyn Flow<'_> + Send>> = vec![];
for response in response.iter() {
let gamenight = Gamenight {
let gamenight = GamenightSelectData {
id: Uuid::parse_str(&response.id)?,
name: response.name.clone(),
start_time:DateTime::parse_from_rfc3339(&response.datetime)?.into(),

View File

@@ -7,7 +7,6 @@ use crate::flows::{add_location::AddLocation, exit::Exit, list_locations::ListLo
use super::*;
#[derive(Clone)]
pub struct Locations {
}

View File

@@ -35,7 +35,7 @@ mod list_locations;
mod view_location;
mod add_location;
mod location_authorize;
mod flow_helpers;
mod remove_game;
pub struct GamenightState {
api_configuration: Configuration,
@@ -140,7 +140,7 @@ impl From<jsonwebtoken::errors::Error> for FlowError {
pub enum FlowOutcome {
Successful,
Cancelled,
Abort
Abort,
}
type FlowResult<'a> = Result<(FlowOutcome, &'a mut GamenightState), FlowError>;

View File

@@ -1,7 +1,7 @@
use std::fmt::Display;
use super::{Flow, FlowError, FlowOutcome, FlowResult, GamenightState};
use crate::flows::flow_helpers::LocationSelectData;
use crate::domain::location_select_data::LocationSelectData;
use async_trait::async_trait;
use gamenight_api_client_rs::apis::default_api::locations_get;
use gamenight_api_client_rs::models::OwnGameRequestBody;

View File

@@ -0,0 +1,38 @@
use std::fmt::Display;
use async_trait::async_trait;
use gamenight_api_client_rs::models::GameId;
use super::{Flow, FlowOutcome, FlowResult, GamenightState};
use crate::domain::game::Game;
#[derive(Clone)]
pub struct RemoveGame {
pub game: Game
}
impl RemoveGame {
pub fn new(game: Game) -> Self {
Self{
game
}
}
}
#[async_trait]
impl<'a> Flow<'a> for RemoveGame {
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
let req = GameId {
game_id: self.game.id.to_string()
};
gamenight_api_client_rs::apis::default_api::game_delete(&state.api_configuration, Some(req)).await?;
//Hack to return to right stack item, skipping detail view that doesn't exist.
Ok((FlowOutcome::Abort, state))
}
}
impl Display for RemoveGame {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Remove")
}
}

View File

@@ -4,7 +4,7 @@ use inquire::Select;
use uuid::Uuid;
use crate::{domain::game::Game, flows::{disown::Disown, exit::Exit, own::Own, rename_game::RenameGame}};
use crate::flows::remove_game::RemoveGame;
use super::*;
#[derive(Clone)]
@@ -45,6 +45,7 @@ impl<'a> Flow<'a> for ViewGame {
let options: Vec<Box<dyn Flow<'a> + Send>> = vec![
own_or_disown,
Box::new(RemoveGame::new(game.clone())),
Box::new(RenameGame::new(game.clone())),
Box::new(Exit::new())
];

View File

@@ -1,24 +1,23 @@
use crate::domain::gamenight::Gamenight;
use std::collections::HashMap;
use futures::future::join_all;
use gamenight_api_client_rs::{apis::default_api::{game_get, owned_games_get, participants_get, user_get, GameGetError}, models::{self, GameId, GamenightId, UserId}};
use inquire::Select;
use uuid::Uuid;
use crate::{domain::{game::Game, gamenight::Gamenight, owned_games::OwnedGames, participants::Participants, user::User}, flows::{exit::Exit, join::Join, leave::Leave}};
use crate::{domain::{game::Game, gamenight_select_data::GamenightSelectData, owned_games::OwnedGames, participants::Participants}, flows::{exit::Exit, join::Join, leave::Leave}};
use super::*;
#[derive(Clone)]
pub struct ViewGamenight {
gamenight: Gamenight
pub gamenight_select_data: GamenightSelectData,
}
impl ViewGamenight {
pub fn new(gamenight: Gamenight) -> Self {
pub fn new(gamenight_select_data: GamenightSelectData) -> Self {
Self {
gamenight
gamenight_select_data
}
}
}
@@ -26,38 +25,41 @@ impl ViewGamenight {
#[async_trait]
impl<'a> Flow<'a> for ViewGamenight {
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
let participants = participants_get(&state.api_configuration, Some(GamenightId{gamenight_id: self.gamenight.id.to_string()})).await?;
let mut users = vec![];
let mut gamenight = Gamenight {
id: self.gamenight_select_data.id,
start_time: self.gamenight_select_data.start_time,
name: self.gamenight_select_data.name.clone(),
organizer: user_get(&state.api_configuration, Some(UserId{user_id: self.gamenight_select_data.owner_id.to_string()})).await?.try_into()?,
participants: Participants(vec![]),
owned_games: OwnedGames(HashMap::new())
};
let participants = participants_get(&state.api_configuration, Some(GamenightId{gamenight_id: gamenight.id.to_string()})).await?;
for participant in participants.participants.iter() {
let user = user_get(&state.api_configuration, Some(UserId{user_id: participant.clone()})).await?;
users.push(User {
id: Uuid::parse_str(&user.id)?,
username: user.username
});
gamenight.participants.0.push(user_get(&state.api_configuration, Some(UserId{user_id: participant.clone()})).await?.try_into()?);
}
let mut user_games: HashMap<String, Vec<Game>> = HashMap::new();
for user in &users {
for user in &gamenight.participants.0 {
let request = UserId{ user_id: user.id.to_string() };
let games: Vec<Game> = join_all(owned_games_get(&state.api_configuration, Some(request)).await?
.iter().map(async |game_id| -> Result<models::Game, Error<GameGetError>> {
let request = GameId{ game_id: game_id.clone() };
game_get(&state.api_configuration, Some(request)).await
})).await.into_iter().collect::<Result<Vec<models::Game>, Error<GameGetError>>>()?.into_iter().map(Into::into).collect();
user_games.insert(user.username.clone(), games);
gamenight.owned_games.0.insert(user.username.clone(), games);
}
println!("{}\nwho: {}\nGames:\n{}", self.gamenight, Participants(&users), OwnedGames(&user_games));
println!("{}", gamenight);
let my_uid = state.get_user_id()?;
let join_or_leave: Box<dyn Flow<'a> + Send> =
if users.iter().map(|x| {x.id}).any(|x| x == my_uid) {
Box::new(Leave::new(self.gamenight.id))
if gamenight.participants.0.iter().map(|x| {x.id}).any(|x| x == my_uid) {
Box::new(Leave::new(gamenight.id))
}
else {
Box::new(Join::new(self.gamenight.id))
else {
Box::new(Join::new(gamenight.id))
};
let options: Vec<Box<dyn Flow<'a> + Send>> = vec![
@@ -71,9 +73,9 @@ impl<'a> Flow<'a> for ViewGamenight {
}
}
impl Display for ViewGamenight {
impl<'a> Display for ViewGamenight {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.gamenight.name, self.gamenight.start_time.format("%d-%m-%Y %H:%M"))
write!(f, "{} {}", self.gamenight_select_data.name, self.gamenight_select_data.start_time.format("%d-%m-%Y %H:%M"))
}
}