diff options
Diffstat (limited to 'backend/src')
-rw-r--r-- | backend/src/bin/server.rs | 2 | ||||
-rw-r--r-- | 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<()> { | |||
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 @@ | |||
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 | } | ||