refactor adapters into modular file structure
config-sqlite: split into repository/ (per entity) + serialization/ (per type) + error.rs http-api: split into dto/ (per resource) + routes/ (per resource) tcp-server: split into broadcaster, event_bus, server, error rss: split parser from adapter, external tests media: split error, external tests
This commit is contained in:
@@ -1,285 +0,0 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct KeyMappingDto {
|
||||
pub source_path: String,
|
||||
pub target_key: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct WidgetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub display_hint: String,
|
||||
pub data_source_id: u16,
|
||||
pub mappings: Vec<KeyMappingDto>,
|
||||
pub max_data_size: u16,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreateWidgetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub display_hint: String,
|
||||
pub data_source_id: u16,
|
||||
pub mappings: Vec<KeyMappingDto>,
|
||||
#[serde(default = "default_max_data_size")]
|
||||
pub max_data_size: u16,
|
||||
}
|
||||
|
||||
fn default_max_data_size() -> u16 { 2048 }
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct DataSourceDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub source_type: String,
|
||||
pub poll_interval_secs: u64,
|
||||
pub url: Option<String>,
|
||||
pub api_key: Option<String>,
|
||||
pub headers: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct SizingDto {
|
||||
#[serde(rename = "type")]
|
||||
pub sizing_type: String,
|
||||
pub value: u16,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct LayoutNodeDto {
|
||||
#[serde(rename = "type")]
|
||||
pub node_type: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub widget_id: Option<u16>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub direction: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub gap: Option<u8>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub padding: Option<u8>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub children: Option<Vec<LayoutChildDto>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct LayoutChildDto {
|
||||
pub sizing: SizingDto,
|
||||
pub node: LayoutNodeDto,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct LayoutDto {
|
||||
pub root: LayoutNodeDto,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct PresetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub layout: LayoutDto,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreatePresetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub layout: LayoutDto,
|
||||
}
|
||||
|
||||
use domain::*;
|
||||
use std::time::Duration;
|
||||
|
||||
impl From<&WidgetConfig> for WidgetDto {
|
||||
fn from(w: &WidgetConfig) -> Self {
|
||||
Self {
|
||||
id: w.id,
|
||||
name: w.name.clone(),
|
||||
display_hint: match w.display_hint {
|
||||
DisplayHint::IconValue => "icon_value",
|
||||
DisplayHint::TextBlock => "text_block",
|
||||
DisplayHint::KeyValue => "key_value",
|
||||
}.into(),
|
||||
data_source_id: w.data_source_id,
|
||||
mappings: w.mappings.iter().map(|m| KeyMappingDto {
|
||||
source_path: m.source_path.clone(),
|
||||
target_key: m.target_key.clone(),
|
||||
}).collect(),
|
||||
max_data_size: w.max_data_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CreateWidgetDto {
|
||||
pub fn into_domain(self) -> Result<WidgetConfig, String> {
|
||||
let hint = match self.display_hint.as_str() {
|
||||
"icon_value" => DisplayHint::IconValue,
|
||||
"text_block" => DisplayHint::TextBlock,
|
||||
"key_value" => DisplayHint::KeyValue,
|
||||
h => return Err(format!("unknown display_hint: {h}")),
|
||||
};
|
||||
Ok(WidgetConfig {
|
||||
id: self.id,
|
||||
name: self.name,
|
||||
display_hint: hint,
|
||||
data_source_id: self.data_source_id,
|
||||
mappings: self.mappings.into_iter().map(|m| KeyMapping {
|
||||
source_path: m.source_path,
|
||||
target_key: m.target_key,
|
||||
}).collect(),
|
||||
max_data_size: self.max_data_size,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&DataSource> for DataSourceDto {
|
||||
fn from(ds: &DataSource) -> Self {
|
||||
Self {
|
||||
id: ds.id,
|
||||
name: ds.name.clone(),
|
||||
source_type: match ds.source_type {
|
||||
DataSourceType::Weather => "weather",
|
||||
DataSourceType::Media => "media",
|
||||
DataSourceType::Xtb => "xtb",
|
||||
DataSourceType::Rss => "rss",
|
||||
DataSourceType::HttpJson => "http_json",
|
||||
DataSourceType::Webhook => "webhook",
|
||||
}.into(),
|
||||
poll_interval_secs: ds.poll_interval.as_secs(),
|
||||
url: ds.config.url.clone(),
|
||||
api_key: ds.config.api_key.clone(),
|
||||
headers: ds.config.headers.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DataSourceDto {
|
||||
pub fn into_domain(self) -> Result<DataSource, String> {
|
||||
let source_type = match self.source_type.as_str() {
|
||||
"weather" => DataSourceType::Weather,
|
||||
"media" => DataSourceType::Media,
|
||||
"xtb" => DataSourceType::Xtb,
|
||||
"rss" => DataSourceType::Rss,
|
||||
"http_json" => DataSourceType::HttpJson,
|
||||
"webhook" => DataSourceType::Webhook,
|
||||
t => return Err(format!("unknown source_type: {t}")),
|
||||
};
|
||||
Ok(DataSource {
|
||||
id: self.id,
|
||||
name: self.name,
|
||||
source_type,
|
||||
poll_interval: Duration::from_secs(self.poll_interval_secs),
|
||||
config: DataSourceConfig {
|
||||
url: self.url,
|
||||
api_key: self.api_key,
|
||||
headers: self.headers,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&LayoutNode> for LayoutNodeDto {
|
||||
fn from(node: &LayoutNode) -> Self {
|
||||
match node {
|
||||
LayoutNode::Leaf(id) => Self {
|
||||
node_type: "leaf".into(),
|
||||
widget_id: Some(*id),
|
||||
direction: None, gap: None, padding: None, children: None,
|
||||
},
|
||||
LayoutNode::Container(c) => Self {
|
||||
node_type: "container".into(),
|
||||
widget_id: None,
|
||||
direction: Some(match c.direction {
|
||||
Direction::Row => "row",
|
||||
Direction::Column => "column",
|
||||
}.into()),
|
||||
gap: Some(c.gap),
|
||||
padding: Some(c.padding),
|
||||
children: Some(c.children.iter().map(|ch| LayoutChildDto {
|
||||
sizing: SizingDto {
|
||||
sizing_type: match ch.sizing {
|
||||
Sizing::Fixed(_) => "fixed".into(),
|
||||
Sizing::Flex(_) => "flex".into(),
|
||||
},
|
||||
value: match ch.sizing {
|
||||
Sizing::Fixed(v) => v,
|
||||
Sizing::Flex(v) => v as u16,
|
||||
},
|
||||
},
|
||||
node: (&ch.node).into(),
|
||||
}).collect()),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LayoutNodeDto {
|
||||
pub fn into_domain(self) -> Result<LayoutNode, String> {
|
||||
match self.node_type.as_str() {
|
||||
"leaf" => {
|
||||
let id = self.widget_id.ok_or("missing widget_id")?;
|
||||
Ok(LayoutNode::Leaf(id))
|
||||
}
|
||||
"container" => {
|
||||
let direction = match self.direction.as_deref().ok_or("missing direction")? {
|
||||
"row" => Direction::Row,
|
||||
"column" => Direction::Column,
|
||||
d => return Err(format!("unknown direction: {d}")),
|
||||
};
|
||||
let children = self.children.ok_or("missing children")?
|
||||
.into_iter()
|
||||
.map(|ch| {
|
||||
let sizing = match ch.sizing.sizing_type.as_str() {
|
||||
"fixed" => Sizing::Fixed(ch.sizing.value),
|
||||
"flex" => Sizing::Flex(ch.sizing.value as u8),
|
||||
s => return Err(format!("unknown sizing: {s}")),
|
||||
};
|
||||
let node = ch.node.into_domain()?;
|
||||
Ok(LayoutChild { sizing, node })
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(LayoutNode::Container(ContainerNode {
|
||||
direction,
|
||||
gap: self.gap.unwrap_or(0),
|
||||
padding: self.padding.unwrap_or(0),
|
||||
children,
|
||||
}))
|
||||
}
|
||||
t => Err(format!("unknown node type: {t}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Layout> for LayoutDto {
|
||||
fn from(l: &Layout) -> Self {
|
||||
Self { root: (&l.root).into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl LayoutDto {
|
||||
pub fn into_domain(self) -> Result<Layout, String> {
|
||||
Ok(Layout { root: self.root.into_domain()? })
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&LayoutPreset> for PresetDto {
|
||||
fn from(p: &LayoutPreset) -> Self {
|
||||
Self {
|
||||
id: p.id,
|
||||
name: p.name.clone(),
|
||||
layout: (&p.layout).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CreatePresetDto {
|
||||
pub fn into_domain(self) -> Result<LayoutPreset, String> {
|
||||
Ok(LayoutPreset {
|
||||
id: self.id,
|
||||
name: self.name,
|
||||
layout: self.layout.into_domain()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
60
crates/adapters/http-api/src/dto/data_source.rs
Normal file
60
crates/adapters/http-api/src/dto/data_source.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::time::Duration;
|
||||
use domain::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct DataSourceDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub source_type: String,
|
||||
pub poll_interval_secs: u64,
|
||||
pub url: Option<String>,
|
||||
pub api_key: Option<String>,
|
||||
pub headers: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
impl From<&DataSource> for DataSourceDto {
|
||||
fn from(ds: &DataSource) -> Self {
|
||||
Self {
|
||||
id: ds.id,
|
||||
name: ds.name.clone(),
|
||||
source_type: match ds.source_type {
|
||||
DataSourceType::Weather => "weather",
|
||||
DataSourceType::Media => "media",
|
||||
DataSourceType::Xtb => "xtb",
|
||||
DataSourceType::Rss => "rss",
|
||||
DataSourceType::HttpJson => "http_json",
|
||||
DataSourceType::Webhook => "webhook",
|
||||
}.into(),
|
||||
poll_interval_secs: ds.poll_interval.as_secs(),
|
||||
url: ds.config.url.clone(),
|
||||
api_key: ds.config.api_key.clone(),
|
||||
headers: ds.config.headers.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DataSourceDto {
|
||||
pub fn into_domain(self) -> Result<DataSource, String> {
|
||||
let source_type = match self.source_type.as_str() {
|
||||
"weather" => DataSourceType::Weather,
|
||||
"media" => DataSourceType::Media,
|
||||
"xtb" => DataSourceType::Xtb,
|
||||
"rss" => DataSourceType::Rss,
|
||||
"http_json" => DataSourceType::HttpJson,
|
||||
"webhook" => DataSourceType::Webhook,
|
||||
t => return Err(format!("unknown source_type: {t}")),
|
||||
};
|
||||
Ok(DataSource {
|
||||
id: self.id,
|
||||
name: self.name,
|
||||
source_type,
|
||||
poll_interval: Duration::from_secs(self.poll_interval_secs),
|
||||
config: DataSourceConfig {
|
||||
url: self.url,
|
||||
api_key: self.api_key,
|
||||
headers: self.headers,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
121
crates/adapters/http-api/src/dto/layout.rs
Normal file
121
crates/adapters/http-api/src/dto/layout.rs
Normal file
@@ -0,0 +1,121 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use domain::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct SizingDto {
|
||||
#[serde(rename = "type")]
|
||||
pub sizing_type: String,
|
||||
pub value: u16,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct LayoutNodeDto {
|
||||
#[serde(rename = "type")]
|
||||
pub node_type: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub widget_id: Option<u16>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub direction: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub gap: Option<u8>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub padding: Option<u8>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub children: Option<Vec<LayoutChildDto>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct LayoutChildDto {
|
||||
pub sizing: SizingDto,
|
||||
pub node: LayoutNodeDto,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct LayoutDto {
|
||||
pub root: LayoutNodeDto,
|
||||
}
|
||||
|
||||
impl From<&LayoutNode> for LayoutNodeDto {
|
||||
fn from(node: &LayoutNode) -> Self {
|
||||
match node {
|
||||
LayoutNode::Leaf(id) => Self {
|
||||
node_type: "leaf".into(),
|
||||
widget_id: Some(*id),
|
||||
direction: None, gap: None, padding: None, children: None,
|
||||
},
|
||||
LayoutNode::Container(c) => Self {
|
||||
node_type: "container".into(),
|
||||
widget_id: None,
|
||||
direction: Some(match c.direction {
|
||||
Direction::Row => "row",
|
||||
Direction::Column => "column",
|
||||
}.into()),
|
||||
gap: Some(c.gap),
|
||||
padding: Some(c.padding),
|
||||
children: Some(c.children.iter().map(|ch| LayoutChildDto {
|
||||
sizing: SizingDto {
|
||||
sizing_type: match ch.sizing {
|
||||
Sizing::Fixed(_) => "fixed".into(),
|
||||
Sizing::Flex(_) => "flex".into(),
|
||||
},
|
||||
value: match ch.sizing {
|
||||
Sizing::Fixed(v) => v,
|
||||
Sizing::Flex(v) => v as u16,
|
||||
},
|
||||
},
|
||||
node: (&ch.node).into(),
|
||||
}).collect()),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LayoutNodeDto {
|
||||
pub fn into_domain(self) -> Result<LayoutNode, String> {
|
||||
match self.node_type.as_str() {
|
||||
"leaf" => {
|
||||
let id = self.widget_id.ok_or("missing widget_id")?;
|
||||
Ok(LayoutNode::Leaf(id))
|
||||
}
|
||||
"container" => {
|
||||
let direction = match self.direction.as_deref().ok_or("missing direction")? {
|
||||
"row" => Direction::Row,
|
||||
"column" => Direction::Column,
|
||||
d => return Err(format!("unknown direction: {d}")),
|
||||
};
|
||||
let children = self.children.ok_or("missing children")?
|
||||
.into_iter()
|
||||
.map(|ch| {
|
||||
let sizing = match ch.sizing.sizing_type.as_str() {
|
||||
"fixed" => Sizing::Fixed(ch.sizing.value),
|
||||
"flex" => Sizing::Flex(ch.sizing.value as u8),
|
||||
s => return Err(format!("unknown sizing: {s}")),
|
||||
};
|
||||
let node = ch.node.into_domain()?;
|
||||
Ok(LayoutChild { sizing, node })
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(LayoutNode::Container(ContainerNode {
|
||||
direction,
|
||||
gap: self.gap.unwrap_or(0),
|
||||
padding: self.padding.unwrap_or(0),
|
||||
children,
|
||||
}))
|
||||
}
|
||||
t => Err(format!("unknown node type: {t}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Layout> for LayoutDto {
|
||||
fn from(l: &Layout) -> Self {
|
||||
Self { root: (&l.root).into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl LayoutDto {
|
||||
pub fn into_domain(self) -> Result<Layout, String> {
|
||||
Ok(Layout { root: self.root.into_domain()? })
|
||||
}
|
||||
}
|
||||
9
crates/adapters/http-api/src/dto/mod.rs
Normal file
9
crates/adapters/http-api/src/dto/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
pub mod widget;
|
||||
pub mod data_source;
|
||||
pub mod layout;
|
||||
pub mod preset;
|
||||
|
||||
pub use widget::{KeyMappingDto, WidgetDto, CreateWidgetDto};
|
||||
pub use data_source::DataSourceDto;
|
||||
pub use layout::{LayoutDto, LayoutNodeDto, LayoutChildDto, SizingDto};
|
||||
pub use preset::{PresetDto, CreatePresetDto};
|
||||
37
crates/adapters/http-api/src/dto/preset.rs
Normal file
37
crates/adapters/http-api/src/dto/preset.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use domain::*;
|
||||
use super::layout::LayoutDto;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct PresetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub layout: LayoutDto,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreatePresetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub layout: LayoutDto,
|
||||
}
|
||||
|
||||
impl From<&LayoutPreset> for PresetDto {
|
||||
fn from(p: &LayoutPreset) -> Self {
|
||||
Self {
|
||||
id: p.id,
|
||||
name: p.name.clone(),
|
||||
layout: (&p.layout).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CreatePresetDto {
|
||||
pub fn into_domain(self) -> Result<LayoutPreset, String> {
|
||||
Ok(LayoutPreset {
|
||||
id: self.id,
|
||||
name: self.name,
|
||||
layout: self.layout.into_domain()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
73
crates/adapters/http-api/src/dto/widget.rs
Normal file
73
crates/adapters/http-api/src/dto/widget.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use domain::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct KeyMappingDto {
|
||||
pub source_path: String,
|
||||
pub target_key: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct WidgetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub display_hint: String,
|
||||
pub data_source_id: u16,
|
||||
pub mappings: Vec<KeyMappingDto>,
|
||||
pub max_data_size: u16,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreateWidgetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub display_hint: String,
|
||||
pub data_source_id: u16,
|
||||
pub mappings: Vec<KeyMappingDto>,
|
||||
#[serde(default = "default_max_data_size")]
|
||||
pub max_data_size: u16,
|
||||
}
|
||||
|
||||
fn default_max_data_size() -> u16 { 2048 }
|
||||
|
||||
impl From<&WidgetConfig> for WidgetDto {
|
||||
fn from(w: &WidgetConfig) -> Self {
|
||||
Self {
|
||||
id: w.id,
|
||||
name: w.name.clone(),
|
||||
display_hint: match w.display_hint {
|
||||
DisplayHint::IconValue => "icon_value",
|
||||
DisplayHint::TextBlock => "text_block",
|
||||
DisplayHint::KeyValue => "key_value",
|
||||
}.into(),
|
||||
data_source_id: w.data_source_id,
|
||||
mappings: w.mappings.iter().map(|m| KeyMappingDto {
|
||||
source_path: m.source_path.clone(),
|
||||
target_key: m.target_key.clone(),
|
||||
}).collect(),
|
||||
max_data_size: w.max_data_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CreateWidgetDto {
|
||||
pub fn into_domain(self) -> Result<WidgetConfig, String> {
|
||||
let hint = match self.display_hint.as_str() {
|
||||
"icon_value" => DisplayHint::IconValue,
|
||||
"text_block" => DisplayHint::TextBlock,
|
||||
"key_value" => DisplayHint::KeyValue,
|
||||
h => return Err(format!("unknown display_hint: {h}")),
|
||||
};
|
||||
Ok(WidgetConfig {
|
||||
id: self.id,
|
||||
name: self.name,
|
||||
display_hint: hint,
|
||||
data_source_id: self.data_source_id,
|
||||
mappings: self.mappings.into_iter().map(|m| KeyMapping {
|
||||
source_path: m.source_path,
|
||||
target_key: m.target_key,
|
||||
}).collect(),
|
||||
max_data_size: self.max_data_size,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
use axum::{
|
||||
Router,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::Json,
|
||||
routing::{get, post, put, delete},
|
||||
};
|
||||
use domain::{ConfigRepository, EventPublisher};
|
||||
use application::ConfigService;
|
||||
use crate::AppState;
|
||||
use crate::dto::*;
|
||||
|
||||
type S<C, E> = State<AppState<C, E>>;
|
||||
|
||||
pub fn api_routes<C, E>() -> Router<AppState<C, E>>
|
||||
where
|
||||
C: ConfigRepository + Send + Sync + 'static,
|
||||
C::Error: std::fmt::Debug + Send,
|
||||
E: EventPublisher + Send + Sync + 'static,
|
||||
E::Error: std::fmt::Debug + Send,
|
||||
{
|
||||
Router::new()
|
||||
.route("/widgets", get(list_widgets::<C, E>).post(create_widget::<C, E>))
|
||||
.route("/widgets/{id}", get(get_widget::<C, E>).put(update_widget::<C, E>).delete(delete_widget::<C, E>))
|
||||
.route("/data-sources", get(list_data_sources::<C, E>).post(create_data_source::<C, E>))
|
||||
.route("/data-sources/{id}", get(get_data_source::<C, E>).put(update_data_source::<C, E>).delete(delete_data_source::<C, E>))
|
||||
.route("/layout", get(get_layout::<C, E>).put(update_layout::<C, E>))
|
||||
.route("/presets", get(list_presets::<C, E>).post(create_preset::<C, E>))
|
||||
.route("/presets/{id}", get(get_preset::<C, E>).delete(delete_preset::<C, E>))
|
||||
.route("/presets/{id}/load", post(load_preset::<C, E>))
|
||||
}
|
||||
|
||||
async fn list_widgets<C, E>(State(state): S<C, E>) -> Result<Json<Vec<WidgetDto>>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let widgets = state.config.list_widgets().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(widgets.iter().map(WidgetDto::from).collect()))
|
||||
}
|
||||
|
||||
async fn get_widget<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<Json<WidgetDto>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let widget = state.config.get_widget(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
match widget {
|
||||
Some(w) => Ok(Json(WidgetDto::from(&w))),
|
||||
None => Err(StatusCode::NOT_FOUND),
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_widget<C, E>(State(state): S<C, E>, Json(body): Json<CreateWidgetDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let widget = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.create_widget(widget).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
async fn update_widget<C, E>(State(state): S<C, E>, Path(_id): Path<u16>, Json(body): Json<CreateWidgetDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let widget = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.update_widget(widget).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
async fn delete_widget<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.delete_widget(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn list_data_sources<C, E>(State(state): S<C, E>) -> Result<Json<Vec<DataSourceDto>>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let sources = state.config.list_data_sources().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(sources.iter().map(DataSourceDto::from).collect()))
|
||||
}
|
||||
|
||||
async fn get_data_source<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<Json<DataSourceDto>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let source = state.config.get_data_source(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
match source {
|
||||
Some(s) => Ok(Json(DataSourceDto::from(&s))),
|
||||
None => Err(StatusCode::NOT_FOUND),
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_data_source<C, E>(State(state): S<C, E>, Json(body): Json<DataSourceDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let source = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.create_data_source(source).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
async fn update_data_source<C, E>(State(state): S<C, E>, Path(_id): Path<u16>, Json(body): Json<DataSourceDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let source = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.update_data_source(source).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
async fn delete_data_source<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.delete_data_source(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn get_layout<C, E>(State(state): S<C, E>) -> Result<Json<Option<LayoutDto>>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let layout = state.config.get_layout().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(layout.as_ref().map(LayoutDto::from)))
|
||||
}
|
||||
|
||||
async fn update_layout<C, E>(State(state): S<C, E>, Json(body): Json<LayoutDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let layout = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.update_layout(layout).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
async fn list_presets<C, E>(State(state): S<C, E>) -> Result<Json<Vec<PresetDto>>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let presets = state.config.list_presets().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(presets.iter().map(PresetDto::from).collect()))
|
||||
}
|
||||
|
||||
async fn get_preset<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<Json<PresetDto>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let preset = state.config.get_preset(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
match preset {
|
||||
Some(p) => Ok(Json(PresetDto::from(&p))),
|
||||
None => Err(StatusCode::NOT_FOUND),
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_preset<C, E>(State(state): S<C, E>, Json(body): Json<CreatePresetDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let preset = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.save_preset(preset).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
async fn delete_preset<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.delete_preset(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn load_preset<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.load_preset(id).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
54
crates/adapters/http-api/src/routes/data_sources.rs
Normal file
54
crates/adapters/http-api/src/routes/data_sources.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::Json,
|
||||
};
|
||||
use domain::{ConfigRepository, EventPublisher};
|
||||
use application::ConfigService;
|
||||
use crate::AppState;
|
||||
use crate::dto::DataSourceDto;
|
||||
|
||||
type S<C, E> = State<AppState<C, E>>;
|
||||
|
||||
pub async fn list_data_sources<C, E>(State(state): S<C, E>) -> Result<Json<Vec<DataSourceDto>>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let sources = state.config.list_data_sources().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(sources.iter().map(DataSourceDto::from).collect()))
|
||||
}
|
||||
|
||||
pub async fn get_data_source<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<Json<DataSourceDto>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let source = state.config.get_data_source(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
match source {
|
||||
Some(s) => Ok(Json(DataSourceDto::from(&s))),
|
||||
None => Err(StatusCode::NOT_FOUND),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_data_source<C, E>(State(state): S<C, E>, Json(body): Json<DataSourceDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let source = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.create_data_source(source).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
pub async fn update_data_source<C, E>(State(state): S<C, E>, Path(_id): Path<u16>, Json(body): Json<DataSourceDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let source = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.update_data_source(source).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
pub async fn delete_data_source<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.delete_data_source(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
27
crates/adapters/http-api/src/routes/layout.rs
Normal file
27
crates/adapters/http-api/src/routes/layout.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
response::Json,
|
||||
};
|
||||
use domain::{ConfigRepository, EventPublisher};
|
||||
use application::ConfigService;
|
||||
use crate::AppState;
|
||||
use crate::dto::LayoutDto;
|
||||
|
||||
type S<C, E> = State<AppState<C, E>>;
|
||||
|
||||
pub async fn get_layout<C, E>(State(state): S<C, E>) -> Result<Json<Option<LayoutDto>>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let layout = state.config.get_layout().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(layout.as_ref().map(LayoutDto::from)))
|
||||
}
|
||||
|
||||
pub async fn update_layout<C, E>(State(state): S<C, E>, Json(body): Json<LayoutDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let layout = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.update_layout(layout).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
27
crates/adapters/http-api/src/routes/mod.rs
Normal file
27
crates/adapters/http-api/src/routes/mod.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
mod widgets;
|
||||
mod data_sources;
|
||||
mod layout;
|
||||
mod presets;
|
||||
|
||||
use axum::Router;
|
||||
use axum::routing::{get, post, put, delete};
|
||||
use domain::{ConfigRepository, EventPublisher};
|
||||
use crate::AppState;
|
||||
|
||||
pub fn api_routes<C, E>() -> Router<AppState<C, E>>
|
||||
where
|
||||
C: ConfigRepository + Send + Sync + 'static,
|
||||
C::Error: std::fmt::Debug + Send,
|
||||
E: EventPublisher + Send + Sync + 'static,
|
||||
E::Error: std::fmt::Debug + Send,
|
||||
{
|
||||
Router::new()
|
||||
.route("/widgets", get(widgets::list_widgets::<C, E>).post(widgets::create_widget::<C, E>))
|
||||
.route("/widgets/{id}", get(widgets::get_widget::<C, E>).put(widgets::update_widget::<C, E>).delete(widgets::delete_widget::<C, E>))
|
||||
.route("/data-sources", get(data_sources::list_data_sources::<C, E>).post(data_sources::create_data_source::<C, E>))
|
||||
.route("/data-sources/{id}", get(data_sources::get_data_source::<C, E>).put(data_sources::update_data_source::<C, E>).delete(data_sources::delete_data_source::<C, E>))
|
||||
.route("/layout", get(layout::get_layout::<C, E>).put(layout::update_layout::<C, E>))
|
||||
.route("/presets", get(presets::list_presets::<C, E>).post(presets::create_preset::<C, E>))
|
||||
.route("/presets/{id}", get(presets::get_preset::<C, E>).delete(presets::delete_preset::<C, E>))
|
||||
.route("/presets/{id}/load", post(presets::load_preset::<C, E>))
|
||||
}
|
||||
53
crates/adapters/http-api/src/routes/presets.rs
Normal file
53
crates/adapters/http-api/src/routes/presets.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::Json,
|
||||
};
|
||||
use domain::{ConfigRepository, EventPublisher};
|
||||
use application::ConfigService;
|
||||
use crate::AppState;
|
||||
use crate::dto::{PresetDto, CreatePresetDto};
|
||||
|
||||
type S<C, E> = State<AppState<C, E>>;
|
||||
|
||||
pub async fn list_presets<C, E>(State(state): S<C, E>) -> Result<Json<Vec<PresetDto>>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let presets = state.config.list_presets().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(presets.iter().map(PresetDto::from).collect()))
|
||||
}
|
||||
|
||||
pub async fn get_preset<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<Json<PresetDto>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let preset = state.config.get_preset(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
match preset {
|
||||
Some(p) => Ok(Json(PresetDto::from(&p))),
|
||||
None => Err(StatusCode::NOT_FOUND),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_preset<C, E>(State(state): S<C, E>, Json(body): Json<CreatePresetDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let preset = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.save_preset(preset).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
pub async fn delete_preset<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.delete_preset(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
pub async fn load_preset<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.load_preset(id).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
54
crates/adapters/http-api/src/routes/widgets.rs
Normal file
54
crates/adapters/http-api/src/routes/widgets.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::Json,
|
||||
};
|
||||
use domain::{ConfigRepository, EventPublisher};
|
||||
use application::ConfigService;
|
||||
use crate::AppState;
|
||||
use crate::dto::{WidgetDto, CreateWidgetDto};
|
||||
|
||||
type S<C, E> = State<AppState<C, E>>;
|
||||
|
||||
pub async fn list_widgets<C, E>(State(state): S<C, E>) -> Result<Json<Vec<WidgetDto>>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let widgets = state.config.list_widgets().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(widgets.iter().map(WidgetDto::from).collect()))
|
||||
}
|
||||
|
||||
pub async fn get_widget<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<Json<WidgetDto>, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let widget = state.config.get_widget(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
match widget {
|
||||
Some(w) => Ok(Json(WidgetDto::from(&w))),
|
||||
None => Err(StatusCode::NOT_FOUND),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_widget<C, E>(State(state): S<C, E>, Json(body): Json<CreateWidgetDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let widget = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.create_widget(widget).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
pub async fn update_widget<C, E>(State(state): S<C, E>, Path(_id): Path<u16>, Json(body): Json<CreateWidgetDto>) -> Result<StatusCode, (StatusCode, String)>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let widget = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.update_widget(widget).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
pub async fn delete_widget<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, StatusCode>
|
||||
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
|
||||
{
|
||||
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
|
||||
svc.delete_widget(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
Reference in New Issue
Block a user