reorganizes api endpoints
This commit is contained in:
@@ -12,7 +12,7 @@ use diesel::query_dsl::RunQueryDsl;
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
|
||||
pub mod models;
|
||||
mod updates;
|
||||
//mod updates;
|
||||
mod schema;
|
||||
|
||||
/// The connection used
|
||||
|
||||
@@ -20,12 +20,6 @@ pub struct Player {
|
||||
pub pp: i32,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
pub fn by_id(id: i32) -> Self {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// Unpack a floating value of gold pieces to integer
|
||||
/// values of copper, silver, gold and platinum pieces
|
||||
///
|
||||
|
||||
@@ -18,7 +18,7 @@ export const Api = {
|
||||
.then(r => r.json())
|
||||
},
|
||||
fetchPlayerList () {
|
||||
return fetch(API_ENDPOINT("players/all"))
|
||||
return fetch(API_ENDPOINT("players/"))
|
||||
.then(r => r.json())
|
||||
},
|
||||
fetchInventory () {
|
||||
@@ -30,7 +30,7 @@ export const Api = {
|
||||
.then(r => r.json())
|
||||
},
|
||||
fetchLoot (playerId) {
|
||||
return fetch(API_ENDPOINT("players/loot/" + playerId))
|
||||
return fetch(API_ENDPOINT("players/" + playerId + "/loot"))
|
||||
.then(r => r.json())
|
||||
},
|
||||
putClaim (player_id, item_id) {
|
||||
@@ -43,15 +43,15 @@ export const Api = {
|
||||
},
|
||||
updateWealth (player_id, value_in_gp) {
|
||||
const payload = { player_id, value_in_gp: Number(value_in_gp) };
|
||||
return this.__doFetch("players/update-wealth", 'PUT', payload);
|
||||
return this.__doFetch("players/" + player_id + "/wealth", 'PUT', payload);
|
||||
},
|
||||
buyItems (player_id, items) {
|
||||
const payload = { player_id, items };
|
||||
return this.__doFetch("players/buy", 'POST', payload);
|
||||
return this.__doFetch("players/" + player_id + "/loot", 'PUT', payload);
|
||||
},
|
||||
sellItems (player_id, items) {
|
||||
const payload = { player_id, items };
|
||||
return this.__doFetch("players/sell", 'POST', payload);
|
||||
return this.__doFetch("players/" + player_id + "/loot", 'DELETE', payload);
|
||||
},
|
||||
newLoot (items) {
|
||||
return this.__doFetch("admin/add-loot", 'POST', items);
|
||||
|
||||
263
src/server.rs
263
src/server.rs
@@ -2,10 +2,10 @@ use actix_cors::Cors;
|
||||
use actix_files as fs;
|
||||
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
||||
use futures::Future;
|
||||
use lootalot_db::{DbApi, Pool, QueryResult};
|
||||
use lootalot_db::models::Item;
|
||||
use lootalot_db::{DbApi, Pool, QueryResult};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
type AppPool = web::Data<Pool>;
|
||||
|
||||
@@ -30,49 +30,94 @@ type AppPool = web::Data<Pool>;
|
||||
/// }
|
||||
/// )
|
||||
/// ```
|
||||
pub fn db_call<J,Q>(
|
||||
pool: AppPool,
|
||||
query: Q,
|
||||
) -> impl Future<Item=HttpResponse, Error=Error>
|
||||
where J: serde::ser::Serialize + Send + 'static,
|
||||
Q: Fn(DbApi) -> QueryResult<J> + Send + 'static,
|
||||
pub fn db_call<J, Q>(pool: AppPool, query: Q) -> impl Future<Item = HttpResponse, Error = Error>
|
||||
where
|
||||
J: serde::ser::Serialize + Send + 'static,
|
||||
Q: Fn(DbApi) -> QueryResult<J> + Send + 'static,
|
||||
{
|
||||
dbg!("db_call");
|
||||
let conn = pool.get().unwrap();
|
||||
web::block(move || {
|
||||
let api = DbApi::with_conn(&conn);
|
||||
query(api)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(players) => HttpResponse::Ok().json(players),
|
||||
Err(e) => {
|
||||
dbg!(&e);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
.then(|res| match res {
|
||||
Ok(r) => HttpResponse::Ok().json(r),
|
||||
Err(e) => {
|
||||
dbg!(&e);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
mod endpoints {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct PlayerClaim {
|
||||
player_id: i32,
|
||||
item_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct WealthUpdate {
|
||||
player_id: i32,
|
||||
value_in_gp: f32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct NewPlayer {
|
||||
pub name: String,
|
||||
pub wealth: f32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct LootUpdate {
|
||||
player_id: i32,
|
||||
items: Vec<(i32, Option<f32>)>,
|
||||
}
|
||||
|
||||
pub fn players_list(pool: AppPool) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
db_call(pool, move |api| api.fetch_players())
|
||||
}
|
||||
|
||||
pub fn player_loot(pool: AppPool, player_id: web::Path<i32>) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
db_call(pool, move |api| api.as_player(*player_id).loot())
|
||||
}
|
||||
|
||||
pub fn update_wealth(pool: AppPool, data: web::Json<WealthUpdate>) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
db_call(pool, move |api| {
|
||||
api.as_player(data.player_id)
|
||||
.update_wealth(data.value_in_gp)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct PlayerClaim {
|
||||
player_id: i32,
|
||||
item_id: i32,
|
||||
}
|
||||
pub fn buy_item(pool: AppPool, data: web::Json<LootUpdate>) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
db_call(pool, move |api| {
|
||||
api.as_player(data.player_id).buy(&data.items)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct WealthUpdate {
|
||||
player_id: i32,
|
||||
value_in_gp: f32,
|
||||
}
|
||||
pub fn sell_item(pool: AppPool, data: web::Json<LootUpdate>) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
db_call(pool, move |api| {
|
||||
api.as_player(data.player_id).sell(&data.items)
|
||||
})
|
||||
}
|
||||
pub fn player_claims(pool: AppPool) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
db_call(pool, move |api| api.fetch_claims())
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct NewPlayer {
|
||||
name: String,
|
||||
wealth: f32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct LootUpdate {
|
||||
player_id: i32,
|
||||
items: Vec<(i32, Option<f32>)>,
|
||||
pub fn put_claim(pool: AppPool, data: web::Json<PlayerClaim>) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
db_call(pool, move |api| {
|
||||
api.as_player(data.player_id).claim(data.item_id)
|
||||
})
|
||||
}
|
||||
pub fn delete_claim(pool: AppPool, data: web::Json<PlayerClaim>) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
db_call(pool, move |api| {
|
||||
api.as_player(data.player_id).unclaim(data.item_id)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn serve() -> std::io::Result<()> {
|
||||
@@ -91,104 +136,74 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
)
|
||||
.service(
|
||||
web::scope("/api")
|
||||
.route("/items", web::get().to_async(move |pool: AppPool| {
|
||||
db_call(pool, move |api| api.fetch_inventory())
|
||||
}))
|
||||
.service(
|
||||
web::scope("/players")
|
||||
.route(
|
||||
"/all",
|
||||
web::get().to_async(move |pool: AppPool| {
|
||||
db_call(pool, move |api| api
|
||||
.fetch_players())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/loot/{player_id}",
|
||||
web::get().to_async(move |pool: AppPool, player_id: web::Path<i32>| {
|
||||
db_call(pool, move |api| api.as_player(*player_id).loot())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/update-wealth",
|
||||
web::put().to_async(move |pool: AppPool, data: web::Json<WealthUpdate>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_player(data.player_id)
|
||||
.update_wealth(data.value_in_gp))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/buy",
|
||||
web::post().to_async(move |pool: AppPool, data: web::Json<LootUpdate>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_player(data.player_id)
|
||||
.buy(&data.items),
|
||||
.service( web::resource("/").route(web::get().to_async(endpoints::players_list))) // List of players
|
||||
//.route(web::put().to_async(endpoints::new_player)) // Create/Update player
|
||||
.service(
|
||||
web::scope("/{player_id}")
|
||||
//.route(web::get().to_async(...)) // Details of player
|
||||
.service(
|
||||
web::resource("/claims")
|
||||
//.route(web::get().to_async(endpoints::player_claims))
|
||||
.route(web::put().to_async(endpoints::put_claim))
|
||||
.route(web::delete().to_async(endpoints::delete_claim)),
|
||||
)
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/sell",
|
||||
web::post().to_async(move |pool: AppPool, data: web::Json<LootUpdate>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_player(data.player_id)
|
||||
.sell(&data.items),
|
||||
.service(
|
||||
web::resource("/wealth")
|
||||
//.route(web::get().to_async(...))
|
||||
.route(web::put().to_async(endpoints::update_wealth)),
|
||||
)
|
||||
}),
|
||||
)
|
||||
.service(
|
||||
web::resource("/loot")
|
||||
.route(web::get().to_async(endpoints::player_loot))
|
||||
.route(web::put().to_async(endpoints::buy_item))
|
||||
.route(web::delete().to_async(endpoints::sell_item)),
|
||||
),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/claims",
|
||||
web::get().to_async(endpoints::player_claims)
|
||||
)
|
||||
.route(
|
||||
"/items",
|
||||
web::get().to_async(move |pool: AppPool| {
|
||||
db_call(pool, move |api| api.fetch_inventory())
|
||||
}),
|
||||
)
|
||||
.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))
|
||||
}))
|
||||
)
|
||||
.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-loot",
|
||||
web::post().to_async(
|
||||
move |pool: AppPool, data: web::Json<Vec<Item>>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_admin()
|
||||
.add_loot(data.to_vec()),
|
||||
)
|
||||
}
|
||||
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",
|
||||
web::get().to_async(
|
||||
move |pool: AppPool, data: web::Json<NewPlayer>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_admin()
|
||||
.add_player(&data.name, data.wealth),
|
||||
)
|
||||
},
|
||||
.route(
|
||||
"/add-loot",
|
||||
web::post().to_async(
|
||||
move |pool: AppPool, data: web::Json<Vec<Item>>| {
|
||||
db_call(pool, move |api| {
|
||||
api.as_admin().add_loot(data.to_vec())
|
||||
})
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/add-player",
|
||||
web::get().to_async(
|
||||
move |pool: AppPool, data: web::Json<endpoints::NewPlayer>| {
|
||||
db_call(pool, move |api| {
|
||||
api.as_admin().add_player(&data.name, data.wealth)
|
||||
})
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
),
|
||||
)
|
||||
.service(fs::Files::new("/", www_root.clone()).index_file("index.html"))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user