2023-04-28 14:08:53 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-08-18 05:16:42 +02:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2023-04-28 14:08:53 +08:00
|
|
|
|
|
|
|
use crate::entities::FieldType;
|
|
|
|
use crate::services::database::DatabaseEditor;
|
2024-04-11 14:49:36 +08:00
|
|
|
use crate::services::field::{MultiSelectTypeOption, SingleSelectTypeOption, TypeOption};
|
2023-04-28 14:08:53 +08:00
|
|
|
|
2024-04-11 14:49:36 +08:00
|
|
|
pub async fn edit_field_type_option<T: TypeOption>(
|
2023-04-28 14:08:53 +08:00
|
|
|
field_id: &str,
|
|
|
|
editor: Arc<DatabaseEditor>,
|
|
|
|
action: impl FnOnce(&mut T),
|
|
|
|
) -> FlowyResult<()> {
|
2024-08-18 05:16:42 +02:00
|
|
|
let field = editor
|
|
|
|
.get_field(field_id)
|
|
|
|
.await
|
|
|
|
.ok_or_else(FlowyError::field_record_not_found)?;
|
|
|
|
let field_type = FieldType::from(field.field_type);
|
|
|
|
let get_type_option = field.get_type_option::<T>(field_type);
|
2023-04-28 14:08:53 +08:00
|
|
|
|
2024-08-18 05:16:42 +02:00
|
|
|
if let Some(mut type_option) = get_type_option {
|
|
|
|
if let Some(old_field) = editor.get_field(field_id).await {
|
2023-04-28 14:08:53 +08:00
|
|
|
action(&mut type_option);
|
2024-04-11 14:49:36 +08:00
|
|
|
let type_option_data = type_option.into();
|
2023-04-28 14:08:53 +08:00
|
|
|
editor
|
2023-12-29 08:11:26 +08:00
|
|
|
.update_field_type_option(field_id, type_option_data, old_field)
|
2023-04-28 14:08:53 +08:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn edit_single_select_type_option(
|
|
|
|
field_id: &str,
|
|
|
|
editor: Arc<DatabaseEditor>,
|
|
|
|
action: impl FnOnce(&mut SingleSelectTypeOption),
|
|
|
|
) -> FlowyResult<()> {
|
2023-12-29 08:11:26 +08:00
|
|
|
edit_field_type_option(field_id, editor, action).await
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn edit_multi_select_type_option(
|
|
|
|
field_id: &str,
|
|
|
|
editor: Arc<DatabaseEditor>,
|
|
|
|
action: impl FnOnce(&mut MultiSelectTypeOption),
|
|
|
|
) -> FlowyResult<()> {
|
2023-12-29 08:11:26 +08:00
|
|
|
edit_field_type_option(field_id, editor, action).await
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|