From 3a4f1b72de1ee7f367e8e5131d8bdd2c7473e59e Mon Sep 17 00:00:00 2001 From: VIGNESH S Date: Tue, 30 Apr 2024 23:23:13 +0530 Subject: [PATCH] backend/fix: Added user entity in auth response --- Backend/diesel.toml | 2 +- Backend/src/db/schema.rs | 5 +-- Backend/src/handlers/auth/auth_verify.rs | 25 +++++++-------- Backend/src/handlers/auth/mod.rs | 2 +- Backend/src/handlers/geometry/mod.rs | 2 +- .../src/handlers/geometry/serviceability.rs | 20 ++++++------ Backend/src/handlers/mod.rs | 2 +- Backend/src/main.rs | 2 +- Backend/src/routes/auth.rs | 4 ++- Backend/src/routes/mod.rs | 2 +- Backend/src/routes/routes.rs | 2 +- Backend/src/routes/serviceability.rs | 13 +++++--- Backend/src/storage/models/auth.rs | 3 +- Backend/src/storage/models/geometry.rs | 14 ++++---- Backend/src/storage/models/mod.rs | 2 +- Backend/src/storage/models/user.rs | 9 ++++-- Backend/src/tools/contants.rs | 3 +- Backend/src/tools/mod.rs | 6 ++-- Backend/src/tools/polygon_utils/geo_json.rs | 11 +++++-- Backend/src/tools/session_utils.rs | 32 ++++++++----------- Backend/src/tools/types.rs | 21 ++++-------- README.md | 3 ++ 22 files changed, 96 insertions(+), 89 deletions(-) diff --git a/Backend/diesel.toml b/Backend/diesel.toml index 5eb5dbf..42edafc 100644 --- a/Backend/diesel.toml +++ b/Backend/diesel.toml @@ -6,4 +6,4 @@ file = "src/db/schema.rs" schema = "school_trips" [migrations_directory] -dir = "../../migrations" +dir = "./migrations" diff --git a/Backend/src/db/schema.rs b/Backend/src/db/schema.rs index 4ded4e2..5c3306f 100644 --- a/Backend/src/db/schema.rs +++ b/Backend/src/db/schema.rs @@ -42,8 +42,5 @@ pub mod school_trips { } } - diesel::allow_tables_to_appear_in_same_query!( - auth, - users, - ); + diesel::allow_tables_to_appear_in_same_query!(auth, users,); } diff --git a/Backend/src/handlers/auth/auth_verify.rs b/Backend/src/handlers/auth/auth_verify.rs index 883af30..4b7e31b 100644 --- a/Backend/src/handlers/auth/auth_verify.rs +++ b/Backend/src/handlers/auth/auth_verify.rs @@ -1,6 +1,5 @@ use crate::storage::models; use crate::storage::models::auth::{Auth, VerifyAuthResponse}; -use crate::storage::models::geometry::ServiceabilityRequest; use crate::storage::models::user::User; use crate::storage::result::QueryResult; use crate::tools::contants::ONEWEEK; @@ -10,7 +9,6 @@ use crate::ServerState; use actix_web::web::{self}; use actix_web::HttpResponse; - pub fn handle_verify_auth( state: web::Data, req: web::Json, @@ -36,7 +34,12 @@ pub fn handle_verify_auth( ..register_user }; response = VerifyAuthResponse { - session_token : SessionToken::new(upadated_user.id.clone(), get_exipry_from_minutes(ONEWEEK).timestamp()).encode(state.config.jwt_secret.clone()) + session_token: SessionToken::new( + upadated_user.id.clone(), + get_exipry_from_minutes(ONEWEEK).timestamp(), + ) + .encode(state.config.jwt_secret.clone()), + user: upadated_user.to_owned() }; upadated_user.update(&state.data.pool); } @@ -50,7 +53,12 @@ pub fn handle_verify_auth( role: auth.role.to_owned(), }; response = VerifyAuthResponse { - session_token : SessionToken::new(user.id.clone(), get_exipry_from_minutes(ONEWEEK).timestamp()).encode(state.config.jwt_secret.clone()) + session_token: SessionToken::new( + user.id.clone(), + get_exipry_from_minutes(ONEWEEK).timestamp(), + ) + .encode(state.config.jwt_secret.clone()), + user: user.to_owned() }; user.insert(&state.data.pool); } @@ -70,12 +78,3 @@ pub fn handle_verify_auth( _ => HttpResponse::InternalServerError().body("INTERNAL_ERROR"), } } - - -pub fn origin_serviceability( - state: web::Data, - req: web::Json, -) -> HttpResponse { - // geo::Polygon:: - return HttpResponse::InternalServerError().body("INTERNAL_ERROR"); -} diff --git a/Backend/src/handlers/auth/mod.rs b/Backend/src/handlers/auth/mod.rs index 0aadfc8..9faa8de 100644 --- a/Backend/src/handlers/auth/mod.rs +++ b/Backend/src/handlers/auth/mod.rs @@ -1,2 +1,2 @@ pub mod auth; -pub mod auth_verify; \ No newline at end of file +pub mod auth_verify; diff --git a/Backend/src/handlers/geometry/mod.rs b/Backend/src/handlers/geometry/mod.rs index 3ae75e8..69ceacc 100644 --- a/Backend/src/handlers/geometry/mod.rs +++ b/Backend/src/handlers/geometry/mod.rs @@ -1 +1 @@ -pub mod serviceability; \ No newline at end of file +pub mod serviceability; diff --git a/Backend/src/handlers/geometry/serviceability.rs b/Backend/src/handlers/geometry/serviceability.rs index da8a71e..83739fb 100644 --- a/Backend/src/handlers/geometry/serviceability.rs +++ b/Backend/src/handlers/geometry/serviceability.rs @@ -1,17 +1,19 @@ -use geo::Contains; - -use crate::{storage::models::geometry::ServiceabilityRequest, tools::polygon_utils::geo_json::get_available_shape_files, web, HttpResponse, ServerState}; +use crate::{ + storage::models::geometry::ServiceabilityRequest, + tools::polygon_utils::geo_json::get_available_shape_files, web, HttpResponse, ServerState, +}; +use geo::{coord, Intersects}; pub fn origin_serviceability( - state: web::Data, + _: web::Data, req: web::Json, ) -> HttpResponse { let areas = get_available_shape_files(); - let mut result = false; for area in areas { - if (area.get_polygon().contains(&req.point)) { - result = true; - } + return HttpResponse::Ok().json( + area.get_polygon() + .intersects(&coord! {x: req.point.lon, y: req.point.lat}), + ); } - return HttpResponse::Ok().json(result); + return HttpResponse::Ok().json(false); } diff --git a/Backend/src/handlers/mod.rs b/Backend/src/handlers/mod.rs index a5602eb..2939e69 100644 --- a/Backend/src/handlers/mod.rs +++ b/Backend/src/handlers/mod.rs @@ -1,3 +1,3 @@ pub mod auth; pub mod error_handler; -pub mod geometry; \ No newline at end of file +pub mod geometry; diff --git a/Backend/src/main.rs b/Backend/src/main.rs index a9c20a1..ed52dd7 100644 --- a/Backend/src/main.rs +++ b/Backend/src/main.rs @@ -7,9 +7,9 @@ mod db; mod handlers; mod routes; mod storage; +mod tools; mod transformers; mod types; -mod tools; #[derive(serde::Deserialize, serde::Serialize)] pub struct Response { diff --git a/Backend/src/routes/auth.rs b/Backend/src/routes/auth.rs index bb97e3e..8a5bdba 100644 --- a/Backend/src/routes/auth.rs +++ b/Backend/src/routes/auth.rs @@ -1,5 +1,7 @@ use crate::{ - handlers::auth::{auth::handle_auth, auth_verify::handle_verify_auth}, storage::models, ServerState + handlers::auth::{auth::handle_auth, auth_verify::handle_verify_auth}, + storage::models, + ServerState, }; use actix_web::{post, web, HttpResponse}; diff --git a/Backend/src/routes/mod.rs b/Backend/src/routes/mod.rs index 3e556bf..a51c7dd 100644 --- a/Backend/src/routes/mod.rs +++ b/Backend/src/routes/mod.rs @@ -1,4 +1,4 @@ pub mod auth; pub mod routes; +pub mod serviceability; pub mod session; -pub mod serviceability; \ No newline at end of file diff --git a/Backend/src/routes/routes.rs b/Backend/src/routes/routes.rs index 8496b12..295f24c 100644 --- a/Backend/src/routes/routes.rs +++ b/Backend/src/routes/routes.rs @@ -7,6 +7,6 @@ pub fn add_all_routes(cfg: &mut web::ServiceConfig) { .service(super::auth::create_auth) .service(super::auth::verify_auth) .service(super::session::verify_session) - .service(super::serviceability::serviceability) + .service(super::serviceability::serviceability), ); } diff --git a/Backend/src/routes/serviceability.rs b/Backend/src/routes/serviceability.rs index 3c8cb57..38e32da 100644 --- a/Backend/src/routes/serviceability.rs +++ b/Backend/src/routes/serviceability.rs @@ -1,10 +1,15 @@ -use actix_web::{get, http::header::AUTHORIZATION, web, HttpRequest, HttpResponse}; +use actix_web::{get, web, HttpResponse}; use crate::{ - handlers::geometry::serviceability::origin_serviceability, storage::models, tools::{session_utils::SessionToken, utils::get_token_from_bearer}, types::ServerState + handlers::geometry::serviceability::origin_serviceability, + storage::models, + types::ServerState, }; #[get("origin/serviceability")] -pub async fn serviceability(state: web::Data, req: web::Json,) -> HttpResponse { - return origin_serviceability(state,req); +pub async fn serviceability( + state: web::Data, + req: web::Json, +) -> HttpResponse { + return origin_serviceability(state, req); } diff --git a/Backend/src/storage/models/auth.rs b/Backend/src/storage/models/auth.rs index a56ecb6..2802689 100644 --- a/Backend/src/storage/models/auth.rs +++ b/Backend/src/storage/models/auth.rs @@ -3,7 +3,7 @@ use chrono::NaiveDateTime; use diesel::prelude::*; use serde::{Deserialize, Serialize}; -use super::user::Role; +use super::user::{Role, User}; /// Auth table sturct #[derive(AsChangeset, Queryable, Insertable)] @@ -41,6 +41,7 @@ pub struct VerifyAuthRequest { #[derive(Deserialize, Serialize)] pub struct VerifyAuthResponse { pub session_token: String, + pub user: User, } // TODO Add User creation #[derive(Deserialize, Serialize)] diff --git a/Backend/src/storage/models/geometry.rs b/Backend/src/storage/models/geometry.rs index 9115953..1752e84 100644 --- a/Backend/src/storage/models/geometry.rs +++ b/Backend/src/storage/models/geometry.rs @@ -1,16 +1,18 @@ -use diesel::sql_types::Bool; -use geo::{coord, Coord}; use serde::{Deserialize, Serialize}; - +#[derive(Deserialize, Serialize)] +pub struct ServiceabilityRequest { + pub point: LatLong, +} #[derive(Deserialize, Serialize)] -pub struct ServiceabilityRequest { - pub point : Coord, +pub struct LatLong { + pub lat: f64, + pub lon: f64, } #[derive(Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct ServiceabilityResponse { pub serviceable: bool, -} \ No newline at end of file +} diff --git a/Backend/src/storage/models/mod.rs b/Backend/src/storage/models/mod.rs index b695f38..9d22048 100644 --- a/Backend/src/storage/models/mod.rs +++ b/Backend/src/storage/models/mod.rs @@ -1,3 +1,3 @@ pub mod auth; +pub mod geometry; pub mod user; -pub mod geometry; \ No newline at end of file diff --git a/Backend/src/storage/models/user.rs b/Backend/src/storage/models/user.rs index ec928ac..a9a2428 100644 --- a/Backend/src/storage/models/user.rs +++ b/Backend/src/storage/models/user.rs @@ -2,7 +2,10 @@ use std::io::Write; use crate::db::{ self, - schema::school_trips::{sql_types::RoleType, users::{self}}, + schema::school_trips::{ + sql_types::RoleType, + users::{self}, + }, }; use chrono::NaiveDateTime; use diesel::{ @@ -42,7 +45,7 @@ impl FromSql for Role { } /// User table sturct -#[derive(AsChangeset, Queryable, Insertable, Selectable)] +#[derive(AsChangeset, Queryable, Insertable, Selectable, Serialize, Deserialize, Clone)] #[diesel(table_name = db::schema::school_trips::users)] pub struct User { pub name: Option, @@ -59,4 +62,4 @@ impl Selectable for Role { fn construct_selection() -> Self::SelectExpression { users::role } -} \ No newline at end of file +} diff --git a/Backend/src/tools/contants.rs b/Backend/src/tools/contants.rs index 672c033..5540539 100644 --- a/Backend/src/tools/contants.rs +++ b/Backend/src/tools/contants.rs @@ -1,4 +1,3 @@ - pub const ONEWEEK: i64 = 7 * 24 * 60; -pub const SHAPE_FILE_PATH: &str = "./shape_files"; \ No newline at end of file +pub const SHAPE_FILE_PATH: &str = "./shape_files"; diff --git a/Backend/src/tools/mod.rs b/Backend/src/tools/mod.rs index 4fcf81e..aa2bc7e 100644 --- a/Backend/src/tools/mod.rs +++ b/Backend/src/tools/mod.rs @@ -1,5 +1,5 @@ -pub mod utils; pub mod contants; -pub mod session_utils; pub mod polygon_utils; -pub mod types; \ No newline at end of file +pub mod session_utils; +pub mod types; +pub mod utils; diff --git a/Backend/src/tools/polygon_utils/geo_json.rs b/Backend/src/tools/polygon_utils/geo_json.rs index 5fc4048..b43667f 100644 --- a/Backend/src/tools/polygon_utils/geo_json.rs +++ b/Backend/src/tools/polygon_utils/geo_json.rs @@ -1,7 +1,9 @@ -use log::{error, info}; +use log::error; use std::{fs, str::FromStr}; -use crate::tools::{contants::SHAPE_FILE_PATH, types::MultiPolygonWithName, utils::get_file_name_without_extension}; +use crate::tools::{ + contants::SHAPE_FILE_PATH, types::MultiPolygonWithName, utils::get_file_name_without_extension, +}; pub fn get_available_shape_files() -> Vec { let mut shape_files = Vec::new(); @@ -15,7 +17,10 @@ pub fn get_available_shape_files() -> Vec { let multi_polygon: geo::MultiPolygon = TryFrom::try_from(geojson.to_owned()).unwrap(); - shape_files.push(MultiPolygonWithName::new(get_file_name_without_extension(file_name), multi_polygon)) + shape_files.push(MultiPolygonWithName::new( + get_file_name_without_extension(file_name), + multi_polygon, + )) } Err(err) => { error!("Error while parsing geo json string {}", err); diff --git a/Backend/src/tools/session_utils.rs b/Backend/src/tools/session_utils.rs index d366d06..b5c00c7 100644 --- a/Backend/src/tools/session_utils.rs +++ b/Backend/src/tools/session_utils.rs @@ -1,22 +1,17 @@ -use std::collections::HashSet; - -use jsonwebtoken::{DecodingKey, EncodingKey, Header, TokenData, Validation}; +use jsonwebtoken::{DecodingKey, EncodingKey, Header, Validation}; use serde::{Deserialize, Serialize}; use super::utils::get_current_time; - - - #[derive(Serialize, Deserialize)] pub struct SessionToken { - exp : i64, + exp: i64, iat: i64, - id : String + id: String, } impl SessionToken { - pub fn encode(&self, secret : String) -> String { + pub fn encode(&self, secret: String) -> String { jsonwebtoken::encode( &Header::default(), &self, @@ -25,22 +20,23 @@ impl SessionToken { .unwrap() } - pub fn decode(token: String, secret : String) -> Option { + pub fn decode(token: String, secret: String) -> Option { match jsonwebtoken::decode::( &token, &DecodingKey::from_secret(secret.to_owned().as_bytes()), - &Validation::default() - ).ok() { + &Validation::default(), + ) + .ok() + { Some(token_data) => Some(token_data.claims), - None => None + None => None, } - } - pub fn new(id:String, exp:i64) -> Self { + } + pub fn new(id: String, exp: i64) -> Self { SessionToken { id, exp, - iat : get_current_time().timestamp() + iat: get_current_time().timestamp(), } } - -} \ No newline at end of file +} diff --git a/Backend/src/tools/types.rs b/Backend/src/tools/types.rs index 788bc9c..e9a5e29 100644 --- a/Backend/src/tools/types.rs +++ b/Backend/src/tools/types.rs @@ -1,25 +1,18 @@ use geo::MultiPolygon; use serde::{Deserialize, Serialize}; -#[derive (Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone)] pub struct MultiPolygonWithName { - name : String, - polygon : MultiPolygon, + name: String, + polygon: MultiPolygon, } impl MultiPolygonWithName { - pub fn new(name:String, polygon: MultiPolygon) -> Self { - MultiPolygonWithName{ - name - , polygon - } - } - - pub fn get_name(self) -> String { - return self.name; + pub fn new(name: String, polygon: MultiPolygon) -> Self { + MultiPolygonWithName { name, polygon } } pub fn get_polygon(self) -> MultiPolygon { - return self.polygon; + return self.polygon; } -} \ No newline at end of file +} diff --git a/README.md b/README.md index c1f8197..413d87d 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,9 @@ Open source project for school trips ``cargo install diesel_cli`` +3. diesel migration run +4. cargo run-script start + to create migrations diesel migration generate