aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-27 07:02:01 +0000
committerAkshay <[email protected]>2020-12-27 07:02:01 +0000
commitcf314e2d96ce3f5e7cebaca6f33fa8ba373ccb2b (patch)
tree761182b57b92e6ca58cd27513fd5e803542c6734
parent192a5c36c46b50167461c3cf1d7afa069e816e01 (diff)
add userprofile endpoint, fix cors (again?)
-rw-r--r--backend/src/bin/server.rs2
-rw-r--r--backend/src/handlers/users.rs53
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<()> {
34 Cors::default() 34 Cors::default()
35 .allowed_origin("http://127.0.0.1:8000") 35 .allowed_origin("http://127.0.0.1:8000")
36 .allowed_origin("http://localhost:8000") 36 .allowed_origin("http://localhost:8000")
37 .allowed_origin("https://poly.googleusercontent.com")
37 .allow_any_method() 38 .allow_any_method()
38 .allow_any_header(), 39 .allow_any_header(),
39 ) 40 )
@@ -46,6 +47,7 @@ async fn main() -> std::io::Result<()> {
46 .data(pool.clone()) 47 .data(pool.clone())
47 .service( 48 .service(
48 web::scope("/user") 49 web::scope("/user")
50 .route("/profile", web::get().to(users::user_profile))
49 .route("/existing", web::post().to(users::name_exists)) 51 .route("/existing", web::post().to(users::name_exists))
50 .route("/login", web::post().to(users::login)) 52 .route("/login", web::post().to(users::login))
51 .route("/logout", web::post().to(users::logout)) 53 .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 @@
1use crate::models::{Customer, NewCustomer}; 1use crate::models::{Customer, NewCustomer, Rating, Transaction};
2use crate::schema::customer::dsl::*; 2use crate::schema::customer::dsl::*;
3use crate::schema::rating::dsl as rs;
4use crate::schema::transaction::dsl as ts;
3use crate::TPool; 5use crate::TPool;
4 6
5use actix_identity::Identity; 7use actix_identity::Identity;
@@ -7,7 +9,7 @@ use actix_web::{web, HttpResponse, Responder};
7use bcrypt::{hash, verify, DEFAULT_COST}; 9use bcrypt::{hash, verify, DEFAULT_COST};
8use diesel::prelude::*; 10use diesel::prelude::*;
9use log::{error, info}; 11use log::{error, info};
10use serde::Deserialize; 12use serde::{Deserialize, Serialize};
11 13
12pub async fn new_user( 14pub async fn new_user(
13 pool: web::Data<TPool>, 15 pool: web::Data<TPool>,
@@ -146,3 +148,50 @@ pub async fn change_password(
146 } 148 }
147 return HttpResponse::Unauthorized().body("Login first"); 149 return HttpResponse::Unauthorized().body("Login first");
148} 150}
151
152#[derive(Serialize)]
153struct UserProfile {
154 pub username: String,
155 pub email_id: String,
156 pub address: Option<String>,
157 pub transactions: Vec<Transaction>,
158 pub ratings_given: i32,
159 pub phone_number: String,
160}
161
162pub async fn user_profile(
163 cookie: Identity,
164 pool: web::Data<TPool>,
165) -> impl Responder {
166 info!("Fetching user profile for {:?}", cookie.identity());
167 let conn = pool.get().unwrap();
168
169 if let Some(uname) = cookie.identity() {
170 let selected_user = customer
171 .filter(username.eq(&uname))
172 .limit(1)
173 .first::<Customer>(&conn)
174 .expect("Couldn't connect to DB");
175 let user_transactions = ts::transaction
176 .filter(ts::customer_id.eq(selected_user.id))
177 .load(&conn)
178 .expect("Couldn't connect to DB");
179 let user_ratings = rs::rating
180 .filter(rs::customer_id.eq(selected_user.id))
181 .load::<Rating>(&conn)
182 .expect("Couldn't connect to DB")
183 .len() as i32;
184 let profile = UserProfile {
185 username: selected_user.username,
186 email_id: selected_user.email_id,
187 address: selected_user.address,
188 transactions: user_transactions,
189 ratings_given: user_ratings,
190 phone_number: selected_user.phone_number,
191 };
192 return HttpResponse::Ok().json(&profile);
193 } else {
194 return HttpResponse::Unauthorized()
195 .body("Need to be logged in to view profile!");
196 }
197}