Skip to content

Commit

Permalink
ajout des route login et create_user
Browse files Browse the repository at this point in the history
  • Loading branch information
thybux committed Apr 26, 2024
1 parent 405db7d commit 004725b
Showing 1 changed file with 70 additions and 9 deletions.
79 changes: 70 additions & 9 deletions services/eat/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use common::db::Db;
use rocket::http::Status;
use rocket::serde::json::Json;
use rocket::serde::Deserialize;
use serde_json::Value;
use rocket::serde::json::Json;

#[macro_use]
extern crate rocket;
Expand All @@ -15,12 +17,25 @@ struct CityCoord {
long: f64,
}

#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
struct AccountCreationData {
email: String,
password: String,
first_name: String,
last_name: String,
}

#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
struct AuthCredentials {
email: String,
password: String,
}

impl CityCoord {
fn new(lat: f64, long: f64) -> Self {
Self {
lat,
long
}
Self { lat, long }
}
}

Expand All @@ -35,6 +50,43 @@ async fn coord(localisation: String) -> Json<CityCoord> {
Json(result)
}

#[post("/login/auth", format = "application/json", data = "<credentials>")]
async fn auth(
credentials: Json<AuthCredentials>,
db: &rocket::State<Db>,
) -> Result<Status, Status> {
match db.login(&credentials.email, &credentials.password).await {
Ok(true) => Ok(Status::Ok),
Ok(false) => Err(Status::Forbidden),
Err(_) => Err(Status::InternalServerError),
}
}

#[post(
"/create_account",
format = "application/json",
data = "<account_data>"
)]
async fn create_account_route(
account_data: Json<AccountCreationData>,
db: &rocket::State<Db>,
) -> Result<Status, Status> {
let result = db
.create_account(
&account_data.email,
&account_data.password,
&account_data.first_name,
&account_data.last_name,
)
.await;

match result {
Ok(true) => Ok(Status::Created),
Ok(false) => Err(Status::Conflict),
Err(_) => Err(Status::InternalServerError),
}
}

// URL pour récupérer les restaurant dans un périmétre donné et pour une localisation donnée
#[get("/service/eat/<localisation>/<radius>")]
async fn index(localisation: String, radius: i32) -> Json<Vec<Emplacement>> {
Expand All @@ -43,9 +95,17 @@ async fn index(localisation: String, radius: i32) -> Json<Vec<Emplacement>> {
Json(result)
}

#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, coord])
#[rocket::main]
async fn main() {
let db = Db::new().await.expect("Failed to create database pool");
let rocket_instance = rocket::build()
.manage(db)
.mount("/", routes![index, coord, auth, create_account_route]);

match rocket_instance.launch().await {
Ok(_) => println!("Server launched successfully."),
Err(e) => println!("Server failed to launch: {:?}", e),
}
}

pub(crate) async fn get_google(localisation: String, radius: i32) -> Value {
Expand Down Expand Up @@ -85,7 +145,8 @@ mod tests {
let expected = exploit_json(get_google(String::from("Le Bardon"), 1000).await).unwrap();

// Convertir `result` et `expected` en JSON String pour la comparaison
let result_str = serde_json::to_string(&result.into_inner()).expect("Failed to serialize result");
let result_str =
serde_json::to_string(&result.into_inner()).expect("Failed to serialize result");
let expected_str = serde_json::to_string(&expected).expect("Failed to serialize expected");

// Comparer les chaînes JSON
Expand Down

0 comments on commit 004725b

Please sign in to comment.