aboutsummaryrefslogtreecommitdiff
path: root/backend/src/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'backend/src/handlers')
-rw-r--r--backend/src/handlers/users.rs53
1 files changed, 51 insertions, 2 deletions
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}