117 lines
3.5 KiB
Rust
117 lines
3.5 KiB
Rust
use sea_orm::{ActiveModelTrait, ActiveValue::Set, DatabaseConnection, EntityTrait, prelude::*};
|
|
use uuid::Uuid;
|
|
|
|
use crate::service::proxy::types::{LimitZoneConfig, ProxyServiceError, ProxyServiceResult};
|
|
|
|
pub struct CreateLimitZoneParams {
|
|
pub name: String,
|
|
pub key: String,
|
|
pub rate: String,
|
|
pub override_of_id: Option<Uuid>,
|
|
}
|
|
|
|
pub struct UpdateLimitZoneParams {
|
|
pub name: Option<String>,
|
|
pub key: Option<String>,
|
|
pub rate: Option<String>,
|
|
pub override_of_id: Option<Option<Uuid>>,
|
|
}
|
|
|
|
#[cfg_attr(test, mockall::automock)]
|
|
#[async_trait::async_trait]
|
|
pub trait LimitZoneService: Send + Sync + 'static {
|
|
async fn get(&self, id: Uuid) -> ProxyServiceResult<LimitZoneConfig>;
|
|
async fn list(&self) -> ProxyServiceResult<Vec<LimitZoneConfig>>;
|
|
async fn create(&self, params: CreateLimitZoneParams) -> ProxyServiceResult<LimitZoneConfig>;
|
|
async fn update(
|
|
&self,
|
|
id: Uuid,
|
|
params: UpdateLimitZoneParams,
|
|
) -> ProxyServiceResult<LimitZoneConfig>;
|
|
async fn delete(&self, id: Uuid) -> ProxyServiceResult<bool>;
|
|
}
|
|
|
|
pub(crate) struct LimitZoneServiceImpl {
|
|
db: DatabaseConnection,
|
|
}
|
|
|
|
impl LimitZoneServiceImpl {
|
|
pub fn new(db: DatabaseConnection) -> Self {
|
|
Self { db }
|
|
}
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
impl LimitZoneService for LimitZoneServiceImpl {
|
|
async fn get(&self, id: Uuid) -> ProxyServiceResult<LimitZoneConfig> {
|
|
use crate::db::entities::limit_zone;
|
|
|
|
let model = limit_zone::Entity::find_by_id(id)
|
|
.one(&self.db)
|
|
.await?
|
|
.ok_or(ProxyServiceError::ConfigNotFound)?;
|
|
|
|
Ok(model.into())
|
|
}
|
|
|
|
async fn list(&self) -> ProxyServiceResult<Vec<LimitZoneConfig>> {
|
|
use crate::db::entities::limit_zone;
|
|
|
|
let models = limit_zone::Entity::find().all(&self.db).await?;
|
|
|
|
Ok(models.into_iter().map(Into::into).collect())
|
|
}
|
|
|
|
async fn create(&self, params: CreateLimitZoneParams) -> ProxyServiceResult<LimitZoneConfig> {
|
|
use crate::db::entities::limit_zone::ActiveModel;
|
|
|
|
let model = ActiveModel {
|
|
id: Set(Uuid::new_v4()),
|
|
name: Set(params.name),
|
|
key: Set(params.key),
|
|
rate: Set(params.rate),
|
|
override_of_id: Set(params.override_of_id),
|
|
};
|
|
|
|
let result = model.insert(&self.db).await?;
|
|
Ok(result.into())
|
|
}
|
|
|
|
async fn update(
|
|
&self,
|
|
id: Uuid,
|
|
params: UpdateLimitZoneParams,
|
|
) -> ProxyServiceResult<LimitZoneConfig> {
|
|
use crate::db::entities::limit_zone::{ActiveModel, Entity as LimitZoneEntity};
|
|
|
|
let existing = LimitZoneEntity::find_by_id(id)
|
|
.one(&self.db)
|
|
.await?
|
|
.ok_or(ProxyServiceError::ConfigNotFound)?;
|
|
|
|
let mut model: ActiveModel = existing.into();
|
|
if let Some(name) = params.name {
|
|
model.name = Set(name);
|
|
}
|
|
if let Some(key) = params.key {
|
|
model.key = Set(key);
|
|
}
|
|
if let Some(rate) = params.rate {
|
|
model.rate = Set(rate);
|
|
}
|
|
if let Some(override_of_id) = params.override_of_id {
|
|
model.override_of_id = Set(override_of_id);
|
|
}
|
|
|
|
let result = model.update(&self.db).await?;
|
|
Ok(result.into())
|
|
}
|
|
|
|
async fn delete(&self, id: Uuid) -> ProxyServiceResult<bool> {
|
|
let result = crate::db::entities::limit_zone::Entity::delete_by_id(id)
|
|
.exec(&self.db)
|
|
.await?;
|
|
Ok(result.rows_affected > 0)
|
|
}
|
|
}
|