diff --git a/src/main.rs b/src/main.rs index 8a5c6d2..cdf82fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,7 @@ struct MinimalMemberWithoutId { } #[derive(Responder)] -#[response(status = 500, content_type = "json")] +#[response(status = 500, content_type = "text/plain")] struct ErrorResponder { message: String } @@ -81,6 +81,21 @@ async fn add_member(db: &State, minimalMemberWithoutId: Json // 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 + .one(db) // We only care about one result + .await?; // Wait for the result + + // If a member exists return an error + if matching_member.is_some() { + return Err( + ErrorResponder { + message: "A member with this NTNU username already exists".to_string(), + } + ) + } + // Create the new member info from the provided JSON let new_member = members::ActiveModel { ntnu_username: ActiveValue::Set(minimalMemberWithoutId.ntnuUsername.to_owned()), @@ -108,6 +123,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,