Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User CRUD #493

Merged
merged 17 commits into from
Aug 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
finished user but no testing
HellenMelon committed May 31, 2024
commit 77cd96c00caa6d4e0e15d57de0434cc99b33472e
4 changes: 0 additions & 4 deletions backend/server/src/handler/user.rs
Original file line number Diff line number Diff line change
@@ -24,7 +24,3 @@ pub async fn get_username(







38 changes: 32 additions & 6 deletions backend/server/src/service/user.rs
Original file line number Diff line number Diff line change
@@ -5,20 +5,46 @@ use crate::models::user::User;
// 1. function to read all data of the user
pub async fn get_user(id: i64, pool: Pool<Postgres>) -> Result<User> {
// fetch username from database
Ok(sqlx::query_as!(
User,
"SELECT * FROM users WHERE id = $1", id)
Ok(
sqlx::query_as!(
User,
"SELECT * FROM users WHERE id = $1", id)
.fetch_one(&pool)
.await?)
}


// 2. Update the user with new details

pub async fn update_user(id: i64, zid?: String, degree_name?: String, degree_starting_year?: i64) -> Result<i64> {
pub async fn update_user(id: i64, zid: Option<String>, degree_name: Option<String>, degree_starting_year: Option<i64>, pool: Pool<Postgres>) -> Result<i64> {

Ok(1)
let possible_user = sqlx::query!("SELECT * FROM users id = $1", id)
.fetch_one(&pool)
.await?;

if possible_user.is_none() {
bail!("User with id {} does not exist", id);
}

if let Some(zid) = zid {
sqlx::query!("UPDATE users SET zid = $1 WHERE id = $2", zid, id)
.execute(&pool)
.await;
}

if let Some(degree_name) = degree_name {
sqlx::query!("UPDATE users SET degree_name = $1 WHERE id = $2", degree_name, id)
.execute(&pool)
.await;
}

if let Some(degree_starting_year) = degree_starting_year {
sqlx::query!("UPDATE users SET degree_name = $1 WHERE id = $2", degree_starting_year, id)
.execute(&pool)
.await;
}

Ok(possible_user)

}

// 4. Delete - delete user