diff options
Diffstat (limited to 'crates/rocie-server/src/storage/sql/get/unit')
| -rw-r--r-- | crates/rocie-server/src/storage/sql/get/unit/mod.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/crates/rocie-server/src/storage/sql/get/unit/mod.rs b/crates/rocie-server/src/storage/sql/get/unit/mod.rs index 6f5d297..2c85970 100644 --- a/crates/rocie-server/src/storage/sql/get/unit/mod.rs +++ b/crates/rocie-server/src/storage/sql/get/unit/mod.rs @@ -57,6 +57,34 @@ impl Unit { Ok(None) } } + + pub(crate) async fn from_name(app: &App, name: &str) -> Result<Option<Self>, from_name::Error> { + let record = query!( + " + SELECT id, full_name_singular, unit_property, full_name_plural, short_name, description + FROM units + WHERE full_name_singular = ? OR full_name_plural = ? OR short_name = ? +", + name, + name, + name + ) + .fetch_optional(&app.db) + .await?; + + if let Some(record) = record { + Ok(Some(Self { + id: UnitId::from_db(&record.id), + unit_property: UnitPropertyId::from_db(&record.unit_property), + full_name_singular: record.full_name_singular, + full_name_plural: record.full_name_plural, + short_name: record.short_name, + description: record.description, + })) + } else { + Ok(None) + } + } } pub(crate) mod get_all { @@ -82,3 +110,15 @@ pub(crate) mod from_id { impl ResponseError for Error {} } + +pub(crate) mod from_name { + use actix_web::ResponseError; + + #[derive(thiserror::Error, Debug)] + pub(crate) enum Error { + #[error("Failed to execute the sql query")] + SqlError(#[from] sqlx::Error), + } + + impl ResponseError for Error {} +} |
