starts improving REST Api
This commit is contained in:
@@ -4,7 +4,7 @@ use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
||||
use futures::Future;
|
||||
use lootalot_db::{DbApi, Pool, QueryResult};
|
||||
use std::env;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
type AppPool = web::Data<Pool>;
|
||||
|
||||
/// Wraps call to the DbApi and process its result as a async HttpResponse
|
||||
@@ -49,6 +49,12 @@ pub fn db_call<
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct PlayerClaim {
|
||||
player_id: i32,
|
||||
item_id: i32,
|
||||
}
|
||||
|
||||
pub(crate) fn serve() -> std::io::Result<()> {
|
||||
let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set");
|
||||
dbg!(&www_root);
|
||||
@@ -60,7 +66,7 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
.wrap(
|
||||
Cors::new()
|
||||
.allowed_origin("http://localhost:8080")
|
||||
.allowed_methods(vec!["GET", "POST"])
|
||||
.allowed_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"])
|
||||
.max_age(3600),
|
||||
)
|
||||
.service(
|
||||
@@ -71,11 +77,25 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
db_call(pool, move |api| api.fetch_players())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/claims",
|
||||
web::get().to_async(move |pool: AppPool| {
|
||||
db_call(pool, move |api| api.fetch_claims())
|
||||
}),
|
||||
.service(
|
||||
web::resource("/claims")
|
||||
.route(web::get()
|
||||
.to_async(move |pool: AppPool| {
|
||||
db_call(pool, move |api| api
|
||||
.fetch_claims())
|
||||
}))
|
||||
.route(web::put()
|
||||
.to_async(move |pool: AppPool, data: web::Json<PlayerClaim>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_player(data.player_id)
|
||||
.claim(data.item_id))
|
||||
}))
|
||||
.route(web::delete()
|
||||
.to_async(move |pool: AppPool, data: web::Json<PlayerClaim>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_player(data.player_id)
|
||||
.unclaim(data.item_id))
|
||||
}))
|
||||
)
|
||||
.route(
|
||||
"/{player_id}/update-wealth/{amount}",
|
||||
@@ -89,34 +109,24 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
db_call(pool, move |api| api.as_player(*player_id).loot())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/{player_id}/claim/{item_id}",
|
||||
web::get().to_async(move |pool: AppPool, data: web::Path<(i32, i32)>| {
|
||||
db_call(pool, move |api| api.as_player(data.0).claim(data.1))
|
||||
}),
|
||||
.service(web::scope("/admin")
|
||||
.route(
|
||||
"/resolve-claims",
|
||||
web::get().to_async(move |pool: AppPool| {
|
||||
db_call(pool, move |api| api.as_admin().resolve_claims())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/add-player/{name}/{wealth}",
|
||||
web::get().to_async(
|
||||
move |pool: AppPool, data: web::Path<(String, f32)>| {
|
||||
db_call(pool, move |api| {
|
||||
api.as_admin().add_player(&data.0, data.1)
|
||||
})
|
||||
},
|
||||
),
|
||||
)
|
||||
)
|
||||
.route(
|
||||
"/{player_id}/unclaim/{item_id}",
|
||||
web::get().to_async(move |pool: AppPool, data: web::Path<(i32, i32)>| {
|
||||
db_call(pool, move |api| api.as_player(data.0).unclaim(data.1))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/admin/resolve-claims",
|
||||
web::get().to_async(move |pool: AppPool| {
|
||||
db_call(pool, move |api| api.as_admin().resolve_claims())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/admin/add-player/{name}/{wealth}",
|
||||
web::get().to_async(
|
||||
move |pool: AppPool, data: web::Path<(String, f32)>| {
|
||||
db_call(pool, move |api| {
|
||||
api.as_admin().add_player(&data.0, data.1)
|
||||
})
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user