make channel_ids optional

This commit is contained in:
jb-alvarado 2024-06-20 17:19:32 +02:00
parent f1ea2f4f43
commit 7c2cbc5a09
5 changed files with 24 additions and 25 deletions

View File

@ -186,7 +186,7 @@ pub async fn login(pool: web::Data<Pool<Sqlite>>, credentials: web::Json<User>)
if verified_password.is_ok() {
let claims = Claims::new(
user.id,
user.channel_ids.clone(),
user.channel_ids.clone().unwrap_or_default(),
username.clone(),
role.clone(),
);
@ -321,14 +321,14 @@ async fn update_user(
fields.push_str(&format!("mail = '{mail}'"));
}
if !data.channel_ids.is_empty() {
if let Some(channel_ids) = &data.channel_ids {
if !fields.is_empty() {
fields.push_str(", ");
}
fields.push_str(&format!(
"channel_ids = '{}'",
data.channel_ids
channel_ids
.iter()
.map(|i| i.to_string())
.collect::<Vec<String>>()
@ -760,7 +760,7 @@ async fn update_playout_config(
let mut config = manager.config.lock().unwrap();
let (filler_path, _, _) = norm_abs_path(
&config.global.storage_path,
&data.storage.filler.to_string_lossy().to_string(),
data.storage.filler.to_string_lossy().as_ref(),
)?;
config.general.stop_threshold = data.general.stop_threshold;

View File

@ -365,6 +365,7 @@ pub async fn insert_user(
.bind(user.role_id)
.bind(
user.channel_ids
.unwrap_or_default()
.iter()
.map(|i| i.to_string())
.collect::<Vec<String>>()

View File

@ -65,7 +65,7 @@ pub struct User {
pub password: String,
pub role_id: Option<i32>,
// #[serde_as(as = "StringWithSeparator::<CommaSeparator, i32>")]
pub channel_ids: Vec<i32>,
pub channel_ids: Option<Vec<i32>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
}
@ -78,12 +78,13 @@ impl FromRow<'_, SqliteRow> for User {
username: row.try_get("username").unwrap_or_default(),
password: row.try_get("password").unwrap_or_default(),
role_id: row.try_get("role_id").unwrap_or_default(),
channel_ids: row
.try_get::<String, &str>("channel_ids")
.unwrap_or_default()
.split(',')
.map(|i| i.parse::<i32>().unwrap_or_default())
.collect(),
channel_ids: Some(
row.try_get::<String, &str>("channel_ids")
.unwrap_or_default()
.split(',')
.map(|i| i.parse::<i32>().unwrap_or_default())
.collect(),
),
token: None,
})
}
@ -233,7 +234,7 @@ where
#[derive(Clone, Debug, Default, Deserialize, Serialize, sqlx::FromRow)]
pub struct Channel {
#[serde(skip_deserializing)]
#[serde(default = "default_id", skip_deserializing)]
pub id: i32,
pub name: String,
pub preview_url: String,
@ -247,13 +248,8 @@ pub struct Channel {
pub utc_offset: i32,
}
impl Channel {
pub fn default() -> Self {
Self {
id: 1,
..Default::default()
}
}
fn default_id() -> i32 {
1
}
#[derive(Clone, Debug, Deserialize, Serialize, sqlx::FromRow)]

View File

@ -267,11 +267,13 @@ pub async fn run_args(pool: &Pool<Sqlite>) -> Result<(), i32> {
username: username.clone(),
password: args.password.unwrap(),
role_id: Some(1),
channel_ids: channels
.unwrap_or(vec![Channel::default()])
.iter()
.map(|c| c.id)
.collect(),
channel_ids: Some(
channels
.unwrap_or(vec![Channel::default()])
.iter()
.map(|c| c.id)
.collect(),
),
token: None,
};

View File

@ -57,7 +57,7 @@ CREATE TABLE
username TEXT NOT NULL,
password TEXT NOT NULL,
role_id INTEGER NOT NULL DEFAULT 3,
channel_ids TEXT NOT NULL DEFAULT "1",
channel_ids TEXT DEFAULT "1",
FOREIGN KEY (role_id) REFERENCES roles (id) ON UPDATE SET NULL ON DELETE SET DEFAULT,
UNIQUE (mail, username)
);