add crud operations for Rule
This commit is contained in:
parent
a3b450736e
commit
357b81db80
@ -3,9 +3,10 @@ use std::fmt::Display;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
#[cfg(feature = "schemars")]
|
#[cfg(feature = "schemars")]
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::api::{DomainExtension, DomainObject, domain_bulk_read, domain_client};
|
use crate::api::{DomainExtension, DomainObject, domain_bulk_read, domain_client, domain_create, domain_delete, domain_update};
|
||||||
use crate::{ApiClient, Client, Result};
|
use crate::{ApiClient, Client, Error, Result};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
@ -140,14 +141,22 @@ domain_bulk_read!(Ruleset, RulesetQuery);
|
|||||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||||
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
|
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
|
||||||
pub struct Rule {
|
pub struct Rule {
|
||||||
|
#[serde(default, skip_serializing_if="Option::is_none")]
|
||||||
|
pub folder_index: Option<usize>,
|
||||||
|
#[serde(default, skip_serializing_if="Option::is_none")]
|
||||||
|
pub id: Option<Uuid>,
|
||||||
|
|
||||||
pub ruleset: String,
|
pub ruleset: String,
|
||||||
pub folder: String,
|
pub folder: String,
|
||||||
pub folder_index: usize,
|
|
||||||
pub properties: RuleProperties,
|
pub properties: RuleProperties,
|
||||||
pub value_raw: String,
|
pub value_raw: String,
|
||||||
pub conditions: RuleCondition
|
pub conditions: RuleCondition
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DomainExtension for Rule {
|
||||||
|
const DOMAIN_TYPE: super::DomainType = super::DomainType::Rule;
|
||||||
|
}
|
||||||
|
|
||||||
impl Rule {
|
impl Rule {
|
||||||
pub fn new(ruleset: String, folder: String) -> Self {
|
pub fn new(ruleset: String, folder: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -173,14 +182,6 @@ impl Rule {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_folder_index(&mut self, index: usize) {
|
|
||||||
self.folder_index = index;
|
|
||||||
}
|
|
||||||
pub fn with_folder_index(mut self, index: usize) -> Self {
|
|
||||||
self.set_folder_index(index);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_properties(&mut self, properties: RuleProperties) {
|
pub fn set_properties(&mut self, properties: RuleProperties) {
|
||||||
self.properties = properties;
|
self.properties = properties;
|
||||||
}
|
}
|
||||||
@ -461,6 +462,34 @@ pub enum LabelOperator {
|
|||||||
#[default] And, Or, Not
|
#[default] And, Or, Not
|
||||||
}
|
}
|
||||||
|
|
||||||
|
domain_client!(Rule, rule_api);
|
||||||
|
domain_create!(Rule, Rule);
|
||||||
|
domain_update!(Rule, Rule);
|
||||||
|
domain_delete!(Rule);
|
||||||
|
|
||||||
|
impl ApiClient<Rule> {
|
||||||
|
pub async fn read(&self, id: Uuid) -> Result<Rule> {
|
||||||
|
let mut object = self.inner
|
||||||
|
.read_domain_object::<Rule, _>(id, &())
|
||||||
|
.await?;
|
||||||
|
object.extensions.id = Some(Uuid::parse_str(&object.id)?);
|
||||||
|
Ok(object.extensions)
|
||||||
|
}
|
||||||
|
pub async fn bulk_read(&self) -> Result<Vec<Rule>> {
|
||||||
|
let objects = self.inner
|
||||||
|
.bulk_read_domain_objects::<Rule, _>(&())
|
||||||
|
.await?;
|
||||||
|
let objects = objects.value
|
||||||
|
.into_iter()
|
||||||
|
.map(|mut object| {
|
||||||
|
object.extensions.id = Some(Uuid::parse_str(&object.id)?);
|
||||||
|
Ok::<_, Error>(object.extensions)
|
||||||
|
})
|
||||||
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
Ok(objects)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod serde_tags {
|
mod serde_tags {
|
||||||
use serde::{Deserialize, Serialize, de::{self, Visitor, Unexpected, Error}, ser::SerializeStruct};
|
use serde::{Deserialize, Serialize, de::{self, Visitor, Unexpected, Error}, ser::SerializeStruct};
|
||||||
|
|
||||||
|
|||||||
@ -32,6 +32,8 @@ pub enum Error {
|
|||||||
MissingHeader(&'static str),
|
MissingHeader(&'static str),
|
||||||
#[error("Failed to parse ETag header: {0}")]
|
#[error("Failed to parse ETag header: {0}")]
|
||||||
ParseEtag(#[source] std::string::FromUtf8Error),
|
ParseEtag(#[source] std::string::FromUtf8Error),
|
||||||
|
#[error("failed to parse domain object id: {0}")]
|
||||||
|
ParseId(#[from] uuid::Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user