From 89421d7ac500ac28cd05defde34d0dcdff1438b1 Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Wed, 9 Aug 2023 16:25:52 +0200 Subject: [PATCH] remove old upload implementation add viewer of file submissions --- src/database.cpp | 12 +++ src/databaseStatementConstCollection.cpp | 2 +- src/main.cpp | 77 +------------------ src/utilities.cpp | 12 +++ ...freelancer_Submission_Management_View.html | 36 +++++++-- 5 files changed, 55 insertions(+), 84 deletions(-) diff --git a/src/database.cpp b/src/database.cpp index 59c538a..de11988 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -683,6 +683,18 @@ namespace Database { return result; } + /* + * Executes the prepared statement SELECT_FREELANCER_FILE_SUBMISSION_BASE_DATA + * Takes an open pqxx::connection and the freelancer email + * returns a list of file submissions with the columns filename, filesizemb, uploaddate + */ + pqxx::result executePreparedStatement_SELECT_FREELANCER_FILE_SUBMISSION_BASE_DATA(pqxx::connection &connection, const std::string& freelancerEmail) { + pqxx::work work(connection); + pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_FILE_SUBMISSION_BASE_DATA, freelancerEmail); + work.commit(); + return result; + } + /* * Executes the prepared statement SELECT_FREELANCER_FILE_SUBMISSION_PATH * Takes an open pqxx::connection the file name and the freelancer email diff --git a/src/databaseStatementConstCollection.cpp b/src/databaseStatementConstCollection.cpp index bce26ab..8c64e33 100644 --- a/src/databaseStatementConstCollection.cpp +++ b/src/databaseStatementConstCollection.cpp @@ -240,7 +240,7 @@ namespace DatabaseStatementConstCollection { * Name and Statement for prepared statement to select file submission data for submission selection interface */ const static std::string PREPARED_STATEMENT_SELECT_FREELANCER_FILE_SUBMISSION_BASE_DATA = "selectFreelancerFileSubmissionBaseData"; - const static std::string SQL_STATEMENT_SELECT_FREELANCER_FILE_SUBMISSION_BASE_DATA = "select filename, round(filesize / 1024.0 / 1024.0, 4) as filesizemb, uploaddate from freelancersubmissions where freelancerid = (select freelancers.id from freelancers where emailaddress = $1)"; + const static std::string SQL_STATEMENT_SELECT_FREELANCER_FILE_SUBMISSION_BASE_DATA = "select filename, round(coalesce(filesize, 0) / 1024.0 / 1024.0, 4) as filesizemb, uploaddate from freelancersubmissions where freelancerid = (select freelancers.id from freelancers where emailaddress = $1)"; /* * Name and Statement for prepared statement to select MB of used freelancer storage diff --git a/src/main.cpp b/src/main.cpp index 0edc722..e3460b6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1160,82 +1160,6 @@ int main(int argc, char *argv[]) { } }); -/* - CROW_ROUTE(app, "/freelancer/submissionManagement/add/fulfillment").methods(crow::HTTPMethod::Post) - ([&, configuration](const crow::request& postRequest) { - auto& cookieCtx = app.get_context(postRequest); - if (Utilities::checkCookieLoginState(configuration, cookieCtx)) { - pqxx::connection databaseConnection(configuration.databaseConnectionString); - Database::prepareStatements(databaseConnection, { - ID_SELECT_FREELANCER_ID, - ID_SELECT_FREELANCER_NAME - }); - - - string freelancerName, freelancerID; - pqxx::result freelancerNameResult = Database::executePreparedStatement_SELECT_FREELANCER_NAME(databaseConnection, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL)); - freelancerName = freelancerNameResult.at(0).at(0).c_str(); - pqxx::result freelancerIDResult = Database::executePreparedStatement_SELECT_FREELANCER_ID(databaseConnection, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL)); - freelancerID = freelancerIDResult.at(0).at(0).c_str(); - - - - crow::multipart::message multipartMessage(postRequest); - return crow::response(400); - for (const auto& part : multipartMessage.part_map) - { - const auto& mustacheSubmissionNameValue = part.first; - const auto& fileSubmission = part.second; - if (MUSTACHE_FREELANCER_SUBMISSION_NAME == mustacheSubmissionNameValue) - { - auto contentDispositionIt = fileSubmission.headers.find("Content-Disposition"); - - if (contentDispositionIt == fileSubmission.headers.end()) - return crow::response(400, "Content-Disposition could not be found"); - - auto filenameIt = contentDispositionIt->second.params.find("filename"); - if (filenameIt == contentDispositionIt->second.params.end()) - return crow::response(400, "File Submission does not have a filename"); - - if (!Utilities::checkFiletypeValidity(configuration, filenameIt->second)) - return crow::response(400, "Submitted File does not have a valid filetype"); - - if (!Utilities::validateFileSize(configuration, fileSubmission.body)) - return crow::response(400, "File Size is not valid"); - - const std::string outputFolderPath = Utilities::generateSubmissionFolderPath(configuration, freelancerName, freelancerID); - - if(!Utilities::validateFolderPath(outputFolderPath)) - return crow::response(400, "Unable to write to Freelancer folder"); - - const std::string outputFilename = filenameIt->second; - const std::string outputFilePath = Utilities::generateSubmissionFilePath(outputFolderPath, outputFilename); - cout << outputFolderPath << endl; - cout << outputFilePath << endl; - if(!Utilities::validateFilePath(outputFilePath)) - return crow::response(400, "file already exists - hash collision"); - - std::ofstream outputFileStream(outputFilePath); - if (!outputFileStream) - return crow::response(500, "File could not be written to disk"); - - outputFileStream << fileSubmission.body; - outputFileStream.close(); - } - else - { - return crow::response(400, "Mustache Submission name could not be found in the request"); - } - } - return crow::response(200); - } - else - { - //ERROR not logged in - return crow::response(403, "Not logged in"); - } - });*/ - /* * Page for freelancer to view existing submissions */ @@ -1244,6 +1168,7 @@ int main(int argc, char *argv[]) { auto& cookieCtx = app.get_context(getRequest); crow::mustache::context ctx; if (Utilities::checkCookieLoginState(configuration, cookieCtx)) { + ctx = Utilities::getFreelancerSubmissions(configuration, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL)); ctx[MUSTACHE_COOKIE_LOGGED_IN] = true; diff --git a/src/utilities.cpp b/src/utilities.cpp index de1ded6..f582115 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -687,6 +687,18 @@ namespace Utilities { return resultJsonFreelancerTemplate; } + /* + * Gets the freelancer submissions and converts them into a wvalue JSON under the name "submissions" + * takes config and freelancer email + */ + crow::json::wvalue getFreelancerSubmissions(const Utilities::config& configuration, const std::string& emailAddress) { + pqxx::connection databaseConnection(configuration.databaseConnectionString); + Database::prepareStatement(databaseConnection, ID_SELECT_FREELANCER_FILE_SUBMISSION_BASE_DATA); + pqxx::result resultJsonFreelancerSubmissions = Database::executePreparedStatement_SELECT_FREELANCER_FILE_SUBMISSION_BASE_DATA(databaseConnection, emailAddress); + crow::json::wvalue jsonFreelancerSubmissions = Database::convertResultToJSON(resultJsonFreelancerSubmissions, "submissions"); + return jsonFreelancerSubmissions; + } + /* * Gets the freelancer alias and converts them into a wvalue JSON under the name "alias" * takes config and freelancer email diff --git a/templates/freelancer_Submission_Management_View.html b/templates/freelancer_Submission_Management_View.html index 9800834..4142834 100644 --- a/templates/freelancer_Submission_Management_View.html +++ b/templates/freelancer_Submission_Management_View.html @@ -7,13 +7,35 @@ {{^COOKIE_LOGGED_IN}} Please Log in. {{/COOKIE_LOGGED_IN}} - {{#COOKIE_LOGGED_IN}} -
- -
-
- -
+ {{#COOKIE_LOGGED_IN}} + + + + + + + {{#submissions}} + + + + + + + + {{/submissions}} +
NameSize (MB)Upload date
+
+ +
+
{{filesizemb}} MB{{uploaddate}} +
+ +
+
+
+ +
+
{{/COOKIE_LOGGED_IN}} {{> templateIncludes/freelancerLoginSignupProfileLogoutInterface.html.html}}