moves api to its own module, thinking about design to choose
This commit is contained in:
@@ -13,24 +13,36 @@ pub mod schema;
|
||||
pub type DbConnection = SqliteConnection;
|
||||
pub type Pool = r2d2::Pool<ConnectionManager<DbConnection>>;
|
||||
pub type QueryResult<T> = Result<T, diesel::result::Error>;
|
||||
pub type ActionResult = QueryResult<bool>;
|
||||
|
||||
pub use models::Item;
|
||||
pub use models::Player;
|
||||
|
||||
impl Player {
|
||||
/// Query the list of all players
|
||||
pub fn fetch_list(conn: &SqliteConnection) -> QueryResult<Vec<Self>> {
|
||||
use schema::players::dsl::*;
|
||||
Ok(players.load::<Self>(conn)?)
|
||||
Ok( players
|
||||
.load::<Self>(conn)?
|
||||
)
|
||||
}
|
||||
|
||||
pub fn fetch_chest(player_id: i32, conn: &SqliteConnection) -> QueryResult<Vec<Item>> {
|
||||
/// Query the list of items belonging to player
|
||||
pub fn fetch_loot(player_id: i32, conn: &SqliteConnection) -> QueryResult<Vec<Item>> {
|
||||
use schema::looted::dsl::*;
|
||||
let owned = looted
|
||||
.filter(owner_id.eq(player_id))
|
||||
.select((id, name, base_price))
|
||||
.load::<Item>(conn)?;
|
||||
dbg!(&owned);
|
||||
Ok(owned)
|
||||
Ok( looted
|
||||
.filter(owner_id.eq(player_id))
|
||||
.select((id, name, base_price))
|
||||
.load::<Item>(conn)?
|
||||
)
|
||||
}
|
||||
|
||||
pub fn action_claim_object(player_id: i32, item_id: i32, conn: &DbConnection) -> ActionResult {
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
pub fn action_withdraw_claim(player_id: i32, item_id: i32, conn: &DbConnection) -> ActionResult {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
83
src/api.rs
Normal file
83
src/api.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use actix_web::{web, web::Json, Error, HttpResponse, Result};
|
||||
use futures::Future;
|
||||
|
||||
use lootalot_db::{Pool};
|
||||
|
||||
// Wrapper for database queries.
|
||||
//fn db_query(q: fn(...)) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
//
|
||||
//}
|
||||
|
||||
mod player {
|
||||
use lootalot_db::{Player as PlayerData, Item, ActionResult, QueryResult, DbConnection};
|
||||
|
||||
/// A reference to a player in database
|
||||
pub(super) struct Player(pub(super) i32);
|
||||
|
||||
impl Player {
|
||||
/// Fetch all players from db
|
||||
pub(super) fn all(conn: &DbConnection) -> QueryResult<Vec<PlayerData>> {
|
||||
PlayerData::fetch_list(conn)
|
||||
}
|
||||
/// Fetch the content of a player's chest
|
||||
pub(super) fn loot(self, conn: &DbConnection) -> QueryResult<Vec<Item>> {
|
||||
PlayerData::fetch_loot(self.0, conn)
|
||||
}
|
||||
pub(super) fn claim(self, item: i32, conn: &DbConnection) -> ActionResult {
|
||||
PlayerData::action_claim_object(self.0, item, conn)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn players_list(
|
||||
pool: web::Data<Pool>
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
web::block(move || {
|
||||
let conn = pool.get().unwrap();
|
||||
player::Player::all(&conn)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(players) => {
|
||||
HttpResponse::Ok().json(players)
|
||||
}
|
||||
Err(_) => {
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn chest_content(
|
||||
player_id: web::Path<i32>,
|
||||
pool: web::Data<Pool>
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
web::block(move || {
|
||||
let conn = pool.get().unwrap();
|
||||
player::Player(*player_id).loot(&conn)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(items) => HttpResponse::Ok().json(items),
|
||||
Err(_) => HttpResponse::InternalServerError().finish(),
|
||||
})
|
||||
}
|
||||
|
||||
fn put_request(
|
||||
params: web::Path<(i32, i32)>,
|
||||
pool: web::Data<Pool>
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
web::block(move || {
|
||||
let (player_id, item_id) = *params;
|
||||
let conn = pool.get().unwrap();
|
||||
player::Player(player_id).claim(item_id, &conn)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(status) => HttpResponse::Ok().json(status),
|
||||
Err(_) => HttpResponse::InternalServerError().finish(),
|
||||
})
|
||||
}
|
||||
|
||||
fn withdraw_request(_player_id: i32, _item_id: i32) -> Result<Json<bool>> {
|
||||
Ok(Json(false))
|
||||
}
|
||||
72
src/main.rs
72
src/main.rs
@@ -6,79 +6,9 @@ extern crate lootalot_db;
|
||||
|
||||
use actix_files as fs;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use lootalot_db::Pool;
|
||||
use std::env;
|
||||
|
||||
mod api {
|
||||
use actix_web::{web, web::Json, Error, HttpResponse, Result};
|
||||
use futures::Future;
|
||||
|
||||
use super::Pool;
|
||||
use lootalot_db::Player as PlayerData;
|
||||
use lootalot_db::{Item, QueryResult, DbConnection};
|
||||
|
||||
// Wrapper for database queries.
|
||||
//fn db_query(q: fn(...)) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
//
|
||||
//}
|
||||
|
||||
struct Player(i32);
|
||||
|
||||
impl Player {
|
||||
|
||||
/// Fetch all players from db
|
||||
fn fetch_list(conn: &DbConnection) -> QueryResult<Vec<PlayerData>> {
|
||||
PlayerData::fetch_list(conn)
|
||||
}
|
||||
/// Fetch the content of a player's chest
|
||||
fn chest(self, conn: &DbConnection) -> QueryResult<Vec<Item>> {
|
||||
PlayerData::fetch_chest(self.0, conn)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn players_list(
|
||||
pool: web::Data<Pool>
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
web::block(move || {
|
||||
let conn = pool.get().unwrap();
|
||||
println!("Waiting for player list...");
|
||||
Player::fetch_list(&conn)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(players) => {
|
||||
println!("Ok! {:?}", &players);
|
||||
HttpResponse::Ok().json(players)
|
||||
}
|
||||
Err(_) => {
|
||||
println!("Error!");
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn chest_content(
|
||||
player_id: web::Path<i32>,
|
||||
pool: web::Data<Pool>
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
web::block(move || {
|
||||
let conn = pool.get().unwrap();
|
||||
dbg!("Fetching chest content...");
|
||||
Player(*player_id).chest(&conn)
|
||||
})
|
||||
.then(|res| match res {
|
||||
Ok(items) => HttpResponse::Ok().json(items),
|
||||
Err(_) => HttpResponse::InternalServerError().finish(),
|
||||
})
|
||||
}
|
||||
|
||||
fn put_request(_player_id: i32, _item_id: i32) -> Result<Json<bool>> {
|
||||
Ok(Json(false))
|
||||
}
|
||||
|
||||
fn withdraw_request(_player_id: i32, _item_id: i32) -> Result<Json<bool>> {
|
||||
Ok(Json(false))
|
||||
}
|
||||
}
|
||||
mod api;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
println!("Hello, world!");
|
||||
|
||||
Reference in New Issue
Block a user