Schema Update & Empire Endpoints
This commit is contained in:
parent
eb18526f51
commit
26e83a3aed
6 changed files with 600 additions and 74 deletions
|
@ -40,19 +40,18 @@ pub struct Ethic {
|
|||
pub struct Empire {
|
||||
pub id: i32,
|
||||
pub group_id: i32,
|
||||
pub group_game_id: i32,
|
||||
pub game_id: i32,
|
||||
pub name: String,
|
||||
pub discord_user: Option<String>,
|
||||
pub gestalt: bool,
|
||||
pub empire_portrait_id: Option<i32>,
|
||||
pub empire_portrait_group_id: Option<i32>,
|
||||
pub portrait_id: i32,
|
||||
pub portrait_group_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema, Debug, FromRow)]
|
||||
pub struct EmpireEthic {
|
||||
pub empires_id: i32,
|
||||
pub empires_group_id: i32,
|
||||
pub empires_group_game_id: i32,
|
||||
pub empire_id: i32,
|
||||
pub empire_game_id: i32,
|
||||
pub ethics_id: i32,
|
||||
pub ethics_fanatic: bool,
|
||||
pub fanatic: bool,
|
||||
}
|
||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -144,7 +144,10 @@ async fn main() -> Result<()> {
|
|||
v3::delete_game,
|
||||
v3::create_group,
|
||||
v3::edit_group,
|
||||
v3::delete_group
|
||||
v3::delete_group,
|
||||
v3::create_empire,
|
||||
v3::edit_empire,
|
||||
v3::delete_empire
|
||||
),
|
||||
components(schemas(
|
||||
v3::schemas::AuthReturn,
|
||||
|
@ -155,9 +158,13 @@ async fn main() -> Result<()> {
|
|||
v3::schemas::PostGroupParams,
|
||||
v3::schemas::UpdateGroupParams,
|
||||
v3::schemas::DeleteGroupParams,
|
||||
v3::schemas::PostEmpireParams,
|
||||
v3::schemas::UpdateEmpireParams,
|
||||
v3::schemas::DeleteEmpireParams,
|
||||
v3::schemas::FullViewData,
|
||||
v3::schemas::Ethic,
|
||||
v3::schemas::EmpireEthic,
|
||||
v3::schemas::EmpireEthicLegacy,
|
||||
v3::schemas::ChellarisGameLegacy,
|
||||
v3::schemas::ChellarisGameFlat,
|
||||
v3::schemas::ChellarisGame,
|
||||
|
@ -165,8 +172,9 @@ async fn main() -> Result<()> {
|
|||
v3::schemas::ChellarisGameGroupLegacy,
|
||||
v3::schemas::ChellarisGroupFlat,
|
||||
v3::schemas::Portrait,
|
||||
v3::schemas::ChellarisEmpire,
|
||||
v3::schemas::ChellarisEmpireFlat
|
||||
v3::schemas::ChellarisEmpireLegacy,
|
||||
v3::schemas::ChellarisEmpireFlat,
|
||||
v3::schemas::ChellarisEmpire
|
||||
)),
|
||||
modifiers(&ApiSecurity)
|
||||
)]
|
||||
|
@ -215,6 +223,9 @@ async fn main() -> Result<()> {
|
|||
.service(v3::create_group)
|
||||
.service(v3::edit_group)
|
||||
.service(v3::delete_group)
|
||||
.service(v3::create_empire)
|
||||
.service(v3::edit_empire)
|
||||
.service(v3::delete_empire)
|
||||
// Swagger UI
|
||||
.service(
|
||||
SwaggerUi::new(concat!(api_base!(), "/swagger/{_:.*}"))
|
||||
|
|
|
@ -27,11 +27,10 @@ pub(crate) async fn empire_ethics(
|
|||
|
||||
db_data.iter().for_each(|entry| {
|
||||
let new_data = schemas::EmpireEthic {
|
||||
empires_id: entry.empires_id,
|
||||
empires_group_game_id: entry.empires_group_game_id,
|
||||
empires_group_id: entry.empires_group_id,
|
||||
empires_id: entry.empire_id,
|
||||
empires_group_game_id: entry.empire_game_id,
|
||||
ethics_id: entry.ethics_id,
|
||||
ethics_fanatic: entry.ethics_fanatic
|
||||
ethics_fanatic: entry.fanatic
|
||||
};
|
||||
|
||||
parsed_data.push(new_data);
|
||||
|
@ -70,9 +69,9 @@ pub(crate) async fn empires(
|
|||
let mut new_data = schemas::Empire {
|
||||
id: entry.id,
|
||||
group_id: entry.group_id,
|
||||
empire_portrait_group_id: entry.empire_portrait_group_id,
|
||||
empire_portrait_id: entry.empire_portrait_id,
|
||||
group_game_id: entry.group_game_id,
|
||||
empire_portrait_group_id: Some(entry.portrait_group_id),
|
||||
empire_portrait_id: Some(entry.portrait_id),
|
||||
group_game_id: entry.game_id,
|
||||
discord_user: None,
|
||||
gestalt: Some(entry.gestalt)
|
||||
};
|
||||
|
|
|
@ -55,7 +55,6 @@ pub struct Empire {
|
|||
#[derive(Serialize, ToSchema, Debug, FromRow)]
|
||||
pub struct EmpireEthic {
|
||||
pub empires_id: i32,
|
||||
pub empires_group_id: i32,
|
||||
pub empires_group_game_id: i32,
|
||||
pub ethics_id: i32,
|
||||
pub ethics_fanatic: bool,
|
||||
|
|
508
src/v3/mod.rs
508
src/v3/mod.rs
|
@ -3,10 +3,14 @@ use std::{collections::HashMap, vec};
|
|||
use actix_web::{
|
||||
delete, get, post, put,
|
||||
web::{self, Json},
|
||||
HttpResponse, Responder, HttpRequest,
|
||||
HttpRequest, HttpResponse, Responder,
|
||||
};
|
||||
use sqlx::{Execute, QueryBuilder};
|
||||
|
||||
use crate::{db, AppState};
|
||||
use crate::{
|
||||
db::{self, schemas::EmpireEthic},
|
||||
AppState,
|
||||
};
|
||||
|
||||
pub(crate) mod schemas;
|
||||
|
||||
|
@ -41,10 +45,7 @@ fn get_auth_header<'a>(req: &'a HttpRequest) -> Option<&'a str> {
|
|||
),
|
||||
)]
|
||||
#[get("/api/v3/auth")]
|
||||
pub(crate) async fn auth(
|
||||
data: web::Data<AppState>,
|
||||
req: HttpRequest,
|
||||
) -> impl Responder {
|
||||
pub(crate) async fn auth(data: web::Data<AppState>, req: HttpRequest) -> impl Responder {
|
||||
let auth_token = get_auth_header(&req);
|
||||
|
||||
let auth_return = verify_auth(auth_token, &data);
|
||||
|
@ -89,7 +90,7 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
|
|||
.expect("Error Fetching Data from DB");
|
||||
|
||||
let db_empire_ethics: Vec<db::schemas::EmpireEthic> =
|
||||
sqlx::query_as("SELECT * FROM public.empire_ethics ORDER BY empires_id")
|
||||
sqlx::query_as("SELECT * FROM public.empire_ethics ORDER BY empire_id")
|
||||
.fetch_all(&data.db)
|
||||
.await
|
||||
.expect("Error Fetching Data from DB");
|
||||
|
@ -117,7 +118,7 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
|
|||
db_portrait_groups.iter().for_each(|species| {
|
||||
let new_data = schemas::Species {
|
||||
id: species.id,
|
||||
displayName: species.name.clone(),
|
||||
display: species.name.clone(),
|
||||
portraits: HashMap::new(),
|
||||
};
|
||||
|
||||
|
@ -178,20 +179,20 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
|
|||
});
|
||||
|
||||
db_empires.iter().for_each(|empire| {
|
||||
let new_data = schemas::ChellarisEmpire {
|
||||
let new_data = schemas::ChellarisEmpireLegacy {
|
||||
id: empire.id,
|
||||
gestalt: empire.gestalt,
|
||||
machine: false,
|
||||
group: empire.group_id,
|
||||
empire_portrait: empire.empire_portrait_id,
|
||||
empire_portrait_group: empire.empire_portrait_group_id,
|
||||
empire_portrait: empire.portrait_id,
|
||||
empire_portrait_group: empire.portrait_group_id,
|
||||
discord_user: None,
|
||||
ethics: HashMap::new(),
|
||||
};
|
||||
|
||||
parsed_data
|
||||
.games
|
||||
.get_mut(&empire.group_game_id)
|
||||
.get_mut(&empire.game_id)
|
||||
.unwrap()
|
||||
.empires
|
||||
.entry(empire.id)
|
||||
|
@ -203,7 +204,7 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
|
|||
db_ethics.iter().for_each(|ethic| {
|
||||
let new_data = schemas::Ethic {
|
||||
id: ethic.id,
|
||||
displayName: ethic.name.clone(),
|
||||
display: ethic.name.clone(),
|
||||
machine: ethic.machine_ethic,
|
||||
};
|
||||
|
||||
|
@ -215,16 +216,14 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
|
|||
});
|
||||
|
||||
db_empire_ethics.iter().for_each(|empire_ethic| {
|
||||
let game_id = empire_ethic.empires_group_game_id;
|
||||
let id = empire_ethic.empires_id;
|
||||
let game_id = empire_ethic.empire_game_id;
|
||||
let id = empire_ethic.empire_id;
|
||||
|
||||
let new_data = schemas::EmpireEthic {
|
||||
let new_data = schemas::EmpireEthicLegacy {
|
||||
id: empire_ethic.ethics_id,
|
||||
displayName: parsed_data.ethics[&empire_ethic.ethics_id]
|
||||
.displayName
|
||||
.clone(),
|
||||
display: parsed_data.ethics[&empire_ethic.ethics_id].display.clone(),
|
||||
machine: parsed_data.ethics[&empire_ethic.ethics_id].machine,
|
||||
fanatic: empire_ethic.ethics_fanatic,
|
||||
fanatic: empire_ethic.fanatic,
|
||||
};
|
||||
|
||||
if new_data.machine {
|
||||
|
@ -261,7 +260,7 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
|
|||
#[utoipa::path(
|
||||
params(),
|
||||
responses(
|
||||
(status = 200, description = "OK", body = HashMap<i32, ChellarisGameLite>),
|
||||
(status = 200, description = "OK", body = HashMap<i32, ChellarisGameFlat>),
|
||||
),
|
||||
security(
|
||||
("api_key" = [])
|
||||
|
@ -334,7 +333,7 @@ pub(crate) async fn get_game_data(
|
|||
|
||||
let db_empires: Vec<db::schemas::Empire> = sqlx::query_as!(
|
||||
db::schemas::Empire,
|
||||
"SELECT * FROM public.empires WHERE group_game_id = $1",
|
||||
"SELECT * FROM public.empires WHERE game_id = $1",
|
||||
param.game_id
|
||||
)
|
||||
.fetch_all(&data.db)
|
||||
|
@ -362,11 +361,20 @@ pub(crate) async fn get_game_data(
|
|||
let new_empire = schemas::ChellarisEmpireFlat {
|
||||
id: empire.id,
|
||||
group: empire.group_id,
|
||||
name: if user_auth.moderator || user_auth.admin {empire.name.clone()} else {"[REDACTED]".to_string()},
|
||||
discord_user: if user_auth.moderator || user_auth.admin {empire.discord_user.clone()} else {None},
|
||||
game: empire.game_id,
|
||||
name: if user_auth.moderator || user_auth.admin {
|
||||
empire.name.clone()
|
||||
} else {
|
||||
"[REDACTED]".to_string()
|
||||
},
|
||||
discord_user: if user_auth.moderator || user_auth.admin {
|
||||
empire.discord_user.clone()
|
||||
} else {
|
||||
None
|
||||
},
|
||||
gestalt: empire.gestalt,
|
||||
empire_portrait: empire.empire_portrait_id,
|
||||
empire_portrait_group: empire.empire_portrait_group_id,
|
||||
portrait_id: empire.portrait_id,
|
||||
portrait_group_id: empire.portrait_group_id,
|
||||
};
|
||||
|
||||
parsed_data
|
||||
|
@ -434,12 +442,13 @@ pub(crate) async fn create_game(
|
|||
|
||||
match sqlx::query!(
|
||||
"INSERT INTO public.game_groups(name, game_id) VALUES ($1, $2)",
|
||||
"N/A", db_game.id
|
||||
"N/A",
|
||||
db_game.id
|
||||
)
|
||||
.execute(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {},
|
||||
Ok(_) => {}
|
||||
Err(_) => return HttpResponse::UnprocessableEntity().finish(),
|
||||
};
|
||||
|
||||
|
@ -488,7 +497,8 @@ pub(crate) async fn edit_game(
|
|||
db_game = match sqlx::query_as!(
|
||||
db::schemas::Game,
|
||||
"UPDATE public.games SET name = $1 WHERE id = $2 RETURNING * ;",
|
||||
params.game_name, params.game_id
|
||||
params.game_name,
|
||||
params.game_id
|
||||
)
|
||||
.fetch_one(&data.db)
|
||||
.await
|
||||
|
@ -511,12 +521,10 @@ pub(crate) async fn edit_game(
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[utoipa::path(
|
||||
params(
|
||||
schemas::DeleteGameParam,
|
||||
),
|
||||
request_body = AuthParams,
|
||||
responses(
|
||||
(status = 200, description = "OK"),
|
||||
(status = 422, description = "Missing Game ID"),
|
||||
|
@ -548,7 +556,7 @@ pub(crate) async fn delete_game(
|
|||
Err(e) => {
|
||||
println!("{:#?}", e);
|
||||
return HttpResponse::UnprocessableEntity().finish();
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
return HttpResponse::Ok().into();
|
||||
|
@ -590,7 +598,8 @@ pub(crate) async fn create_group(
|
|||
db_group = match sqlx::query_as!(
|
||||
db::schemas::GameGroup,
|
||||
"INSERT INTO public.game_groups(name, game_id) VALUES ($1, $2) RETURNING * ",
|
||||
params.group_name, params.game_id
|
||||
params.group_name,
|
||||
params.game_id
|
||||
)
|
||||
.fetch_one(&data.db)
|
||||
.await
|
||||
|
@ -671,7 +680,6 @@ pub(crate) async fn edit_group(
|
|||
params(
|
||||
schemas::DeleteGroupParams,
|
||||
),
|
||||
request_body = AuthParams,
|
||||
responses(
|
||||
(status = 200, description = "OK"),
|
||||
(status = 422, description = "Missing Game ID"),
|
||||
|
@ -695,9 +703,13 @@ pub(crate) async fn delete_group(
|
|||
// SQL Queries
|
||||
// TODO: Optimize by utilizing some SQL magic, for now this has to do
|
||||
if user_auth.admin || user_auth.moderator {
|
||||
match sqlx::query!("DELETE FROM public.game_groups WHERE id = $1 AND game_id = $2 AND name != 'N/A'", param.group_id, param.game_id)
|
||||
.execute(&data.db)
|
||||
.await
|
||||
match sqlx::query!(
|
||||
"DELETE FROM public.game_groups WHERE id = $1 AND game_id = $2 AND name != 'N/A'",
|
||||
param.group_id,
|
||||
param.game_id
|
||||
)
|
||||
.execute(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(_) => return HttpResponse::UnprocessableEntity().finish(),
|
||||
|
@ -711,6 +723,428 @@ pub(crate) async fn delete_group(
|
|||
|
||||
// Empire Endpoints
|
||||
|
||||
#[utoipa::path(
|
||||
request_body = PostEmpireParams,
|
||||
responses(
|
||||
(status = 200, description = "OK", body = ChellarisEmpireFlat),
|
||||
(status = 422, description = "Missing Game Name"),
|
||||
(status = 401, description = "Auth Token Invalid"),
|
||||
),
|
||||
security(
|
||||
("api_key" = [])
|
||||
),
|
||||
)]
|
||||
#[post("/api/v3/empire")]
|
||||
pub(crate) async fn create_empire(
|
||||
data: web::Data<AppState>,
|
||||
params: web::Json<schemas::PostEmpireParams>,
|
||||
req: HttpRequest,
|
||||
) -> impl Responder {
|
||||
let auth_token = get_auth_header(&req);
|
||||
let params = params.into_inner();
|
||||
|
||||
let user_auth: schemas::AuthReturn = verify_auth(auth_token.as_deref(), &data);
|
||||
|
||||
// SQL Queries
|
||||
// TODO: Optimize by utilizing some SQL magic, for now this has to do
|
||||
if user_auth.admin || user_auth.moderator {
|
||||
let mut all_params_present = true;
|
||||
|
||||
if params.empire_name == "" {
|
||||
all_params_present = false;
|
||||
}
|
||||
|
||||
if all_params_present {
|
||||
// Basic Empire Creation
|
||||
let db_empire: db::schemas::Empire;
|
||||
|
||||
let mut db_empire_query =
|
||||
QueryBuilder::<sqlx::Postgres>::new("INSERT INTO public.empires(");
|
||||
|
||||
db_empire_query.push("group_id");
|
||||
db_empire_query.push(", game_id");
|
||||
db_empire_query.push(", name");
|
||||
db_empire_query.push(", gestalt");
|
||||
db_empire_query.push(", portrait_id");
|
||||
db_empire_query.push(", portrait_group_id");
|
||||
db_empire_query.push(", discord_user");
|
||||
db_empire_query.push(") VALUES (");
|
||||
|
||||
db_empire_query.push("").push_bind(params.group_id);
|
||||
db_empire_query.push(", ").push_bind(params.game_id);
|
||||
db_empire_query.push(", ").push_bind(params.empire_name);
|
||||
db_empire_query.push(", ").push_bind(params.gestalt);
|
||||
db_empire_query.push(", ").push_bind(params.portrait_id);
|
||||
db_empire_query
|
||||
.push(", ")
|
||||
.push_bind(params.portrait_group_id);
|
||||
|
||||
if let Some(discord_user) = params.discord_user {
|
||||
db_empire_query.push(", ").push_bind(discord_user);
|
||||
} else {
|
||||
let val: Option<String> = None;
|
||||
db_empire_query.push(", ").push_bind(val);
|
||||
}
|
||||
|
||||
db_empire_query.push(") RETURNING * ");
|
||||
|
||||
db_empire = match db_empire_query
|
||||
.build_query_as::<db::schemas::Empire>()
|
||||
.fetch_one(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
return HttpResponse::UnprocessableEntity()
|
||||
.body(format!("{:#?}", e.to_string()))
|
||||
}
|
||||
};
|
||||
|
||||
let mut db_ethics: HashMap<i32, schemas::EmpireEthic> = HashMap::new();
|
||||
|
||||
for ethic in params.ethics {
|
||||
let mut db_ethic_query =
|
||||
QueryBuilder::<sqlx::Postgres>::new("INSERT INTO public.empire_ethics(");
|
||||
|
||||
db_ethic_query
|
||||
.push("empire_id")
|
||||
.push(", empire_game_id");
|
||||
db_ethic_query.push(", ethics_id").push(", fanatic");
|
||||
|
||||
db_ethic_query.push(") VALUES (");
|
||||
|
||||
db_ethic_query.push("").push_bind(db_empire.id);
|
||||
db_ethic_query.push(", ").push_bind(db_empire.game_id);
|
||||
db_ethic_query.push(", ").push_bind(ethic.ethic_id);
|
||||
db_ethic_query.push(", ").push_bind(ethic.fanatic);
|
||||
|
||||
db_ethic_query.push(") RETURNING * ");
|
||||
|
||||
match db_ethic_query
|
||||
.build_query_as::<db::schemas::EmpireEthic>()
|
||||
.fetch_one(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(data) => db_ethics.insert(
|
||||
data.ethics_id,
|
||||
schemas::EmpireEthic {
|
||||
ethic_id: data.ethics_id,
|
||||
fanatic: data.fanatic,
|
||||
},
|
||||
),
|
||||
Err(e) => {
|
||||
return HttpResponse::UnprocessableEntity()
|
||||
.body(format!("{:#?}", e.to_string()))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Empire Ethic Creation
|
||||
let parsed_empire: schemas::ChellarisEmpire = schemas::ChellarisEmpire {
|
||||
id: db_empire.id,
|
||||
group: db_empire.group_id,
|
||||
game: db_empire.game_id,
|
||||
name: db_empire.name,
|
||||
discord_user: db_empire.discord_user,
|
||||
gestalt: db_empire.gestalt,
|
||||
portrait_id: db_empire.portrait_id,
|
||||
portrait_group_id: db_empire.portrait_group_id,
|
||||
ethics: db_ethics,
|
||||
};
|
||||
|
||||
return HttpResponse::Ok().json(parsed_empire);
|
||||
} else {
|
||||
return HttpResponse::UnprocessableEntity().finish();
|
||||
}
|
||||
} else {
|
||||
return HttpResponse::Unauthorized().finish();
|
||||
}
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
request_body = UpdateEmpireParams,
|
||||
responses(
|
||||
(status = 200, description = "OK", body = ChellarisEmpireFlat),
|
||||
(status = 422, description = "Missing Game Name"),
|
||||
(status = 401, description = "Auth Token Invalid"),
|
||||
),
|
||||
security(
|
||||
("api_key" = [])
|
||||
),
|
||||
)]
|
||||
#[put("/api/v3/empire")]
|
||||
pub(crate) async fn edit_empire(
|
||||
data: web::Data<AppState>,
|
||||
params: web::Json<schemas::UpdateEmpireParams>,
|
||||
req: HttpRequest,
|
||||
) -> impl Responder {
|
||||
let auth_token = get_auth_header(&req);
|
||||
let params = params.into_inner();
|
||||
|
||||
let user_auth: schemas::AuthReturn = verify_auth(auth_token.as_deref(), &data);
|
||||
|
||||
// SQL Queries
|
||||
// TODO: Optimize by utilizing some SQL magic, for now this has to do
|
||||
if user_auth.admin || user_auth.moderator {
|
||||
let mut any_param_present = false;
|
||||
|
||||
let mut db_empire_query = QueryBuilder::<sqlx::Postgres>::new("UPDATE public.empires SET ");
|
||||
let mut db_empire_separated = db_empire_query.separated(", ");
|
||||
|
||||
if let Some(new_game) = params.game_id {
|
||||
any_param_present = true;
|
||||
db_empire_separated
|
||||
.push(" game_id = ")
|
||||
.push_bind_unseparated(new_game);
|
||||
}
|
||||
|
||||
if let Some(new_group) = params.group_id {
|
||||
any_param_present = true;
|
||||
db_empire_separated
|
||||
.push(" group_id = ")
|
||||
.push_bind_unseparated(new_group);
|
||||
}
|
||||
|
||||
if let Some(new_name) = params.empire_name {
|
||||
if new_name != "" {
|
||||
any_param_present = true;
|
||||
db_empire_separated
|
||||
.push(" name = ")
|
||||
.push_bind_unseparated(new_name);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(new_gestalt) = params.gestalt {
|
||||
any_param_present = true;
|
||||
db_empire_separated
|
||||
.push(" gestalt = ")
|
||||
.push_bind_unseparated(new_gestalt);
|
||||
}
|
||||
|
||||
if let Some(new_portrait) = params.portrait_id {
|
||||
any_param_present = true;
|
||||
db_empire_separated
|
||||
.push(" portrait_id = ")
|
||||
.push_bind_unseparated(new_portrait);
|
||||
}
|
||||
|
||||
if let Some(new_portrait_group) = params.portrait_group_id {
|
||||
any_param_present = true;
|
||||
db_empire_separated
|
||||
.push(" portrait_group_id = ")
|
||||
.push_bind_unseparated(new_portrait_group);
|
||||
}
|
||||
|
||||
if let Some(new_discord_user) = params.discord_user {
|
||||
if new_discord_user != "" {
|
||||
any_param_present = true;
|
||||
db_empire_separated
|
||||
.push(" discord_user = ")
|
||||
.push_bind_unseparated(new_discord_user);
|
||||
}
|
||||
}
|
||||
|
||||
let db_empire: db::schemas::Empire;
|
||||
|
||||
if any_param_present {
|
||||
db_empire_separated
|
||||
.push_unseparated(" WHERE id = ")
|
||||
.push_bind_unseparated(params.empire_id);
|
||||
db_empire_separated
|
||||
.push_unseparated(" AND group_id = ")
|
||||
.push_bind_unseparated(params.group_id);
|
||||
db_empire_separated
|
||||
.push_unseparated(" AND game_id = ")
|
||||
.push_bind_unseparated(params.game_id);
|
||||
db_empire_separated.push_unseparated(" RETURNING * ");
|
||||
|
||||
db_empire = match db_empire_query
|
||||
.build_query_as::<db::schemas::Empire>()
|
||||
.fetch_one(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
return HttpResponse::UnprocessableEntity()
|
||||
.body(format!("{:#?}", e.to_string()))
|
||||
}
|
||||
};
|
||||
} else {
|
||||
let mut db_empire_query =
|
||||
QueryBuilder::<sqlx::Postgres>::new("SELECT * FROM public.empires");
|
||||
db_empire_separated
|
||||
.push_unseparated(" WHERE id = ")
|
||||
.push_bind_unseparated(params.empire_id);
|
||||
db_empire_separated
|
||||
.push_unseparated(" AND group_id = ")
|
||||
.push_bind_unseparated(params.group_id);
|
||||
db_empire_separated
|
||||
.push_unseparated(" AND game_id = ")
|
||||
.push_bind_unseparated(params.game_id);
|
||||
|
||||
db_empire = match db_empire_query
|
||||
.build_query_as::<db::schemas::Empire>()
|
||||
.fetch_one(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
return HttpResponse::UnprocessableEntity()
|
||||
.body(format!("{:#?}", e.to_string()))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let mut ethics_changed = false;
|
||||
let mut db_ethics: HashMap<i32, schemas::EmpireEthic> = HashMap::new();
|
||||
|
||||
if let Some(new_ethics) = params.ethics {
|
||||
if new_ethics.len() != 0 {
|
||||
ethics_changed = true;
|
||||
|
||||
let mut db_ethic_wipe =
|
||||
QueryBuilder::<sqlx::Postgres>::new("DELETE FROM public.empire_ethics");
|
||||
db_ethic_wipe
|
||||
.push(" WHERE empire_id = ")
|
||||
.push_bind(params.empire_id);
|
||||
db_ethic_wipe
|
||||
.push(" AND empire_game_id = ")
|
||||
.push_bind(params.game_id);
|
||||
|
||||
match db_ethic_wipe
|
||||
.build()
|
||||
.execute(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
return HttpResponse::UnprocessableEntity()
|
||||
.body(format!("{:#?}", e.to_string()))
|
||||
}
|
||||
};
|
||||
|
||||
for ethic in new_ethics {
|
||||
let mut db_ethic_query =
|
||||
QueryBuilder::<sqlx::Postgres>::new("INSERT INTO public.empire_ethics(");
|
||||
|
||||
db_ethic_query
|
||||
.push("empire_id")
|
||||
.push(", empire_game_id");
|
||||
db_ethic_query.push(", ethics_id").push(", fanatic");
|
||||
|
||||
db_ethic_query.push(") VALUES (");
|
||||
|
||||
db_ethic_query.push("").push_bind(db_empire.id);
|
||||
db_ethic_query.push(", ").push_bind(db_empire.game_id);
|
||||
db_ethic_query.push(", ").push_bind(ethic.ethic_id);
|
||||
db_ethic_query.push(", ").push_bind(ethic.fanatic);
|
||||
|
||||
db_ethic_query.push(") RETURNING * ");
|
||||
|
||||
match db_ethic_query.build().execute(&data.db).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
return HttpResponse::UnprocessableEntity()
|
||||
.body(format!("{:#?}", e.to_string()))
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut db_ethic_query =
|
||||
QueryBuilder::<sqlx::Postgres>::new("SELECT * FROM public.empire_ethics");
|
||||
db_ethic_query
|
||||
.push(" WHERE empire_id = ")
|
||||
.push_bind(params.empire_id);
|
||||
db_ethic_query
|
||||
.push(" AND empire_game_id = ")
|
||||
.push_bind(params.game_id);
|
||||
|
||||
match db_ethic_query
|
||||
.build_query_as::<db::schemas::EmpireEthic>()
|
||||
.fetch_all(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(data) => data.iter().for_each(|db_ethic| {
|
||||
db_ethics.insert(
|
||||
db_ethic.ethics_id,
|
||||
schemas::EmpireEthic {
|
||||
ethic_id: db_ethic.ethics_id,
|
||||
fanatic: db_ethic.fanatic,
|
||||
},
|
||||
);
|
||||
}),
|
||||
Err(e) => {
|
||||
return HttpResponse::UnprocessableEntity().body(format!("{:#?}", e.to_string()))
|
||||
}
|
||||
};
|
||||
|
||||
if any_param_present || ethics_changed {
|
||||
let parsed_empire: schemas::ChellarisEmpire = schemas::ChellarisEmpire {
|
||||
id: db_empire.id,
|
||||
group: db_empire.group_id,
|
||||
game: db_empire.game_id,
|
||||
name: db_empire.name,
|
||||
discord_user: db_empire.discord_user,
|
||||
gestalt: db_empire.gestalt,
|
||||
portrait_id: db_empire.portrait_id,
|
||||
portrait_group_id: db_empire.portrait_group_id,
|
||||
ethics: db_ethics,
|
||||
};
|
||||
return HttpResponse::Ok().json(parsed_empire);
|
||||
} else {
|
||||
return HttpResponse::UnprocessableEntity().finish();
|
||||
}
|
||||
} else {
|
||||
return HttpResponse::Unauthorized().finish();
|
||||
}
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
params(
|
||||
schemas::DeleteEmpireParams,
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "OK"),
|
||||
(status = 422, description = "Missing Game ID"),
|
||||
(status = 401, description = "Auth Token Invalid"),
|
||||
),
|
||||
security(
|
||||
("api_key" = [])
|
||||
),
|
||||
)]
|
||||
#[delete("/api/v3/empire")]
|
||||
pub(crate) async fn delete_empire(
|
||||
data: web::Data<AppState>,
|
||||
req: HttpRequest,
|
||||
param: web::Query<schemas::DeleteEmpireParams>,
|
||||
) -> impl Responder {
|
||||
let auth_token = get_auth_header(&req);
|
||||
let param = param.into_inner();
|
||||
|
||||
let user_auth: schemas::AuthReturn = verify_auth(auth_token, &data);
|
||||
|
||||
// SQL Queries
|
||||
// TODO: Optimize by utilizing some SQL magic, for now this has to do
|
||||
if user_auth.admin || user_auth.moderator {
|
||||
match sqlx::query!(
|
||||
"DELETE FROM public.empires WHERE id = $1 AND group_id = $2 AND game_id = $3",
|
||||
param.empire_id,
|
||||
param.group_id,
|
||||
param.game_id
|
||||
)
|
||||
.execute(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(_) => return HttpResponse::UnprocessableEntity().finish(),
|
||||
};
|
||||
|
||||
return HttpResponse::Ok().into();
|
||||
} else {
|
||||
return HttpResponse::Unauthorized().finish();
|
||||
}
|
||||
}
|
||||
|
||||
// Data Manipulation Endpoints
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ pub struct AuthReturn {
|
|||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug, IntoParams)]
|
||||
pub struct GetGameParam {
|
||||
#[schema(example = 0)]
|
||||
#[schema(example = 1)]
|
||||
pub game_id: i32,
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ pub struct PostGameParams {
|
|||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug)]
|
||||
pub struct UpdateGameParams {
|
||||
#[schema(example = 0)]
|
||||
#[schema(example = 1)]
|
||||
pub game_id: i32,
|
||||
#[schema(example = "Game XY")]
|
||||
pub game_name: String,
|
||||
|
@ -35,7 +35,7 @@ pub struct UpdateGameParams {
|
|||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug, IntoParams)]
|
||||
pub struct DeleteGameParam {
|
||||
#[schema(example = 0)]
|
||||
#[schema(example = 1)]
|
||||
pub game_id: i32,
|
||||
}
|
||||
|
||||
|
@ -51,7 +51,7 @@ pub struct ChellarisGameLegacy {
|
|||
pub id: i32,
|
||||
pub name: String,
|
||||
pub groups: HashMap<i32, ChellarisGameGroupLegacy>,
|
||||
pub empires: HashMap<i32, ChellarisEmpire>,
|
||||
pub empires: HashMap<i32, ChellarisEmpireLegacy>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema, Debug, Clone)]
|
||||
|
@ -74,15 +74,15 @@ pub struct ChellarisGame {
|
|||
pub struct PostGroupParams {
|
||||
#[schema(example = "Group XY")]
|
||||
pub group_name: String,
|
||||
#[schema(example = 0)]
|
||||
#[schema(example = 1)]
|
||||
pub game_id: i32
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug)]
|
||||
pub struct UpdateGroupParams {
|
||||
#[schema(example = 0)]
|
||||
#[schema(example = 1)]
|
||||
pub group_id: i32,
|
||||
#[schema(example = 0)]
|
||||
#[schema(example = 1)]
|
||||
pub game_id: i32,
|
||||
#[schema(example = "Group XY")]
|
||||
pub group_name: String
|
||||
|
@ -90,9 +90,9 @@ pub struct UpdateGroupParams {
|
|||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug, IntoParams)]
|
||||
pub struct DeleteGroupParams {
|
||||
#[schema(example = 0)]
|
||||
#[schema(example = 1)]
|
||||
pub group_id: i32,
|
||||
#[schema(example = 0)]
|
||||
#[schema(example = 1)]
|
||||
pub game_id: i32,
|
||||
}
|
||||
|
||||
|
@ -108,40 +108,124 @@ pub struct ChellarisGameGroupLegacy {
|
|||
pub name: String,
|
||||
}
|
||||
|
||||
// Empire Structs
|
||||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug)]
|
||||
pub struct PostEmpireParams {
|
||||
#[schema(example = 1)]
|
||||
pub game_id: i32,
|
||||
#[schema(example = 1)]
|
||||
pub group_id: i32,
|
||||
#[schema(example = "Example Empire")]
|
||||
pub empire_name: String,
|
||||
#[schema(example = true)]
|
||||
pub gestalt: bool,
|
||||
#[schema(example = 1)]
|
||||
pub portrait_id: i32,
|
||||
#[schema(example = 1)]
|
||||
pub portrait_group_id: i32,
|
||||
#[schema(example = json!([
|
||||
{ "ethic_id": 1, "fanatic": false },
|
||||
{ "ethic_id": 2, "fanatic": true },
|
||||
{ "ethic_id": 3, "fanatic": false },
|
||||
]))]
|
||||
pub ethics: Vec<EmpireEthic>,
|
||||
#[schema(example = "UsernameExample")]
|
||||
pub discord_user: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug)]
|
||||
pub struct UpdateEmpireParams {
|
||||
#[schema(example = 1)]
|
||||
pub empire_id: i32,
|
||||
#[schema(example = 1)]
|
||||
pub game_id: Option<i32>,
|
||||
#[schema(example = 1)]
|
||||
pub group_id: Option<i32>,
|
||||
#[schema(example = "Example Empire")]
|
||||
pub empire_name: Option<String>,
|
||||
#[schema(example = true)]
|
||||
pub gestalt: Option<bool>,
|
||||
#[schema(example = 1)]
|
||||
pub portrait_id: Option<i32>,
|
||||
#[schema(example = 1)]
|
||||
pub portrait_group_id: Option<i32>,
|
||||
#[schema(example = json!([
|
||||
{ "ethic_id": 1, "fanatic": false },
|
||||
{ "ethic_id": 2, "fanatic": true },
|
||||
{ "ethic_id": 3, "fanatic": false },
|
||||
]))]
|
||||
pub ethics: Option<Vec<EmpireEthic>>,
|
||||
#[schema(example = "UsernameExample")]
|
||||
pub discord_user: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug, IntoParams)]
|
||||
pub struct DeleteEmpireParams {
|
||||
#[schema(example = 1)]
|
||||
pub game_id: i32,
|
||||
#[schema(example = 1)]
|
||||
pub group_id: i32,
|
||||
#[schema(example = 1)]
|
||||
pub empire_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema, Debug, Clone)]
|
||||
pub struct ChellarisEmpireFlat {
|
||||
pub id: i32,
|
||||
pub group: i32,
|
||||
pub game: i32,
|
||||
pub name: String,
|
||||
pub discord_user: Option<String>,
|
||||
pub gestalt: bool,
|
||||
pub empire_portrait: Option<i32>,
|
||||
pub empire_portrait_group: Option<i32>,
|
||||
pub portrait_id: i32,
|
||||
pub portrait_group_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema, Debug, Clone)]
|
||||
pub struct ChellarisEmpire {
|
||||
pub id: i32,
|
||||
pub group: i32,
|
||||
pub game: i32,
|
||||
pub name: String,
|
||||
pub discord_user: Option<String>,
|
||||
pub gestalt: bool,
|
||||
pub portrait_id: i32,
|
||||
pub portrait_group_id: i32,
|
||||
pub ethics: HashMap<i32, EmpireEthic>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema, Debug, Clone)]
|
||||
pub struct ChellarisEmpireLegacy {
|
||||
pub id: i32,
|
||||
pub group: i32,
|
||||
pub gestalt: bool,
|
||||
pub machine: bool,
|
||||
pub group: i32,
|
||||
pub empire_portrait: Option<i32>,
|
||||
pub empire_portrait_group: Option<i32>,
|
||||
pub empire_portrait: i32,
|
||||
pub empire_portrait_group: i32,
|
||||
pub discord_user: Option<String>,
|
||||
pub ethics: HashMap<i32, EmpireEthic>,
|
||||
pub ethics: HashMap<i32, EmpireEthicLegacy>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema, Debug, Clone)]
|
||||
pub struct Ethic {
|
||||
pub id: i32,
|
||||
pub displayName: String,
|
||||
pub display: String,
|
||||
pub machine: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema, Debug, Clone)]
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug, Clone)]
|
||||
pub struct EmpireEthic {
|
||||
#[schema(example = "1")]
|
||||
pub ethic_id: i32,
|
||||
#[schema(example = "true")]
|
||||
pub fanatic: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema, Debug, Clone)]
|
||||
pub struct EmpireEthicLegacy {
|
||||
pub id: i32,
|
||||
pub displayName: String,
|
||||
pub display: String,
|
||||
pub machine: bool,
|
||||
pub fanatic: bool,
|
||||
}
|
||||
|
@ -149,7 +233,7 @@ pub struct EmpireEthic {
|
|||
#[derive(Serialize, ToSchema, Debug, Clone)]
|
||||
pub struct Species {
|
||||
pub id: i32,
|
||||
pub displayName: String,
|
||||
pub display: String,
|
||||
pub portraits: HashMap<i32, Portrait>,
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue