add edit/delete game capabilities
This commit is contained in:
@@ -103,6 +103,63 @@ async fn add_game(
|
||||
Some(game)
|
||||
}
|
||||
|
||||
#[patch("/game", data = "<game>")]
|
||||
async fn update_game(
|
||||
_token: auth::AdminToken,
|
||||
game_list: &rocket::State<Mutex<Vec<Game>>>,
|
||||
user_list: &rocket::State<Mutex<Vec<User>>>,
|
||||
game: proto_utils::Proto<items::Game>,
|
||||
) -> Option<items::Game> {
|
||||
let mut games = game_list.lock().await;
|
||||
let mut game = game.into_inner();
|
||||
|
||||
game.title = game.title.trim().to_string();
|
||||
|
||||
if game.remote_id == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut r_existing = None;
|
||||
|
||||
if let Some(existing) = games.iter_mut().find(|g| g.title == game.title) {
|
||||
existing.source = game.source;
|
||||
existing.min_players = game.min_players;
|
||||
existing.max_players = game.max_players;
|
||||
existing.price = game.price;
|
||||
existing.remote_id = game.remote_id;
|
||||
|
||||
r_existing = Some(existing.clone());
|
||||
}
|
||||
let users = user_list.lock().await;
|
||||
save_state(&games, &users);
|
||||
|
||||
r_existing
|
||||
}
|
||||
|
||||
#[delete("/game/<title>")]
|
||||
async fn delete_game(
|
||||
_token: auth::AdminToken,
|
||||
game_list: &rocket::State<Mutex<Vec<Game>>>,
|
||||
user_list: &rocket::State<Mutex<Vec<User>>>,
|
||||
title: &str,
|
||||
) -> Option<items::Game> {
|
||||
let mut games = game_list.lock().await;
|
||||
|
||||
if let Some(pos) = games
|
||||
.iter()
|
||||
.position(|g| g.title.to_lowercase() == title.to_lowercase())
|
||||
{
|
||||
let game = games.remove(pos);
|
||||
|
||||
let users = user_list.lock().await;
|
||||
save_state(&games, &users);
|
||||
|
||||
return Some(game);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[post("/opinion", data = "<req>")]
|
||||
async fn add_opinion(
|
||||
token: auth::Token,
|
||||
@@ -334,6 +391,7 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
rocket::build()
|
||||
.manage(Mutex::new(user_list))
|
||||
.manage(auth::AuthState::new())
|
||||
.manage(auth::AdminState::new())
|
||||
.manage(Mutex::new(game_list))
|
||||
.mount(
|
||||
"/api",
|
||||
@@ -345,6 +403,8 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
add_opinion,
|
||||
remove_opinion,
|
||||
add_game,
|
||||
update_game,
|
||||
delete_game,
|
||||
get_game_thumbnail,
|
||||
get_games_batch
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user