Domain: User entity, AuthPort/PasswordHashPort/SecretStore ports. Adapters: auth (argon2 hashing, JWT tokens), secret-store (env-based), config-sqlite user repository, http-api auth routes + extractors. Application: auth_service. SPA: login page, auth client, protected router.
158 lines
4.5 KiB
Rust
158 lines
4.5 KiB
Rust
use crate::AppState;
|
|
use crate::extractors::AuthUser;
|
|
use api_types::{CreateWidgetDto, WidgetDto};
|
|
use application::ConfigService;
|
|
use axum::{
|
|
extract::{Path, State},
|
|
http::StatusCode,
|
|
response::Json,
|
|
};
|
|
use domain::{ConfigRepository, EventPublisher, WidgetStateReader};
|
|
|
|
type S<C, E, W, B, R, A, H> = State<AppState<C, E, W, B, R, A, H>>;
|
|
|
|
pub async fn list_widgets<C, E, W, B, R, A, H>(
|
|
_auth: AuthUser,
|
|
State(state): S<C, E, W, B, R, A, H>,
|
|
) -> 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, W, B, R, A, H>(
|
|
_auth: AuthUser,
|
|
State(state): S<C, E, W, B, R, A, H>,
|
|
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, W, B, R, A, H>(
|
|
_auth: AuthUser,
|
|
State(state): S<C, E, W, B, R, A, H>,
|
|
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, W, B, R, A, H>(
|
|
_auth: AuthUser,
|
|
State(state): S<C, E, W, B, R, A, H>,
|
|
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, W, B, R, A, H>(
|
|
_auth: AuthUser,
|
|
State(state): S<C, E, W, B, R, A, H>,
|
|
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)
|
|
}
|
|
|
|
pub async fn preview_widget<C, E, W, B, R, A, H>(
|
|
_auth: AuthUser,
|
|
State(state): S<C, E, W, B, R, A, H>,
|
|
Path(id): Path<u16>,
|
|
) -> Result<Json<serde_json::Value>, StatusCode>
|
|
where
|
|
C: ConfigRepository,
|
|
C::Error: std::fmt::Debug,
|
|
E: EventPublisher,
|
|
E::Error: std::fmt::Debug,
|
|
W: WidgetStateReader,
|
|
{
|
|
match state.widget_states.get_widget_state(id).await {
|
|
Some(ws) => {
|
|
let map: serde_json::Map<String, serde_json::Value> = ws
|
|
.data
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), domain_value_to_json(v)))
|
|
.collect();
|
|
Ok(Json(serde_json::Value::Object(map)))
|
|
}
|
|
None => Err(StatusCode::NOT_FOUND),
|
|
}
|
|
}
|
|
|
|
fn domain_value_to_json(v: &domain::Value) -> serde_json::Value {
|
|
match v {
|
|
domain::Value::Null => serde_json::Value::Null,
|
|
domain::Value::Bool(b) => serde_json::Value::Bool(*b),
|
|
domain::Value::Number(n) => serde_json::json!(n),
|
|
domain::Value::String(s) => serde_json::Value::String(s.clone()),
|
|
domain::Value::Array(arr) => {
|
|
serde_json::Value::Array(arr.iter().map(domain_value_to_json).collect())
|
|
}
|
|
domain::Value::Object(obj) => {
|
|
let map = obj
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), domain_value_to_json(v)))
|
|
.collect();
|
|
serde_json::Value::Object(map)
|
|
}
|
|
}
|
|
}
|