summaryrefslogtreecommitdiff
path: root/src/models.rs
diff options
context:
space:
mode:
authorbatsumaru <>2026-10-16 15:10:58 +0900
committerbatsumaru <>2026-10-16 15:10:58 +0900
commitad9a134daec1f73940e8deaeb6c2a481ce9d6520 (patch)
treef8a1f9093434af0899f53fdf634324282506045f /src/models.rs
parentbaaf466ff41c52a4e39624442ec2b122727feb89 (diff)
Clean up endpoint semantics
Diffstat (limited to 'src/models.rs')
-rw-r--r--src/models.rs60
1 files changed, 39 insertions, 21 deletions
diff --git a/src/models.rs b/src/models.rs
index 9e14b71..6b44040 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use strum::{EnumIter, IntoStaticStr};
use uuid::Uuid;
-#[derive(sqlx::Type, Serialize, Deserialize, EnumIter, Debug, IntoStaticStr)]
+#[derive(sqlx::Type, Serialize, Deserialize, EnumIter, Debug, IntoStaticStr, Clone, Copy)]
#[sqlx(type_name = "metric_type")]
pub enum MetricType {
BodyWeight,
@@ -12,6 +12,17 @@ pub enum MetricType {
Waist,
}
+impl MetricType {
+ pub fn accepts(&self, unit: &Unit) -> bool {
+ matches!(
+ (self, unit),
+ (MetricType::BodyWeight, Unit::Kilogram)
+ | (MetricType::Hip, Unit::Centimeter)
+ | (MetricType::Waist, Unit::Centimeter)
+ )
+ }
+}
+
#[derive(sqlx::Type, Serialize, Deserialize, EnumIter, Debug, IntoStaticStr)]
#[sqlx(type_name = "unit")]
pub enum Unit {
@@ -52,35 +63,42 @@ impl TryFrom<MeasurementDataUnchecked> for MeasurementData {
if value.value < 0.0 || value.value.is_nan() {
return Err("Invalid value".to_string());
}
- match (&value.metric_type, &value.unit) {
- (MetricType::BodyWeight, Unit::Kilogram)
- | (MetricType::Hip, Unit::Centimeter)
- | (MetricType::Waist, Unit::Centimeter) => Ok(Self {
- measured_at: value.measured_at,
- metric_type: value.metric_type,
- value: value.value,
- unit: value.unit,
- }),
- (metric_type, unit) => {
- let metric_type: &'static str = metric_type.into();
- let unit: &'static str = unit.into();
- Err(format!("Invalid pair {}, {}", metric_type, unit))
- }
+ if !value.metric_type.accepts(&value.unit) {
+ let metric_type: &'static str = (&value.metric_type).into();
+ let unit: &'static str = (&value.unit).into();
+ return Err(format!("Invalid pair {}, {}", metric_type, unit));
}
+ Ok(Self {
+ measured_at: value.measured_at,
+ metric_type: value.metric_type,
+ value: value.value,
+ unit: value.unit,
+ })
}
}
+#[derive(Serialize)]
+pub struct MetricUnitPair {
+ pub metric_type: MetricType,
+ pub unit: Unit,
+}
+
+#[derive(Deserialize, Default)]
+#[serde(rename_all = "lowercase")]
+pub enum Order {
+ #[default]
+ Asc,
+ Desc,
+}
+
#[derive(Deserialize)]
-pub struct DateQuery {
+pub struct ListQuery {
pub date: Option<NaiveDate>,
pub from: Option<NaiveDate>,
pub to: Option<NaiveDate>,
-}
-
-#[derive(Deserialize)]
-pub struct AllQuery {
pub limit: Option<i64>,
- pub reverse: Option<bool>,
+ #[serde(default)]
+ pub order: Order,
}
#[cfg(test)]