Forráskód Böngészése

Remove the Option from the user id.

Lee Morgan 1 éve
szülő
commit
4399f67d54
4 módosított fájl, 19 hozzáadás és 33 törlés
  1. 6 6
      src/controllers/user.rs
  2. 6 9
      src/dto/user.rs
  3. 1 6
      src/models/account.rs
  4. 6 12
      src/models/user.rs

+ 6 - 6
src/controllers/user.rs

@@ -3,16 +3,16 @@ use mongodb::{bson::doc, Database, Collection};
 use regex::Regex;
 use serde_json::json;
 
-use crate::models::user::{User, NewUserInput};
+use crate::models::user::User;
 use crate::models::account::{Account};
-use crate::dto;
+use crate::dto::user::{CreateInput, LoginInput};
 use crate::app_error::AppError;
 use crate::auth::user_auth;
 
 #[post("/api/user")]
 pub async fn create_route(
     db: web::Data<Database>,
-    payload: web::Json<NewUserInput>
+    payload: web::Json<CreateInput>
 ) -> Result<HttpResponse, AppError> {
     let user_collection = db.collection::<User>("users");
     let email = payload.email.to_lowercase();
@@ -25,13 +25,13 @@ pub async fn create_route(
 #[post("/api/user/login")]
 pub async fn login_route(
     db: web::Data<Database>,
-    payload: web::Json<dto::user::Login>
+    payload: web::Json<LoginInput>
 ) -> Result<HttpResponse, AppError> {
     let user_collection = db.collection::<User>("users");
     let email = payload.email.to_lowercase();
     let user = User::find_by_email(&user_collection, &email).await?;
     validate_password(&user, &payload.password_hash, &payload.password_salt)?;
-    let cookie = create_user_cookie(Some(user._id.unwrap().to_string()));
+    let cookie = create_user_cookie(Some(user.id.to_string()));
     Ok(HttpResponse::Ok().cookie(cookie).json(user.response(None)))
 }
 
@@ -45,7 +45,7 @@ pub async fn logout_route() -> Result<HttpResponse, AppError> {
 pub async fn get_route(db: web::Data<Database>, req: HttpRequest) -> Result<HttpResponse, AppError> {
     let user = user_auth(&db, &req).await?;
     let account_collection = db.collection::<Account>("accounts");
-    let accounts = Account::find_by_user(&account_collection, user._id.unwrap()).await?;
+    let accounts = Account::find_by_user(&account_collection, user.id).await?;
     let response_accounts = accounts.into_iter().map(Account::response).collect();
     Ok(HttpResponse::Ok().json(user.response(Some(response_accounts))))
 }

+ 6 - 9
src/dto/user.rs

@@ -1,18 +1,15 @@
-use serde::{Serialize, Deserialize};
-
-use crate::models::account::ResponseAccount;
+use serde::Deserialize;
 
 #[derive(Deserialize)]
-pub struct Login {
+pub struct LoginInput {
     pub email: String,
     pub password_hash: String,
     pub password_salt: String
 }
 
-#[derive(Serialize, Deserialize)]
-pub struct ResponseUser {
-    pub id: String,
+#[derive(Deserialize)]
+pub struct CreateInput {
     pub email: String,
-    pub accounts: Option<Vec<ResponseAccount>>,
-    pub created_at: i64,
+    pub password_hash: String,
+    pub password_salt: String
 }

+ 1 - 6
src/models/account.rs

@@ -25,14 +25,9 @@ pub struct ResponseAccount {
 
 impl Account {
     pub async fn insert(collection: &Collection<Account>, input: CreateInput, user: User) -> Result<ObjectId, AppError> {
-        let object_id = match user._id {
-            Some(oid) => Ok(oid),
-            None => Err(AppError::Auth)
-        };
-
         let account = Account {
             _id: None,
-            user: object_id?,
+            user: user.id,
             data: input.data,
             created_at: DateTime::now()
         };

+ 6 - 12
src/models/user.rs

@@ -4,24 +4,18 @@ use mongodb::{Collection, error::Error};
 
 use crate::app_error::AppError;
 use crate::models::account::ResponseAccount;
+use crate::dto::user::CreateInput;
 
 #[derive(Debug, Serialize, Deserialize)]
 pub struct User {
-    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
-    pub _id: Option<ObjectId>,
+    #[serde(rename = "_id")]
+    pub id: ObjectId,
     pub email: String,
     pub password_hash: String,
     pub password_salt: String,
     pub created_at: bson::DateTime
 }
 
-#[derive(Debug, Deserialize)]
-pub struct NewUserInput {
-    pub email: String,
-    pub password_hash: String,
-    pub password_salt: String
-}
-
 #[derive(Serialize)]
 pub struct ResponseUser {
     id: String,
@@ -31,9 +25,9 @@ pub struct ResponseUser {
 }
 
 impl User {
-    pub async fn insert(collection: &Collection<User>, input: NewUserInput) -> Result<(), Error> {
+    pub async fn insert(collection: &Collection<User>, input: CreateInput) -> Result<(), Error> {
         let user = User {
-            _id: None,
+            id: ObjectId::new(),
             email: input.email,
             password_hash: input.password_hash,
             password_salt: input.password_salt,
@@ -64,7 +58,7 @@ impl User {
 
     pub fn response(self, accounts: Option<Vec<ResponseAccount>>) -> ResponseUser {
         ResponseUser {
-            id: self._id.unwrap().to_string(),
+            id: self.id.to_string(),
             email: self.email.clone(),
             created_at: self.created_at.timestamp_millis(),
             accounts: accounts