Skip to content

Commit

Permalink
get all user, delete user
Browse files Browse the repository at this point in the history
  • Loading branch information
jb-alvarado committed Nov 23, 2023
1 parent 81e3274 commit f86a6c3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
71 changes: 70 additions & 1 deletion ffplayout-api/src/api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,45 @@ async fn get_user(
}
}

/// **Get User by ID**
///
/// ```BASH
/// curl -X GET 'http://127.0.0.1:8787/api/user/2' -H 'Content-Type: application/json' \
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
#[get("/user/{name}")]
#[has_any_role("Role::Admin", type = "Role")]
async fn get_user_by_name(
pool: web::Data<Pool<Sqlite>>,
name: web::Path<String>,
) -> Result<impl Responder, ServiceError> {
match handles::select_user(&pool.into_inner(), &name).await {
Ok(user) => Ok(web::Json(user)),
Err(e) => {
error!("{e}");
Err(ServiceError::InternalServerError)
}
}
}

// **Get all User**
///
/// ```BASH
/// curl -X GET 'http://127.0.0.1:8787/api/users' -H 'Content-Type: application/json' \
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
#[get("/users")]
#[has_any_role("Role::Admin", type = "Role")]
async fn get_users(pool: web::Data<Pool<Sqlite>>) -> Result<impl Responder, ServiceError> {
match handles::select_users(&pool.into_inner()).await {
Ok(users) => Ok(web::Json(users)),
Err(e) => {
error!("{e}");
Err(ServiceError::InternalServerError)
}
}
}

/// **Update current User**
///
/// ```BASH
Expand All @@ -245,11 +284,20 @@ async fn update_user(
id: web::Path<i32>,
user: web::ReqData<LoginUser>,
data: web::Json<User>,
role: AuthDetails<Role>,
) -> Result<impl Responder, ServiceError> {
if id.into_inner() == user.id {
if id.into_inner() == user.id || role.has_role(&Role::Admin) {
let mut fields = String::new();

if !data.username.is_empty() {
fields.push_str(format!("username = '{}'", data.username).as_str());
}

if let Some(mail) = data.mail.clone() {
if !fields.is_empty() {
fields.push_str(", ");
}

fields.push_str(format!("mail = '{mail}'").as_str());
}

Expand Down Expand Up @@ -301,6 +349,27 @@ async fn add_user(
}
}

// **Delete User**
///
/// ```BASH
/// curl -X GET 'http://127.0.0.1:8787/api/user/2' -H 'Content-Type: application/json' \
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
#[delete("/user/{name}")]
#[has_any_role("Role::Admin", type = "Role")]
async fn remove_user(
pool: web::Data<Pool<Sqlite>>,
name: web::Path<String>,
) -> Result<impl Responder, ServiceError> {
match handles::delete_user(&pool.into_inner(), &name).await {
Ok(_) => return Ok("Delete user success"),
Err(e) => {
error!("{e}");
Err(ServiceError::InternalServerError)
}
}
}

/// #### ffpapi Settings
///
/// **Get Settings from Channel**
Expand Down
21 changes: 21 additions & 0 deletions ffplayout-api/src/db/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ pub async fn select_user(conn: &Pool<Sqlite>, user: &str) -> Result<User, sqlx::
sqlx::query_as(query).bind(user).fetch_one(conn).await
}

pub async fn select_user_by_id(conn: &Pool<Sqlite>, id: i32) -> Result<User, sqlx::Error> {
let query = "SELECT id, mail, username, role_id FROM user WHERE id = $1";

sqlx::query_as(query).bind(id).fetch_one(conn).await
}

pub async fn select_users(conn: &Pool<Sqlite>) -> Result<Vec<User>, sqlx::Error> {
let query = "SELECT id, username FROM user";

sqlx::query_as(query).fetch_all(conn).await
}

pub async fn insert_user(
conn: &Pool<Sqlite>,
user: User,
Expand Down Expand Up @@ -260,6 +272,15 @@ pub async fn update_user(
sqlx::query(&query).bind(id).execute(conn).await
}

pub async fn delete_user(
conn: &Pool<Sqlite>,
name: &str,
) -> Result<SqliteQueryResult, sqlx::Error> {
let query = "DELETE FROM user WHERE username = $1;";

sqlx::query(query).bind(name).execute(conn).await
}

pub async fn select_presets(conn: &Pool<Sqlite>, id: i32) -> Result<Vec<TextPreset>, sqlx::Error> {
let query = "SELECT * FROM presets WHERE channel_id = $1";

Expand Down
1 change: 1 addition & 0 deletions ffplayout-api/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct User {
#[serde(skip_deserializing)]
pub id: i32,
#[sqlx(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub mail: Option<String>,
pub username: String,
#[sqlx(default)]
Expand Down
3 changes: 3 additions & 0 deletions ffplayout-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ async fn main() -> std::io::Result<()> {
.wrap(auth)
.service(add_user)
.service(get_user)
.service(get_user_by_name)
.service(get_users)
.service(remove_user)
.service(get_playout_config)
.service(update_playout_config)
.service(add_preset)
Expand Down

0 comments on commit f86a6c3

Please sign in to comment.