Get Single Empire
This commit is contained in:
parent
26e83a3aed
commit
eb63e033eb
3 changed files with 115 additions and 22 deletions
|
@ -145,6 +145,7 @@ async fn main() -> Result<()> {
|
|||
v3::create_group,
|
||||
v3::edit_group,
|
||||
v3::delete_group,
|
||||
v3::get_empire,
|
||||
v3::create_empire,
|
||||
v3::edit_empire,
|
||||
v3::delete_empire
|
||||
|
@ -158,6 +159,7 @@ async fn main() -> Result<()> {
|
|||
v3::schemas::PostGroupParams,
|
||||
v3::schemas::UpdateGroupParams,
|
||||
v3::schemas::DeleteGroupParams,
|
||||
v3::schemas::GetEmpireParams,
|
||||
v3::schemas::PostEmpireParams,
|
||||
v3::schemas::UpdateEmpireParams,
|
||||
v3::schemas::DeleteEmpireParams,
|
||||
|
@ -223,6 +225,7 @@ async fn main() -> Result<()> {
|
|||
.service(v3::create_group)
|
||||
.service(v3::edit_group)
|
||||
.service(v3::delete_group)
|
||||
.service(v3::get_empire)
|
||||
.service(v3::create_empire)
|
||||
.service(v3::edit_empire)
|
||||
.service(v3::delete_empire)
|
||||
|
|
122
src/v3/mod.rs
122
src/v3/mod.rs
|
@ -723,6 +723,104 @@ pub(crate) async fn delete_group(
|
|||
|
||||
// Empire Endpoints
|
||||
|
||||
#[utoipa::path(
|
||||
params(
|
||||
schemas::GetEmpireParams
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "OK", body = ChellarisEmpire),
|
||||
),
|
||||
security(
|
||||
("api_key" = [])
|
||||
),
|
||||
)]
|
||||
#[get("/api/v3/empire")]
|
||||
pub(crate) async fn get_empire(
|
||||
data: web::Data<AppState>,
|
||||
params: web::Query<schemas::GetEmpireParams>,
|
||||
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 db_empire: db::schemas::Empire;
|
||||
|
||||
let mut db_empire_query =
|
||||
QueryBuilder::<sqlx::Postgres>::new("SELECT * FROM public.empires WHERE");
|
||||
|
||||
db_empire_query.push(" id = ").push_bind(params.empire_id);
|
||||
db_empire_query
|
||||
.push(" AND game_id = ")
|
||||
.push_bind(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 db_ethics: HashMap<i32, schemas::EmpireEthic> = HashMap::new();
|
||||
|
||||
let mut db_ethic_query =
|
||||
QueryBuilder::<sqlx::Postgres>::new("SELECT * FROM public.empire_ethics WHERE");
|
||||
|
||||
db_ethic_query
|
||||
.push(" 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) => {
|
||||
for ethic in data {
|
||||
db_ethics.insert(
|
||||
ethic.ethics_id,
|
||||
schemas::EmpireEthic {
|
||||
ethic_id: ethic.ethics_id,
|
||||
fanatic: ethic.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::Unauthorized().finish();
|
||||
}
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
request_body = PostEmpireParams,
|
||||
responses(
|
||||
|
@ -806,9 +904,7 @@ pub(crate) async fn create_empire(
|
|||
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("empire_id").push(", empire_game_id");
|
||||
db_ethic_query.push(", ethics_id").push(", fanatic");
|
||||
|
||||
db_ethic_query.push(") VALUES (");
|
||||
|
@ -1011,12 +1107,8 @@ pub(crate) async fn edit_empire(
|
|||
.push(" AND empire_game_id = ")
|
||||
.push_bind(params.game_id);
|
||||
|
||||
match db_ethic_wipe
|
||||
.build()
|
||||
.execute(&data.db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {},
|
||||
match db_ethic_wipe.build().execute(&data.db).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
return HttpResponse::UnprocessableEntity()
|
||||
.body(format!("{:#?}", e.to_string()))
|
||||
|
@ -1027,9 +1119,7 @@ pub(crate) async fn edit_empire(
|
|||
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("empire_id").push(", empire_game_id");
|
||||
db_ethic_query.push(", ethics_id").push(", fanatic");
|
||||
|
||||
db_ethic_query.push(") VALUES (");
|
||||
|
@ -1128,9 +1218,8 @@ pub(crate) async fn delete_empire(
|
|||
// 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",
|
||||
"DELETE FROM public.empires WHERE id = $1 AND game_id = $2",
|
||||
param.empire_id,
|
||||
param.group_id,
|
||||
param.game_id
|
||||
)
|
||||
.execute(&data.db)
|
||||
|
@ -1148,11 +1237,6 @@ pub(crate) async fn delete_empire(
|
|||
|
||||
// Data Manipulation Endpoints
|
||||
|
||||
// Moderator & Admin
|
||||
// Add/Update/Remove Empire
|
||||
// Update Group
|
||||
// Update Game
|
||||
|
||||
// Admin
|
||||
// Add/Update/Remove Portrait
|
||||
// Add/Update/Remove Species
|
||||
|
|
|
@ -110,6 +110,14 @@ pub struct ChellarisGameGroupLegacy {
|
|||
|
||||
// Empire Structs
|
||||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug, IntoParams)]
|
||||
pub struct GetEmpireParams {
|
||||
#[schema(example = 1)]
|
||||
pub game_id: i32,
|
||||
#[schema(example = 1)]
|
||||
pub empire_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, ToSchema, Debug)]
|
||||
pub struct PostEmpireParams {
|
||||
#[schema(example = 1)]
|
||||
|
@ -165,8 +173,6 @@ pub struct DeleteEmpireParams {
|
|||
#[schema(example = 1)]
|
||||
pub game_id: i32,
|
||||
#[schema(example = 1)]
|
||||
pub group_id: i32,
|
||||
#[schema(example = 1)]
|
||||
pub empire_id: i32,
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue