pub async fn save_function(mut req: Request<AppState>) -> tide::Result {
    let item: UserFunctionDeclaration = req.body_json().await?;
    let name = &item.name;

    // Check if the cron string is valid.
    match &item.trigger {
        Trigger::Interval(cron_str) => {
            let e = cron_str.parse::<cron::Schedule>();
            if e.is_err() {
                error!("Couldn't parse CRON string ({}): {:?}", cron_str, e);
            }
            e.map_err(|e| tide::Error::from_str(StatusCode::BadRequest, e.to_string()))?;
        }
        _ => (),
    }
    debug!(
        "Saving function with Name: {}, Trigger: {:?}, Code: {}",
        name, item.trigger, item.code
    );
    if !name.trim().is_empty() {
        let (storage, connection) = req.state();

        let new_record = match storage.get(&name).await {
            Some(f) => {
                // if a function is already saved it needs to be updated
                let env_id = f.environment_id.clone();

                // ... and for that we have to disable the function
                match connection.send(RuntimeRequest::Disable(f)).await {
                    Ok(_) => Ok(()),
                    Err(e) => {
                        error!(
                            "Couldn't disable the runtime for '{}' at this time. Aborting",
                            env_id
                        );
                        Err(e)
                    }
                }?;

                // ... and create a new record with the same environment as the old one
                UserFunctionRecord::new(item.clone(), env_id)
            }
            None => UserFunctionRecord::from(item.clone()),
        };

        // replace the exisiting function
        storage.set(name.clone(), new_record).await;
        let code = storage
            .get(&name)
            .await
            .ok_or_else(|| AnyError::msg(format!("Function couldn't be found: {}", name)))?;

        connection.send(RuntimeRequest::NewFunction(code)).await?;
        Ok(Response::new(StatusCode::Ok))
    } else {
        error!("ERROR :(");

        Err(tide::Error::from_str(
            StatusCode::BadRequest,
            format!("Name '{}' is invalid", name),
        ))
    }
}
///
/// API call to save a function using a JSON object.
///
API call to save a function using a JSON object.
API call to save a function using a JSON object .
pub async fn save_function(mut req: Request<AppState>) -> tide::Result {
    let item: UserFunctionDeclaration = req.body_json().await?;
    let name = &item.name;
    // Check if the cron string is valid.
    match &item.trigger {
        Trigger::Interval(cron_str) => {
            let e = cron_str.parse::<cron::Schedule>();
            if e.is_err() {
                error!("Couldn't parse CRON string ({}): {:?}", cron_str, e);
            }
            e.map_err(|e| tide::Error::from_str(StatusCode::BadRequest, e.to_string()))?;
        }
        _ => (),
    }
    debug!(
        "Saving function with Name: {}, Trigger: {:?}, Code: {}",
        name, item.trigger, item.code
    );
    if !name.trim().is_empty() {
        let (storage, connection) = req.state();
        let new_record = match storage.get(&name).await {
            Some(f) => {
                // if a function is already saved it needs to be updated
                let env_id = f.environment_id.clone();
                // ... and for that we have to disable the function
                match connection.send(RuntimeRequest::Disable(f)).await {
                    Ok(_) => Ok(()),
                    Err(e) => {
                        error!(
                            "Couldn't disable the runtime for '{}' at this time. Aborting",
                            env_id
                        );
                        Err(e)
                    }
                }?;
                // ... and create a new record with the same environment as the old one
                UserFunctionRecord::new(item.clone(), env_id)
            }
            None => UserFunctionRecord::from(item.clone()),
        };
        // replace the exisiting function
        storage.set(name.clone(), new_record).await;
        let code = storage
            .get(&name)
            .await
            .ok_or_else(|| AnyError::msg(format!("Function couldn't be found: {}", name)))?;
        connection.send(RuntimeRequest::NewFunction(code)).await?;
        Ok(Response::new(StatusCode::Ok))
    } else {
        error!("ERROR :(");
        Err(tide::Error::from_str(
            StatusCode::BadRequest,
            format!("Name '{}' is invalid", name),
        ))
    }
}
pub async fn save_function ( mut req : Request < AppState > ) -> tide :: Result { let item : UserFunctionDeclaration = req . body_json ( ) . await ? ; let name = & item . name ; // Check if the cron string is valid. match & item . trigger { Trigger :: Interval ( cron_str ) => { let e = cron_str . parse :: < cron :: Schedule > ( ) ; if e . is_err ( ) { error ! ( " " cron_str e ) ; } e . map_err ( | e | tide :: Error :: from_str ( StatusCode :: BadRequest , e . to_string ( ) ) ) ? ; } _ => ( ) , } debug ! ( " " name item trigger item code ) ; if ! name . trim ( ) . is_empty ( ) { let ( storage , connection ) = req . state ( ) ; let new_record = match storage . get ( & name ) . await { Some ( f ) => { // if a function is already saved it needs to be updated let env_id = f . environment_id . clone ( ) ; // ... and for that we have to disable the function match connection . send ( RuntimeRequest :: Disable ( f ) ) . await { Ok ( _ ) => Ok ( ( ) ) , Err ( e ) => { error ! ( " " env_id ) ; Err ( e ) } } ? ; // ... and create a new record with the same environment as the old one UserFunctionRecord :: new ( item . clone ( ) , env_id ) } None => UserFunctionRecord :: from ( item . clone ( ) ) , } ; // replace the exisiting function storage . set ( name . clone ( ) , new_record ) . await ; let code = storage . get ( & name ) . await . ok_or_else ( | | AnyError :: msg ( format ! ( " " name ) ) ) ? ; connection . send ( RuntimeRequest :: NewFunction ( code ) ) . await ? ; Ok ( Response :: new ( StatusCode :: Ok ) ) } else { error ! ( " " ) ; Err ( tide :: Error :: from_str ( StatusCode :: BadRequest , format ! ( " " name ) , ) ) } }
API call to save a function using a JSON object.
API call to save a function using a JSON object .
req
Request<AppState>
req
Request<AppState>