Lines
83.33 %
Functions
50 %
Branches
100 %
//! The `commodity-create` form submit: a symbol and a name.
use forms::{COMMODITY_CREATE, FormValues};
use server::command::CmdResult;
use server::command::commodity::CreateCommodity;
use uuid::Uuid;
use super::Failure;
use super::transaction_rows::single_line;
const CTX: &str = "commodity-create";
fn internal(message: String) -> Failure {
Failure::Internal(wasmtime::Error::msg(format!("{CTX}: {message}")))
}
/// `commodity-create` submit: the new commodity's id.
pub(super) async fn create_commodity(user_id: Uuid, values: &FormValues) -> Result<Uuid, Failure> {
let mut errors = values.check(&COMMODITY_CREATE);
for key in ["symbol", "name"] {
let text = values.text(key).unwrap_or_default().trim();
errors.extend(single_line(key, text).err());
if !errors.is_empty() {
return Err(errors.into());
let symbol = values.text("symbol").unwrap_or_default().trim().to_string();
let name = values.text("name").unwrap_or_default().trim().to_string();
let created = CreateCommodity::new()
.symbol(symbol)
.name(name)
.user_id(user_id)
.run()
.await
.map_err(|err| Failure::from_command(CTX, err))?;
match created {
Some(CmdResult::String(id)) => {
Uuid::parse_str(&id).map_err(|err| internal(err.to_string()))
other => Err(internal(format!("unexpected create result {other:?}"))),