thoughts on new api structure, formats code
This commit is contained in:
@@ -2,8 +2,8 @@ use actix_cors::Cors;
|
||||
use actix_files as fs;
|
||||
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
||||
use futures::Future;
|
||||
use lootalot_db::models::Item;
|
||||
use lootalot_db::{DbApi, Pool, QueryResult};
|
||||
use lootalot_db::{Item, LootManager};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
|
||||
@@ -33,14 +33,10 @@ 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,
|
||||
Q: Fn(DbConnection) -> QueryResult<J> + Send + 'static,
|
||||
{
|
||||
let conn = pool.get().unwrap();
|
||||
web::block(move || {
|
||||
let api = DbApi::with_conn(&conn);
|
||||
query(api)
|
||||
})
|
||||
.then(|res| match res {
|
||||
web::block(move || query(conn)).then(|res| match res {
|
||||
Ok(r) => HttpResponse::Ok().json(r),
|
||||
Err(e) => {
|
||||
dbg!(&e);
|
||||
@@ -77,42 +73,58 @@ mod endpoints {
|
||||
items: Vec<(i32, Option<f32>)>,
|
||||
}
|
||||
|
||||
pub fn players_list(pool: AppPool) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
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 player_loot(
|
||||
pool: AppPool,
|
||||
player_id: web::Path<i32>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
db_call(pool, move |conn| LootManager(&conn, *player_id).all())
|
||||
}
|
||||
|
||||
pub fn update_wealth(pool: AppPool, data: web::Json<WealthUpdate>) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn buy_item(pool: AppPool, data: web::Json<LootUpdate>) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn sell_item(pool: AppPool, data: web::Json<LootUpdate>) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
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>{
|
||||
pub fn player_claims(pool: AppPool) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
db_call(pool, move |api| api.fetch_claims())
|
||||
}
|
||||
|
||||
pub fn put_claim(pool: AppPool, (player, loot): (web::Path<i32>, web::Json<PlayerClaim>)) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
db_call(pool, move |api| {
|
||||
api.as_player(*player).claim(loot.item_id)
|
||||
})
|
||||
pub fn put_claim(
|
||||
pool: AppPool,
|
||||
(player, loot): (web::Path<i32>, web::Json<PlayerClaim>),
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
db_call(pool, move |api| api.as_player(*player).claim(loot.item_id))
|
||||
}
|
||||
pub fn delete_claim(pool: AppPool, (player, data): (web::Path<i32>, web::Json<PlayerClaim>)) -> impl Future<Item = HttpResponse, Error = Error>{
|
||||
pub fn delete_claim(
|
||||
pool: AppPool,
|
||||
(player, data): (web::Path<i32>, web::Json<PlayerClaim>),
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
db_call(pool, move |api| {
|
||||
api.as_player(*player).unclaim(data.item_id)
|
||||
})
|
||||
@@ -137,7 +149,10 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
web::scope("/api")
|
||||
.service(
|
||||
web::scope("/players")
|
||||
.service( web::resource("/").route(web::get().to_async(endpoints::players_list))) // List of players
|
||||
.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}")
|
||||
@@ -161,10 +176,7 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/claims",
|
||||
web::get().to_async(endpoints::player_claims)
|
||||
)
|
||||
.route("/claims", web::get().to_async(endpoints::player_claims))
|
||||
.route(
|
||||
"/items",
|
||||
web::get().to_async(move |pool: AppPool| {
|
||||
|
||||
Reference in New Issue
Block a user