diff --git a/src/main.rs b/src/main.rs index 0b87eda..6260e29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ // Webserver #[macro_use] extern crate rocket; +use entities::members::ActiveModel; use rocket::{State, Error}; use rocket::response::status; use rocket::serde::{Serialize, Deserialize, json::Json}; @@ -59,6 +60,34 @@ impl From<::Model> for Member } } +// Implement DB Member Model -> MinimalMember +impl From<::Model> for MinimalMember { + fn from(member: ::Model) -> MinimalMember { + MinimalMember { + id: member.id, + ntnuUsername: member.ntnu_username, + firstName: member.first_name, + lastName: member.last_name, + email: member.email, + } + } +} + +// Create the functionality to create a database model member from the local struct model member +impl Into for rocket::serde::json::Json { + fn into(self) -> entities::members::ActiveModel { + entities::members::ActiveModel { + ntnu_username: ActiveValue::Set(self.ntnuUsername.to_owned()), + first_name: ActiveValue::Set(self.firstName.to_owned()), + last_name: ActiveValue::Set(self.lastName.to_owned()), + email: ActiveValue::Set(self.email.to_owned()), + balance: ActiveValue::Set(0), + image_preference: ActiveValue::Set("".to_string()), + ..Default::default() + } + } +} + #[derive(Serialize, Deserialize)] struct MinimalMember { id: i32, @@ -141,14 +170,14 @@ async fn get_members(db: &State) -> Result, minimalMemberWithoutId: Json) -> Result, ErrorResponder> { +#[post("/member", data = "")] +async fn add_member(db: &State, minimal_member_without_id: Json) -> Result, ErrorResponder> { // Grab the database connection let db = db as &DatabaseConnection; // Check if a member with the same NTNU username already exists let matching_member: Option = Members::find() // Find a member in the "Members" table - .filter(members::Column::NtnuUsername.eq(minimalMemberWithoutId.ntnuUsername.to_owned())) // Filter by the provided username in the NtnuUsername column + .filter(members::Column::NtnuUsername.eq(minimal_member_without_id.ntnuUsername.to_owned())) // Filter by the provided username in the NtnuUsername column .one(db) // We only care about one result .await?; // Wait for the result @@ -162,15 +191,7 @@ async fn add_member(db: &State, minimalMemberWithoutId: Json } // Create the new member info from the provided JSON - let new_member = members::ActiveModel { - ntnu_username: ActiveValue::Set(minimalMemberWithoutId.ntnuUsername.to_owned()), - first_name: ActiveValue::Set(minimalMemberWithoutId.firstName.to_owned()), - last_name: ActiveValue::Set(minimalMemberWithoutId.lastName.to_owned()), - email: ActiveValue::Set(minimalMemberWithoutId.email.to_owned()), - balance: ActiveValue::Set(0), - image_preference: ActiveValue::Set("Money".to_string()), - ..Default::default() - }; + let new_member: members::ActiveModel = minimal_member_without_id.into(); // Add the new member to the database let res = Members::insert(new_member).exec(db).await?; @@ -189,13 +210,7 @@ async fn add_member(db: &State, minimalMemberWithoutId: Json } // Put the fetched info into a minimal member for returning - let member = MinimalMember { - id: new_member.id, - ntnuUsername: new_member.ntnu_username, - firstName: new_member.first_name, - lastName: new_member.last_name, - email: new_member.email, - }; + let member: MinimalMember = new_member.into(); Ok(Json(member)) } diff --git a/test.db b/test.db index c34eab2..8397dca 100644 Binary files a/test.db and b/test.db differ