Change add_member to use from and into functionality
This commit is contained in:
parent
3c6e6c8656
commit
8a3eb8027b
53
src/main.rs
53
src/main.rs
@ -1,6 +1,7 @@
|
|||||||
// Webserver
|
// Webserver
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
|
|
||||||
|
use entities::members::ActiveModel;
|
||||||
use rocket::{State, Error};
|
use rocket::{State, Error};
|
||||||
use rocket::response::status;
|
use rocket::response::status;
|
||||||
use rocket::serde::{Serialize, Deserialize, json::Json};
|
use rocket::serde::{Serialize, Deserialize, json::Json};
|
||||||
@ -59,6 +60,34 @@ impl From<<entities::members::Entity as sea_orm::EntityTrait>::Model> for Member
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implement DB Member Model -> MinimalMember
|
||||||
|
impl From<<entities::members::Entity as sea_orm::EntityTrait>::Model> for MinimalMember {
|
||||||
|
fn from(member: <entities::members::Entity as sea_orm::EntityTrait>::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<entities::members::ActiveModel> for rocket::serde::json::Json<MinimalMemberWithoutId> {
|
||||||
|
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)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct MinimalMember {
|
struct MinimalMember {
|
||||||
id: i32,
|
id: i32,
|
||||||
@ -141,14 +170,14 @@ async fn get_members(db: &State<DatabaseConnection>) -> Result<Json<MultipleMemb
|
|||||||
Ok(Json(output))
|
Ok(Json(output))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/member", data = "<minimalMemberWithoutId>")]
|
#[post("/member", data = "<minimal_member_without_id>")]
|
||||||
async fn add_member(db: &State<DatabaseConnection>, minimalMemberWithoutId: Json<MinimalMemberWithoutId>) -> Result<Json<MinimalMember>, ErrorResponder> {
|
async fn add_member(db: &State<DatabaseConnection>, minimal_member_without_id: Json<MinimalMemberWithoutId>) -> Result<Json<MinimalMember>, ErrorResponder> {
|
||||||
// Grab the database connection
|
// Grab the database connection
|
||||||
let db = db as &DatabaseConnection;
|
let db = db as &DatabaseConnection;
|
||||||
|
|
||||||
// Check if a member with the same NTNU username already exists
|
// Check if a member with the same NTNU username already exists
|
||||||
let matching_member: Option<members::Model> = Members::find() // Find a member in the "Members" table
|
let matching_member: Option<members::Model> = 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
|
.one(db) // We only care about one result
|
||||||
.await?; // Wait for the result
|
.await?; // Wait for the result
|
||||||
|
|
||||||
@ -162,15 +191,7 @@ async fn add_member(db: &State<DatabaseConnection>, minimalMemberWithoutId: Json
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create the new member info from the provided JSON
|
// Create the new member info from the provided JSON
|
||||||
let new_member = members::ActiveModel {
|
let new_member: members::ActiveModel = minimal_member_without_id.into();
|
||||||
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()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add the new member to the database
|
// Add the new member to the database
|
||||||
let res = Members::insert(new_member).exec(db).await?;
|
let res = Members::insert(new_member).exec(db).await?;
|
||||||
@ -189,13 +210,7 @@ async fn add_member(db: &State<DatabaseConnection>, minimalMemberWithoutId: Json
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Put the fetched info into a minimal member for returning
|
// Put the fetched info into a minimal member for returning
|
||||||
let member = MinimalMember {
|
let member: MinimalMember = new_member.into();
|
||||||
id: new_member.id,
|
|
||||||
ntnuUsername: new_member.ntnu_username,
|
|
||||||
firstName: new_member.first_name,
|
|
||||||
lastName: new_member.last_name,
|
|
||||||
email: new_member.email,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Json(member))
|
Ok(Json(member))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user