diff options
Diffstat (limited to 'backend/src/handlers')
-rw-r--r-- | backend/src/handlers/users.rs | 53 |
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 @@ | |||
1 | use crate::models::{Customer, NewCustomer}; | 1 | use crate::models::{Customer, NewCustomer, Rating, Transaction}; |
2 | use crate::schema::customer::dsl::*; | 2 | use crate::schema::customer::dsl::*; |
3 | use crate::schema::rating::dsl as rs; | ||
4 | use crate::schema::transaction::dsl as ts; | ||
3 | use crate::TPool; | 5 | use crate::TPool; |
4 | 6 | ||
5 | use actix_identity::Identity; | 7 | use actix_identity::Identity; |
@@ -7,7 +9,7 @@ use actix_web::{web, HttpResponse, Responder}; | |||
7 | use bcrypt::{hash, verify, DEFAULT_COST}; | 9 | use bcrypt::{hash, verify, DEFAULT_COST}; |
8 | use diesel::prelude::*; | 10 | use diesel::prelude::*; |
9 | use log::{error, info}; | 11 | use log::{error, info}; |
10 | use serde::Deserialize; | 12 | use serde::{Deserialize, Serialize}; |
11 | 13 | ||
12 | pub async fn new_user( | 14 | pub 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)] | ||
153 | struct 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 | |||
162 | pub 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 | } | ||