Skip to content

Commit

Permalink
add endpoint for POSTing JSON FileArchive
Browse files Browse the repository at this point in the history
  • Loading branch information
nam20485 committed May 8, 2024
1 parent 7c1c305 commit 35b0cb5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
43 changes: 40 additions & 3 deletions OdbDesignServer/Controllers/FileModelController.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "FileModelController.h"
#include <JsonCrowReturnable.h>
#include "UrlEncoding.h"
#include <memory>
#include <FileModel/Design/FileArchive.h>


using namespace Odb::Lib::App;
Expand Down Expand Up @@ -53,7 +55,7 @@ namespace Odb::App::Server
return this->filemodels_list_route_handler(req);
});

CROW_ROUTE(m_serverApp.crow_app(), "/filemodels/<string>")
CROW_ROUTE(m_serverApp.crow_app(), "/filemodels/<string>").methods(crow::HTTPMethod::GET)
([&](const crow::request& req, std::string designName)
{
// authenticate request before sending to handler
Expand All @@ -63,9 +65,22 @@ namespace Odb::App::Server
return authResp;
}

return this->filemodels_route_handler(designName, req);
return this->filemodels_get_route_handler(designName, req);
});

CROW_ROUTE(m_serverApp.crow_app(), "/filemodels/<string>").methods(crow::HTTPMethod::POST)
([&](const crow::request& req, std::string designName)
{
// authenticate request before sending to handler
auto authResp = m_serverApp.request_auth().AuthenticateRequest(req);
if (authResp.code != crow::status::OK)
{
return authResp;
}

return this->filemodels_post_route_handler(designName, req);
});

CROW_ROUTE(m_serverApp.crow_app(), "/filemodels/<string>/steps/<string>")
([&](const crow::request& req, std::string designName, std::string stepName)
{
Expand Down Expand Up @@ -344,7 +359,7 @@ namespace Odb::App::Server
});
}

crow::response FileModelController::filemodels_route_handler(const std::string& designName, const crow::request& req)
crow::response FileModelController::filemodels_get_route_handler(const std::string& designName, const crow::request& req)
{
auto designNameDecoded = UrlEncoding::decode(designName);
if (designNameDecoded.empty())
Expand All @@ -363,6 +378,28 @@ namespace Odb::App::Server
return crow::response(JsonCrowReturnable(*pFileArchive));
}

crow::response FileModelController::filemodels_post_route_handler(const std::string& designName, const crow::request& req)
{
auto designNameDecoded = UrlEncoding::decode(designName);
if (designNameDecoded.empty())
{
return crow::response(crow::status::BAD_REQUEST, "design name not specified");
}

const auto& json = req.body;
if (json.empty())
{
return crow::response(crow::status::BAD_REQUEST, "no data provided in POST body");
}

auto fileArchive = std::make_shared<FileArchive>();
fileArchive->from_json(json);

m_serverApp.designs().AddFileArchive(designName, fileArchive, false);

return crow::response();
}

crow::response FileModelController::steps_edadata_route_handler(const std::string& designName,
const std::string& stepName,
const crow::request& req)
Expand Down
3 changes: 2 additions & 1 deletion OdbDesignServer/Controllers/FileModelController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace Odb::App::Server
void register_routes() override;

private:
crow::response filemodels_route_handler(const std::string& designName, const crow::request& req);
crow::response filemodels_get_route_handler(const std::string& designName, const crow::request& req);
crow::response filemodels_post_route_handler(const std::string& designName, const crow::request& req);
crow::response filemodels_list_route_handler(const crow::request& req);

crow::response steps_route_handler(const std::string& designName, const std::string& stepName, const crow::request& req);
Expand Down

0 comments on commit 35b0cb5

Please sign in to comment.