| 849 | } |
| 850 | |
| 851 | MeasureManagerServer::ResponseType MeasureManagerServer::create_measure(const web::json::value& body) { |
| 852 | static const std::array<std::string, 7> requiredParams = { |
| 853 | "measure_dir", "display_name", "class_name", "taxonomy_tag", "measure_type", "description", "modeler_description", |
| 854 | }; |
| 855 | for (const auto& requiredParam : requiredParams) { |
| 856 | auto key = utility::conversions::to_string_t(requiredParam); |
| 857 | if (!body.has_string_field(key)) { |
| 858 | auto msg = fmt::format("The '{}' (string) must be in the post data.", requiredParam); |
| 859 | fmt::print("{}\n", msg); |
| 860 | return {web::http::status_codes::BadRequest, toWebJSON(msg)}; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | MeasureType measureType = MeasureType::ModelMeasure; |
| 865 | auto measureTypeString = get_field<std::string>(body, "measure_type", "ModelMeasure"); |
| 866 | try { |
| 867 | measureType = MeasureType(measureTypeString); |
| 868 | } catch (const std::exception& e) { |
| 869 | auto msg = fmt::format("Couldn't convert '{}' to a MeasureType: {}", measureTypeString, e.what()); |
| 870 | fmt::print("{}\n", msg); |
| 871 | return {web::http::status_codes::BadRequest, toWebJSON(msg)}; |
| 872 | } |
| 873 | |
| 874 | MeasureLanguage measureLanguage = MeasureLanguage::Ruby; |
| 875 | if (auto measureLanguageString_ = get_field<std::string>(body, "measure_language")) { |
| 876 | try { |
| 877 | measureLanguage = MeasureLanguage(*measureLanguageString_); |
| 878 | } catch (const std::exception& e) { |
| 879 | auto msg = fmt::format("Couldn't convert '{}' to a MeasureLanguage: {}", *measureLanguageString_, e.what()); |
| 880 | fmt::print("{}\n", msg); |
| 881 | return {web::http::status_codes::BadRequest, toWebJSON(msg)}; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | const openstudio::path measureDir = get_field<openstudio::path>(body, "measure_dir", ""); |
| 886 | // This is throwy when the directory already exists |
| 887 | if (openstudio::filesystem::is_directory(measureDir)) { |
| 888 | auto msg = fmt::format("The directory already exists at '{}'.", measureDir.generic_string()); |
| 889 | fmt::print("{}\n", msg); |
| 890 | return {web::http::status_codes::BadRequest, toWebJSON(msg)}; |
| 891 | } |
| 892 | const BCLMeasure measure(get_field<std::string>(body, "display_name", "DisplayName"), get_field<std::string>(body, "class_name", "ClassName"), |
| 893 | measureDir, get_field<std::string>(body, "taxonomy_tag", "taxonomy.tag"), measureType, |
| 894 | get_field<std::string>(body, "description", "Description"), |
| 895 | get_field<std::string>(body, "modeler_description", "ModelerDescription"), measureLanguage); |
| 896 | |
| 897 | if (boost::optional<BCLMeasure> measure_ = m_measureManager.getMeasure(measureDir, true)) { |
| 898 | return {web::http::status_codes::OK, toWebJSON(measure_->toJSON())}; |
| 899 | } else { |
| 900 | fmt::print("Failed to update measure after creation, this shouldn't happen."); |
| 901 | return {web::http::status_codes::BadRequest, toWebJSON(measure.toJSON())}; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | MeasureManagerServer::ResponseType MeasureManagerServer::duplicate_measure(const web::json::value& body) { |
| 906 | // Required parameters: |
nothing calls this directly
no test coverage detected