get all user, delete user
This commit is contained in:
parent
81e3274789
commit
f86a6c3f1d
@ -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**
|
/// **Update current User**
|
||||||
///
|
///
|
||||||
/// ```BASH
|
/// ```BASH
|
||||||
@ -245,11 +284,20 @@ async fn update_user(
|
|||||||
id: web::Path<i32>,
|
id: web::Path<i32>,
|
||||||
user: web::ReqData<LoginUser>,
|
user: web::ReqData<LoginUser>,
|
||||||
data: web::Json<User>,
|
data: web::Json<User>,
|
||||||
|
role: AuthDetails<Role>,
|
||||||
) -> Result<impl Responder, ServiceError> {
|
) -> 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();
|
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 let Some(mail) = data.mail.clone() {
|
||||||
|
if !fields.is_empty() {
|
||||||
|
fields.push_str(", ");
|
||||||
|
}
|
||||||
|
|
||||||
fields.push_str(format!("mail = '{mail}'").as_str());
|
fields.push_str(format!("mail = '{mail}'").as_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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
|
/// #### ffpapi Settings
|
||||||
///
|
///
|
||||||
/// **Get Settings from Channel**
|
/// **Get Settings from Channel**
|
||||||
|
@ -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
|
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(
|
pub async fn insert_user(
|
||||||
conn: &Pool<Sqlite>,
|
conn: &Pool<Sqlite>,
|
||||||
user: User,
|
user: User,
|
||||||
@ -260,6 +272,15 @@ pub async fn update_user(
|
|||||||
sqlx::query(&query).bind(id).execute(conn).await
|
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> {
|
pub async fn select_presets(conn: &Pool<Sqlite>, id: i32) -> Result<Vec<TextPreset>, sqlx::Error> {
|
||||||
let query = "SELECT * FROM presets WHERE channel_id = $1";
|
let query = "SELECT * FROM presets WHERE channel_id = $1";
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ pub struct User {
|
|||||||
#[serde(skip_deserializing)]
|
#[serde(skip_deserializing)]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
#[sqlx(default)]
|
#[sqlx(default)]
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub mail: Option<String>,
|
pub mail: Option<String>,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
#[sqlx(default)]
|
#[sqlx(default)]
|
||||||
|
@ -109,6 +109,9 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.wrap(auth)
|
.wrap(auth)
|
||||||
.service(add_user)
|
.service(add_user)
|
||||||
.service(get_user)
|
.service(get_user)
|
||||||
|
.service(get_user_by_name)
|
||||||
|
.service(get_users)
|
||||||
|
.service(remove_user)
|
||||||
.service(get_playout_config)
|
.service(get_playout_config)
|
||||||
.service(update_playout_config)
|
.service(update_playout_config)
|
||||||
.service(add_preset)
|
.service(add_preset)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user