adds documentation
This commit is contained in:
54
src/api.rs
54
src/api.rs
@@ -1,25 +1,46 @@
|
|||||||
use actix_web::{web, web::Json, Error, HttpResponse, Result};
|
use actix_web::{web, web::Json, Error, HttpResponse, Result};
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
use lootalot_db::Pool;
|
use lootalot_db::{Pool, DbConnection, QueryResult, ActionResult};
|
||||||
use lootalot_db::{ActionResult, DbConnection, Item, Player, QueryResult};
|
use lootalot_db::{Item, Player};
|
||||||
|
|
||||||
|
/// A wrapper providing an API over the database
|
||||||
|
/// It offers a convenient way to deal with connection
|
||||||
pub struct DbApi<'q>(&'q DbConnection);
|
pub struct DbApi<'q>(&'q DbConnection);
|
||||||
|
|
||||||
impl<'q> DbApi<'q> {
|
impl<'q> DbApi<'q> {
|
||||||
|
/// Returns a DbApi using the user given connection
|
||||||
|
///
|
||||||
|
/// # Usage
|
||||||
|
/// ```
|
||||||
|
/// let conn = DbConnection::establish();
|
||||||
|
/// let api = DbApi::with_conn(&conn);
|
||||||
|
/// ```
|
||||||
pub fn with_conn(conn: &'q DbConnection) -> Self {
|
pub fn with_conn(conn: &'q DbConnection) -> Self {
|
||||||
Self(conn)
|
Self(conn)
|
||||||
}
|
}
|
||||||
|
/// Fetch the list of all players
|
||||||
|
///
|
||||||
|
/// This method consumes the DbApi object.
|
||||||
pub fn fetch_players(self) -> QueryResult<Vec<Player>> {
|
pub fn fetch_players(self) -> QueryResult<Vec<Player>> {
|
||||||
Player::fetch_list(self.0)
|
Player::fetch_list(self.0)
|
||||||
}
|
}
|
||||||
|
/// Wrapper for acting as a specific player
|
||||||
|
///
|
||||||
|
/// The DbApi is moved inside a new AsPlayer object.
|
||||||
|
///
|
||||||
|
/// # Usage
|
||||||
|
/// ```
|
||||||
|
/// let player_id: i32 = 1; // Id that references player in DB
|
||||||
|
/// let player = api.as_player(player_id);
|
||||||
|
/// ```
|
||||||
pub fn as_player(self, id: i32) -> AsPlayer<'q> {
|
pub fn as_player(self, id: i32) -> AsPlayer<'q> {
|
||||||
AsPlayer { id, conn: self.0 }
|
AsPlayer { id, conn: self.0 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A wrapper for interactions of players with the database
|
||||||
|
/// Possible actions are exposed as methods
|
||||||
pub struct AsPlayer<'q> {
|
pub struct AsPlayer<'q> {
|
||||||
id: i32,
|
id: i32,
|
||||||
conn: &'q DbConnection,
|
conn: &'q DbConnection,
|
||||||
@@ -30,6 +51,7 @@ impl<'q> AsPlayer<'q> {
|
|||||||
pub fn loot(self) -> QueryResult<Vec<Item>> {
|
pub fn loot(self) -> QueryResult<Vec<Item>> {
|
||||||
Player::fetch_loot(self.id, self.conn)
|
Player::fetch_loot(self.id, self.conn)
|
||||||
}
|
}
|
||||||
|
/// Put a claim on a specific item
|
||||||
pub fn claim(self, item: i32) -> ActionResult {
|
pub fn claim(self, item: i32) -> ActionResult {
|
||||||
Player::action_claim_object(self.id, item, self.conn)
|
Player::action_claim_object(self.id, item, self.conn)
|
||||||
}
|
}
|
||||||
@@ -49,9 +71,27 @@ impl<'q> AsPlayer<'q> {
|
|||||||
// .resolve_claims()
|
// .resolve_claims()
|
||||||
//
|
//
|
||||||
|
|
||||||
// fn db_call(f: fn(DbApi) -> ApiResponse) { ... }
|
/// Wraps call to the DbApi and process its result as a async HttpResponse
|
||||||
// ApiResponse needs to be Serialize
|
///
|
||||||
|
/// Provides a convenient way to call the api inside a route definition. Given a connection pool,
|
||||||
|
/// access to the api is granted in a closure. The closure is called in a blocking way and should
|
||||||
|
/// return a QueryResult.
|
||||||
|
/// If the query succeeds, it's result is returned as JSON data. Otherwise, an InternalServerError
|
||||||
|
/// is returned.
|
||||||
|
///
|
||||||
|
/// # Usage
|
||||||
|
/// ```
|
||||||
|
/// (...)
|
||||||
|
/// .route("path/to/",
|
||||||
|
/// move |pool: web::Data<Pool>| {
|
||||||
|
/// // user data can be processed here
|
||||||
|
/// // ...
|
||||||
|
/// db_call(pool, move |api| {
|
||||||
|
/// // ...do what you want with the api
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// )
|
||||||
|
/// ```
|
||||||
pub fn db_call<
|
pub fn db_call<
|
||||||
J: serde::ser::Serialize + Send + 'static,
|
J: serde::ser::Serialize + Send + 'static,
|
||||||
Q: Fn(DbApi) -> QueryResult<J> + Send + 'static,
|
Q: Fn(DbApi) -> QueryResult<J> + Send + 'static,
|
||||||
|
|||||||
@@ -9,5 +9,9 @@ mod server;
|
|||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
|
// TODO: Build a cli app with complete support of DbApi
|
||||||
|
// Server is started with 'serve' subcommand
|
||||||
|
// $ lootalot serve
|
||||||
|
// Start the server.
|
||||||
server::serve();
|
server::serve();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user