|
| 1 | +use crate::{ |
| 2 | + helper::{generate_nonce, validate_signature}, |
| 3 | + models::user_model::{UpdateUser, User}, |
| 4 | + repository::mongodb_repo::{CreateUserRequest, MongoRepo}, |
| 5 | +}; |
| 6 | +use mongodb::results::InsertOneResult; |
| 7 | +use rocket::{http::Status, serde::json::Json, State}; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | + |
| 10 | +#[derive(Serialize)] |
| 11 | +pub struct NonceResponse { |
| 12 | + nonce: i32, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Deserialize)] |
| 16 | +pub struct VerifyRequest { |
| 17 | + pub address: String, |
| 18 | + pub signature: String, |
| 19 | +} |
| 20 | + |
| 21 | +#[get("/<address>")] |
| 22 | +pub fn random_nonce(db: &State<MongoRepo>, address: String) -> Result<Json<NonceResponse>, Status> { |
| 23 | + let nonce = generate_nonce(); |
| 24 | + let user = db.get_user_by_address(&address.to_owned()); |
| 25 | + if user.unwrap().is_none() { |
| 26 | + let data = CreateUserRequest { |
| 27 | + address: address.to_owned(), |
| 28 | + nonce: nonce, |
| 29 | + }; |
| 30 | + db.create_user_with_nonce(data); |
| 31 | + } else { |
| 32 | + db.update_nonce(&address, nonce); |
| 33 | + } |
| 34 | + Ok(Json(NonceResponse { nonce: nonce })) |
| 35 | +} |
| 36 | + |
| 37 | +#[post("/verifySignature", data = "<payload>")] |
| 38 | +pub fn verify_signature(db: &State<MongoRepo>, payload: Json<VerifyRequest>) -> Result<Json<Option<User>>, Status> { |
| 39 | + let user_detail = db.get_user_by_address(&payload.address); |
| 40 | + match user_detail { |
| 41 | + Ok(user) => { |
| 42 | + let result = validate_signature(user.as_ref().unwrap().nonce, payload); |
| 43 | + println!("{}", result); |
| 44 | + Ok(Json(user)) |
| 45 | + } |
| 46 | + Err(_) => Err(Status::BadRequest), |
| 47 | + } |
| 48 | + // send jwt token |
| 49 | +} |
| 50 | + |
| 51 | +#[post("/", data = "<new_user>")] |
| 52 | +pub fn create_user( |
| 53 | + db: &State<MongoRepo>, |
| 54 | + new_user: Json<User>, |
| 55 | +) -> Result<Json<InsertOneResult>, Status> { |
| 56 | + let data = User { |
| 57 | + id: None, |
| 58 | + username: None, |
| 59 | + profile: None, |
| 60 | + banner: None, |
| 61 | + address: new_user.address.to_owned(), |
| 62 | + nonce: new_user.nonce.to_owned(), |
| 63 | + }; |
| 64 | + let user_detail = db.create_user(data); |
| 65 | + match user_detail { |
| 66 | + Ok(user) => Ok(Json(user)), |
| 67 | + Err(_) => Err(Status::InternalServerError), |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[get("/")] |
| 72 | +pub fn get_all_users(db: &State<MongoRepo>) -> Result<Json<Vec<User>>, Status> { |
| 73 | + let users = db.get_all_users(); |
| 74 | + match users { |
| 75 | + Ok(users) => Ok(Json(users)), |
| 76 | + Err(_) => Err(Status::InternalServerError), |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[get("/<path>")] |
| 81 | +pub fn get_user(db: &State<MongoRepo>, path: String) -> Result<Json<User>, Status> { |
| 82 | + let id = path; |
| 83 | + if id.is_empty() { |
| 84 | + return Err(Status::BadRequest); |
| 85 | + }; |
| 86 | + let user_detail = db.get_user_by_id(&id); |
| 87 | + match user_detail { |
| 88 | + Ok(user) => Ok(Json(user)), |
| 89 | + Err(_) => Err(Status::InternalServerError), |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[put("/<path>", data = "<new_user>")] |
| 94 | +pub fn update_user( |
| 95 | + db: &State<MongoRepo>, |
| 96 | + path: String, |
| 97 | + new_user: Json<UpdateUser>, |
| 98 | +) -> Result<Json<User>, Status> { |
| 99 | + let id = path; |
| 100 | + if id.is_empty() { |
| 101 | + return Err(Status::BadRequest); |
| 102 | + }; |
| 103 | + let data = UpdateUser { |
| 104 | + id: None, |
| 105 | + username: new_user.profile.to_owned(), |
| 106 | + profile: new_user.profile.to_owned(), |
| 107 | + banner: new_user.banner.to_owned(), |
| 108 | + address: new_user.address.to_owned(), |
| 109 | + }; |
| 110 | + let update_result = db.update_user(&id, data); |
| 111 | + match update_result { |
| 112 | + Ok(update) => { |
| 113 | + if update.matched_count == 1 { |
| 114 | + let updated_user_info = db.get_user_by_id(&id); |
| 115 | + return match updated_user_info { |
| 116 | + Ok(user) => Ok(Json(user)), |
| 117 | + Err(_) => Err(Status::InternalServerError), |
| 118 | + }; |
| 119 | + } else { |
| 120 | + return Err(Status::NotFound); |
| 121 | + } |
| 122 | + } |
| 123 | + Err(_) => Err(Status::InternalServerError), |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +#[delete("/<path>")] |
| 128 | +pub fn delete_user(db: &State<MongoRepo>, path: String) -> Result<Json<&str>, Status> { |
| 129 | + let id = path; |
| 130 | + if id.is_empty() { |
| 131 | + return Err(Status::BadRequest); |
| 132 | + }; |
| 133 | + let result = db.delete_user(&id); |
| 134 | + match result { |
| 135 | + Ok(res) => { |
| 136 | + if res.deleted_count == 1 { |
| 137 | + return Ok(Json("User successfully deleted!")); |
| 138 | + } else { |
| 139 | + return Err(Status::NotFound); |
| 140 | + } |
| 141 | + } |
| 142 | + Err(_) => Err(Status::InternalServerError), |
| 143 | + } |
| 144 | +} |
0 commit comments