forked from Roflin/gamenight
Implemented deleting games as admin.
This commit is contained in:
@@ -1,19 +1,27 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use std::fmt::{Display, Formatter};
|
||||
use chrono::{DateTime, Local};
|
||||
use uuid::Uuid;
|
||||
use crate::domain::owned_games::OwnedGames;
|
||||
use crate::domain::participants::Participants;
|
||||
use crate::domain::user::User;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Gamenight {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub start_time: DateTime<Local>,
|
||||
pub owner_id: Uuid,
|
||||
pub organizer: User,
|
||||
pub participants: Participants,
|
||||
pub owned_games: OwnedGames,
|
||||
}
|
||||
|
||||
impl Display for Gamenight {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, r#"Name: {}
|
||||
When: {}"#, self.name, self.start_time.format("%d-%m-%Y %H:%M"))
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
writeln!(f, "Name: {}", self.name)?;
|
||||
writeln!(f, "Organizer: {}", self.organizer)?;
|
||||
writeln!(f, "Start: {}", self.start_time.to_rfc3339())?;
|
||||
writeln!(f, "Participants: {}", self.participants)?;
|
||||
write!(f, "Owned games:\n{}", self.owned_games)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use chrono::{DateTime, Local};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GamenightSelectData {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub start_time: DateTime<Local>,
|
||||
pub owner_id: Uuid,
|
||||
}
|
||||
|
||||
impl Display for GamenightSelectData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, r#"Name: {}
|
||||
When: {}"#, self.name, self.start_time.format("%d-%m-%Y %H:%M"))
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
pub mod gamenight;
|
||||
pub mod gamenight_select_data;
|
||||
pub mod user;
|
||||
pub mod config;
|
||||
pub mod participants;
|
||||
pub mod game;
|
||||
pub mod owned_games;
|
||||
pub mod location;
|
||||
pub mod location;
|
||||
pub mod location_select_data;
|
||||
pub mod gamenight;
|
||||
@@ -2,12 +2,12 @@ use std::{collections::HashMap, fmt::Display};
|
||||
|
||||
use crate::domain::game::Game;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OwnedGames(pub HashMap<String, Vec<Game>>);
|
||||
|
||||
pub struct OwnedGames<'a>(pub &'a HashMap<String, Vec<Game>>);
|
||||
|
||||
impl<'a> Display for OwnedGames<'a> {
|
||||
impl Display for OwnedGames {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for (k,v) in self.0 {
|
||||
for (k,v) in &self.0 {
|
||||
write!(f, "{k}:\n")?;
|
||||
for g in v {
|
||||
write!(f, "\t{}\n", g.name)?;
|
||||
|
||||
@@ -2,9 +2,10 @@ use std::fmt::Display;
|
||||
|
||||
use crate::domain::user::User;
|
||||
|
||||
pub struct Participants<'a>(pub &'a Vec<User>);
|
||||
#[derive(Clone)]
|
||||
pub struct Participants(pub Vec<User>);
|
||||
|
||||
impl<'a> Display for Participants<'a> {
|
||||
impl<'a> Display for Participants {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let string_list: Vec<String> = self.0.iter().map(|x| {format!("{x}")}).collect();
|
||||
write!(f, "{}", string_list.join(", "))
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use gamenight_api_client_rs::models;
|
||||
use uuid::Uuid;
|
||||
use crate::flows::FlowError;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct User {
|
||||
@@ -13,4 +14,15 @@ impl Display for User {
|
||||
write!(f, "{}", self.username)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<models::User> for User {
|
||||
type Error = FlowError;
|
||||
|
||||
fn try_from(user: models::User) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
username: user.username,
|
||||
id: Uuid::parse_str(&user.id)?
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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)]
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -7,7 +7,6 @@ use crate::flows::{add_location::AddLocation, exit::Exit, list_locations::ListLo
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Locations {
|
||||
}
|
||||
|
||||
@@ -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>;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
];
|
||||
|
||||
@@ -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"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user