From 008b6edce15bea05ac1be1c29df2e00c6ba20c2f Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Wed, 2 Aug 2023 18:16:22 +0200 Subject: [PATCH] Display used storage space in UI --- src/database.cpp | 12 ++++++++++++ src/databaseStatementConstCollection.cpp | 2 +- src/main.cpp | 11 +++++++++-- src/templateConstCollection.cpp | 2 +- templates/freelancer_Submission_Management_Add.html | 2 +- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/database.cpp b/src/database.cpp index 1083393..ebba9f9 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -671,6 +671,18 @@ namespace Database { work.commit(); } + /* + * Executes the prepared statement SELECT_FREELANCER_FILE_SUBMISSION_USED_STORAGE + * Takes an open pqxx::connection and the freelancer email + * returns the sum of MB of all submissions in the database if none exist 0 is returned + */ + pqxx::result executePreparedStatement_SELECT_FREELANCER_FILE_SUBMISSION_USED_STORAGE(pqxx::connection &connection, const std::string& freelancerEmail) { + pqxx::work work(connection); + pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_FILE_SUBMISSION_USED_STORAGE, freelancerEmail); + work.commit(); + return result; + } + /* * Prepares a statement based on ID * Takes an open pqxx::connection, the statement id diff --git a/src/databaseStatementConstCollection.cpp b/src/databaseStatementConstCollection.cpp index 47ba2e2..147f3ac 100644 --- a/src/databaseStatementConstCollection.cpp +++ b/src/databaseStatementConstCollection.cpp @@ -246,7 +246,7 @@ namespace DatabaseStatementConstCollection { * Name and Statement for prepared statement to select MB of used freelancer storage */ const static std::string PREPARED_STATEMENT_SELECT_FREELANCER_FILE_SUBMISSION_USED_STORAGE = "selectFreelancerFileSubmissionUsedStorage"; - const static std::string SQL_STATEMENT_SELECT_FREELANCER_FILE_SUBMISSION_USED_STORAGE = "select round(sum(filesize) / 1024.0 / 1024.0, 4) from freelancersubmissions where freelancerid = (select freelancers.id from freelancers where emailaddress = $1)"; + const static std::string SQL_STATEMENT_SELECT_FREELANCER_FILE_SUBMISSION_USED_STORAGE = "select round(coalesce(sum(filesize), 0) / 1024.0 / 1024.0, 4) from freelancersubmissions where freelancerid = (select freelancers.id from freelancers where emailaddress = $1)"; /* * Name and Statement for prepared statement to select fullpath of a submission based on freelancer email and filename diff --git a/src/main.cpp b/src/main.cpp index a7f70b2..7b27df3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -983,9 +983,16 @@ int main(int argc, char *argv[]) { if (Utilities::checkCookieLoginState(configuration, cookieCtx)) { ctx[MUSTACHE_COOKIE_LOGGED_IN] = true; - size_t usedStorageInMB = 0; //todo:database operation + pqxx::connection databaseConnection(configuration.databaseConnectionString); + Database::prepareStatements(databaseConnection, { + ID_SELECT_FREELANCER_FILE_SUBMISSION_USED_STORAGE + }); + + pqxx::result freelancerUsedStorageResult = Database::executePreparedStatement_SELECT_FREELANCER_FILE_SUBMISSION_USED_STORAGE(databaseConnection, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL)); + + double usedStorageInMB = stod(freelancerUsedStorageResult.at(0).at(0).c_str()); size_t maxStorageInMB = configuration.submissionMaxtotalStorageMB; - ctx[MUSTACHE_FREELANCER_AVAILIBLE_STORAGE_IN_MB] = usedStorageInMB; + ctx[MUSTACHE_FREELANCER_USED_STORAGE_IN_MB] = usedStorageInMB; ctx[MUSTACHE_FREELANCER_MAXIMUM_STORAGE_IN_MB] = maxStorageInMB; ctx[MUSTACHE_FREELANCER_MAXIMUM_FILE_NAME_SIZE] = configuration.submissionMaxFileNameSize; diff --git a/src/templateConstCollection.cpp b/src/templateConstCollection.cpp index 349668c..a7b2551 100644 --- a/src/templateConstCollection.cpp +++ b/src/templateConstCollection.cpp @@ -87,7 +87,7 @@ namespace TemplateConstCollection { const static std::string MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_EDIT = "OPERATION_EDIT"; const static std::string MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_DELETE = "OPERATION_DELETE"; const static std::string MUSTACHE_FREELANCER_SUBMISSION_NAME = "FILE_SUBMISSION"; - const static std::string MUSTACHE_FREELANCER_AVAILIBLE_STORAGE_IN_MB = "AVAILIBLE_STORAGE_IN_MB"; + const static std::string MUSTACHE_FREELANCER_USED_STORAGE_IN_MB = "USED_STORAGE_IN_MB"; const static std::string MUSTACHE_FREELANCER_MAXIMUM_STORAGE_IN_MB = "MAXIMUM_STORAGE_IN_MB"; const static std::string MUSTACHE_FREELANCER_MAXIMUM_FILE_NAME_SIZE = "MAXIMUM_FILE_NAME_SIZE"; const static std::string MUSTACHE_FREELANCER_ALLOWED_FILE_TYPES_LIST_COMMA_SEPARATED = "ALLOWED_FILE_TYPES_LIST_COMMA_SEPARATED"; diff --git a/templates/freelancer_Submission_Management_Add.html b/templates/freelancer_Submission_Management_Add.html index 972c077..f8818a1 100644 --- a/templates/freelancer_Submission_Management_Add.html +++ b/templates/freelancer_Submission_Management_Add.html @@ -11,7 +11,7 @@ {{#COOKIE_LOGGED_IN}}

- Availible Space: {{AVAILIBLE_STORAGE_IN_MB}} MB / {{MAXIMUM_STORAGE_IN_MB}} MB + Used Storage Space: {{USED_STORAGE_IN_MB}} MB / {{MAXIMUM_STORAGE_IN_MB}} MB

Allowed File Types: {{ALLOWED_FILE_TYPES_LIST_COMMA_SEPARATED}}