Section V - adding the business logic

reporting.rs

Now that we know the service is reading the path parameter product we can add our business logic to retrieve the JSON payload.

Add additional use declarations:

use std::{fs, io};
use std::fs::File;
use std::io::prelude::*;
use serde_json::json;
use serde_json::value::Value;

Add a new constant as a global variable

static WORKSPACE_LOCAL_STORAGE: &str = "./workshop_storage";

Include a supportive function after the outside of the main function.

fn extract_product_name(file_path: String) -> String {
    file_path
        .replace(&format!("{}/clothing-", WORKSPACE_LOCAL_STORAGE),"")
        .replace(".json","")
        .replace("_"," ")
}
fn product_file(product_name: String) -> String {
    format!("{}/clothing-{}.json", WORKSPACE_LOCAL_STORAGE, product_name.replace(" ","_")) 
}

Modify the index function with the new business logic.

The final reporting.rs file should look like the following:

Last updated

Was this helpful?