Skip to content

Commit

Permalink
Update and rebase
Browse files Browse the repository at this point in the history
* Change versions in `Cargo.toml`
* Use the new versions of the functions
  • Loading branch information
n-pochet committed Nov 8, 2018
1 parent 727a36f commit bde0f88
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ members = [
# static_assets
"examples/static_assets",

# diesel example
"examples/diesel",

# example_contribution_template
"examples/example_contribution_template/name"
]
Expand Down
20 changes: 10 additions & 10 deletions examples/diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ publish = false
[dependencies]
gotham = { path = "../../gotham" }
gotham_derive = { path = "../../gotham_derive" }
gotham_middleware_diesel = { path = "../../middleware/diesel" }
gotham_middleware_diesel = { path = "../../middleware/under_development/diesel" }

hyper = "0.11"
futures = "~0.1.11"
hyper = "0.12"
futures = "0.1"
mime = "0.3"
log = "0.3"
diesel = { version = "1.0.0", features = ["sqlite"] }
diesel_migrations = { version = "1.0.0", features = ["sqlite"] }
r2d2 = "0.8.2"
r2d2-diesel = "1.0.0"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
diesel = { version = "1", features = ["sqlite"] }
diesel_migrations = { version = "1", features = ["sqlite"] }
r2d2 = "0.8"
r2d2-diesel = "1"
serde = "1"
serde_json = "1"
serde_derive = "1"
2 changes: 1 addition & 1 deletion examples/diesel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module holds the functions to get and create products from the DB.

pub mod schema;
pub mod models;
pub mod schema;

#[macro_use]
extern crate diesel;
Expand Down
56 changes: 23 additions & 33 deletions examples/diesel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ extern crate r2d2;
extern crate r2d2_diesel;
extern crate serde_json;

use hyper::{Response, StatusCode};
use gotham::state::{FromState, State};
use gotham::router::Router;
use basic_diesel::models::NewProduct;
use diesel::sqlite::SqliteConnection;
use futures::{future, Future, Stream};
use gotham::handler::HandlerFuture;
use gotham::handler::IntoHandlerError;
use gotham::helpers::http::response::create_response;
use gotham::pipeline::new_pipeline;
use gotham::pipeline::single::single_pipeline;
use gotham::router::builder::*;
use gotham::router::route::dispatch::{finalize_pipeline_set, new_pipeline_set};
use gotham::handler::HandlerFuture;
use gotham::http::response::create_response;
use gotham::router::Router;
use gotham::state::{FromState, State};
use gotham_middleware_diesel::DieselMiddleware;
use gotham::handler::IntoHandlerError;
use diesel::sqlite::SqliteConnection;
use r2d2_diesel::ConnectionManager;
use hyper::{Body, Response, StatusCode};
use r2d2::{Pool, PooledConnection};
use futures::{future, Future, Stream};
use r2d2_diesel::ConnectionManager;
use std::str;
use basic_diesel::models::NewProduct;

// The URL of the database.
static DATABASE_URL: &'static str = "products.db";
Expand All @@ -38,17 +38,18 @@ fn create_middleware(url: &str) -> DieselMiddleware<SqliteConnection> {
}

/// Handler function. Responsible of getting and displaying the products from the DB
fn get_products_handler(state: State) -> (State, Response) {
fn get_products_handler(state: State) -> (State, Response<Body>) {
let conn: PooledConnection<ConnectionManager<SqliteConnection>> =
gotham_middleware_diesel::state_data::connection(&state);
let products = basic_diesel::get_products(&conn);

(
state,
Response::new()
.with_status(StatusCode::Ok)
.with_body(format!("{}", serde_json::to_string(&products).unwrap())),
)
let response = create_response(
&state,
StatusCode::OK,
mime::APPLICATION_JSON,
serde_json::to_string(&products).expect("serialized product"),
);
(state, response)
}

/// Handle function. Manages the `NewProduct` to insert to the DB
Expand All @@ -63,19 +64,15 @@ fn post_product_handler(mut state: State) -> Box<HandlerFuture> {
};
let conn: PooledConnection<ConnectionManager<SqliteConnection>> =
gotham_middleware_diesel::state_data::connection(&state);
let mut res: Response;
let mut res: Response<Body>;
match basic_diesel::create_product(
&conn,
product.title,
product.price,
product.link,
) {
Ok(_) => {
res = create_response(
&state,
StatusCode::Created,
Some((vec![], mime::TEXT_PLAIN)),
)
res = create_response(&state, StatusCode::CREATED, mime::TEXT_PLAIN, vec![])
}
Err(e) => return future::err((state, e.into_handler_error())),
}
Expand All @@ -96,18 +93,11 @@ fn post_product_handler(mut state: State) -> Box<HandlerFuture> {
/// It returns the content of the SQLite DB file located in `products.db`
/// This DB consists of `Products` entries.
fn router(middleware: DieselMiddleware<SqliteConnection>) -> Router {
// Create a new pipeline set
let editable_pipeline_set = new_pipeline_set();

// Add the middleware to a new pipeline
let (editable_pipeline_set, pipeline) =
editable_pipeline_set.add(new_pipeline().add(middleware).build());
let pipeline_set = finalize_pipeline_set(editable_pipeline_set);

let default_pipeline_chain = (pipeline, ());
let (chain, pipeline) = single_pipeline(new_pipeline().add(middleware).build());

// Build the router
build_router(default_pipeline_chain, pipeline_set, |route| {
build_router(chain, pipeline, |route| {
route.get("/").to(get_products_handler);
route.post("/").to(post_product_handler);
})
Expand Down

0 comments on commit bde0f88

Please sign in to comment.