Skip to content

Instantly share code, notes, and snippets.

@janinge
Created May 24, 2024 22:02
Show Gist options
  • Save janinge/401c0dca6776bae6638e13caa271f39f to your computer and use it in GitHub Desktop.
Save janinge/401c0dca6776bae6638e13caa271f39f to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate rocket;
use chrono::prelude::*;
use rocket::serde::{json::Json, Deserialize, Serialize};
use std::fmt;
use uuid::Uuid;
#[derive(Deserialize)]
struct AccountHolder {
id: Uuid,
first_name: String,
last_name: String,
address: Address,
email: String,
}
#[derive(Deserialize)]
struct Address {
street: String,
city: String,
state: State,
zip: String,
}
#[derive(Deserialize)]
struct SendMoneyRequest {
from: AccountHolder,
to: AccountHolder,
amount: f64,
send_on: DateTime<Utc>,
}
#[derive(Deserialize, Serialize)]
struct Receipt {
from_account: String,
to_account: String,
amount: f64,
created_on: DateTime<Utc>,
to_address: String,
}
#[derive(Serialize, Deserialize, Debug)]
enum State {
CA,
NY,
WA,
TX,
FL,
IL,
PA,
OH,
GA,
MI,
NC,
NJ,
VA,
}
#[get("/hi")]
fn index() -> &'static str {
"hi"
}
#[post("/send-money", data = "<request>")]
fn send_money(request: Json<SendMoneyRequest>) -> Json<Receipt> {
Json(Receipt {
from_account: format!("{} {}", request.from.first_name, request.from.last_name),
to_account: format!("{} {}", request.to.first_name, request.to.last_name),
amount: request.amount,
created_on: Utc::now(),
to_address: format!(
"{}, {}, {:?}, {}",
request.to.address.street,
request.to.address.city,
request.to.address.state,
request.to.address.zip
),
})
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, send_money])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment