Created
July 19, 2018 05:58
-
-
Save timxor/3fcf0379c60fe746a34a996bd4335be8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(plugin)] | |
#![plugin(rocket_codegen)] | |
extern crate rocket; | |
extern crate dotenv; | |
#[macro_use] extern crate rocket_contrib; | |
#[macro_use] extern crate diesel; | |
extern crate payment_api; | |
extern crate r2d2; | |
extern crate r2d2_diesel; | |
mod db; | |
mod schema; | |
use diesel::prelude::*; | |
use payment_api::*; | |
use payment_api::models::User; | |
use rocket_contrib::{Json, Value}; | |
#[post("/", data = "<user>")] | |
fn create(user: Json<User>, connection: db::Connection) -> Json<User> { | |
let insert = User { id: None, ..user.into_inner() }; | |
Json(User::create(insert, &connection)) | |
} | |
fn new_user(first_name: &str) -> User { | |
let conn = establish_connection(); | |
let new_user = create_user(&conn, first_name); | |
println!("Saved person: {}", new_user.first_name); | |
new_user | |
} | |
#[get("/")] | |
fn read(connection: db::Connection) -> Json<Value> { | |
Json(json!(User::read(&connection))) | |
} | |
#[put("/<id>", data = "<user>")] | |
fn update(id: i32, user: Json<User>, connection: db::Connection) -> Json<Value> { | |
// todo fix this, update was not working, delete is not right | |
let update = User { id: Some(id), ..user.into_inner() }; | |
Json(json!({ | |
"success": User::delete(id, &connection) | |
})) | |
} | |
#[delete("/<id>")] | |
fn delete(id: i32, connection: db::Connection) -> Json<Value> { | |
Json(json!({ | |
"success": User::delete(id, &connection) | |
})) | |
} | |
fn main() { | |
rocket::ignite() | |
.mount("/user", routes![create, update, delete]) | |
.mount("/users", routes![read]) | |
.manage(db::connect()) | |
.launch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment