From cf314e2d96ce3f5e7cebaca6f33fa8ba373ccb2b Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 27 Dec 2020 12:32:01 +0530 Subject: add userprofile endpoint, fix cors (again?) --- backend/src/bin/server.rs | 2 ++ backend/src/handlers/users.rs | 53 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/backend/src/bin/server.rs b/backend/src/bin/server.rs index 310914e..135dccc 100644 --- a/backend/src/bin/server.rs +++ b/backend/src/bin/server.rs @@ -34,6 +34,7 @@ async fn main() -> std::io::Result<()> { Cors::default() .allowed_origin("http://127.0.0.1:8000") .allowed_origin("http://localhost:8000") + .allowed_origin("https://poly.googleusercontent.com") .allow_any_method() .allow_any_header(), ) @@ -46,6 +47,7 @@ async fn main() -> std::io::Result<()> { .data(pool.clone()) .service( web::scope("/user") + .route("/profile", web::get().to(users::user_profile)) .route("/existing", web::post().to(users::name_exists)) .route("/login", web::post().to(users::login)) .route("/logout", web::post().to(users::logout)) diff --git a/backend/src/handlers/users.rs b/backend/src/handlers/users.rs index a043c1f..8423384 100644 --- a/backend/src/handlers/users.rs +++ b/backend/src/handlers/users.rs @@ -1,5 +1,7 @@ -use crate::models::{Customer, NewCustomer}; +use crate::models::{Customer, NewCustomer, Rating, Transaction}; use crate::schema::customer::dsl::*; +use crate::schema::rating::dsl as rs; +use crate::schema::transaction::dsl as ts; use crate::TPool; use actix_identity::Identity; @@ -7,7 +9,7 @@ use actix_web::{web, HttpResponse, Responder}; use bcrypt::{hash, verify, DEFAULT_COST}; use diesel::prelude::*; use log::{error, info}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; pub async fn new_user( pool: web::Data, @@ -146,3 +148,50 @@ pub async fn change_password( } return HttpResponse::Unauthorized().body("Login first"); } + +#[derive(Serialize)] +struct UserProfile { + pub username: String, + pub email_id: String, + pub address: Option, + pub transactions: Vec, + pub ratings_given: i32, + pub phone_number: String, +} + +pub async fn user_profile( + cookie: Identity, + pool: web::Data, +) -> impl Responder { + info!("Fetching user profile for {:?}", cookie.identity()); + let conn = pool.get().unwrap(); + + if let Some(uname) = cookie.identity() { + let selected_user = customer + .filter(username.eq(&uname)) + .limit(1) + .first::(&conn) + .expect("Couldn't connect to DB"); + let user_transactions = ts::transaction + .filter(ts::customer_id.eq(selected_user.id)) + .load(&conn) + .expect("Couldn't connect to DB"); + let user_ratings = rs::rating + .filter(rs::customer_id.eq(selected_user.id)) + .load::(&conn) + .expect("Couldn't connect to DB") + .len() as i32; + let profile = UserProfile { + username: selected_user.username, + email_id: selected_user.email_id, + address: selected_user.address, + transactions: user_transactions, + ratings_given: user_ratings, + phone_number: selected_user.phone_number, + }; + return HttpResponse::Ok().json(&profile); + } else { + return HttpResponse::Unauthorized() + .body("Need to be logged in to view profile!"); + } +} -- cgit v1.2.3