From 66fc7bfaf2fa43d29634c72ca4ec0a66b465703f Mon Sep 17 00:00:00 2001 From: Evgenii Sopov Date: Fri, 26 Jun 2020 23:12:49 +0700 Subject: [PATCH 1/5] Implemented add/delete tag for useful_links in admin --- fhq-server/CMakeLists.txt | 2 + .../src/cmd/cmd_handlers_useful_links.cpp | 116 +++++++++++++- .../updates/update_722kt134lq_6c8d4zm6u2.cpp | 18 +++ .../updates/update_722kt134lq_6c8d4zm6u2.h | 11 ++ web-admin/index.css | 25 +++ web-admin/js/fhq.admin.useful_links.js | 151 ++++++++++++++---- web-admin/js/fhq.ws.admin.js | 12 ++ 7 files changed, 298 insertions(+), 37 deletions(-) create mode 100644 fhq-server/src/storages/updates/update_722kt134lq_6c8d4zm6u2.cpp create mode 100644 fhq-server/src/storages/updates/update_722kt134lq_6c8d4zm6u2.h diff --git a/fhq-server/CMakeLists.txt b/fhq-server/CMakeLists.txt index 7ff4c38b..a10d6fa3 100644 --- a/fhq-server/CMakeLists.txt +++ b/fhq-server/CMakeLists.txt @@ -201,6 +201,8 @@ list (APPEND WSJCPP_SOURCES "src/storages/updates/update_snwxenqco0_csww6lwcrp.h list (APPEND WSJCPP_SOURCES "src/storages/updates/update_snwxenqco0_csww6lwcrp.cpp") list (APPEND WSJCPP_SOURCES "src/storages/updates/update_csww6lwcrp_722kt134lq.h") list (APPEND WSJCPP_SOURCES "src/storages/updates/update_csww6lwcrp_722kt134lq.cpp") +list (APPEND WSJCPP_SOURCES "src/storages/updates/update_722kt134lq_6c8d4zm6u2.h") +list (APPEND WSJCPP_SOURCES "src/storages/updates/update_722kt134lq_6c8d4zm6u2.cpp") list (APPEND WSJCPP_SOURCES "src/storages/updates/update0101.h") list (APPEND WSJCPP_SOURCES "src/storages/updates/update0101.cpp") diff --git a/fhq-server/src/cmd/cmd_handlers_useful_links.cpp b/fhq-server/src/cmd/cmd_handlers_useful_links.cpp index 8ffc3398..33ce96d5 100644 --- a/fhq-server/src/cmd/cmd_handlers_useful_links.cpp +++ b/fhq-server/src/cmd/cmd_handlers_useful_links.cpp @@ -173,7 +173,7 @@ CmdHandlerUsefulLinksRetrieve::CmdHandlerUsefulLinksRetrieve() setActivatedFromVersion("0.2.28"); - setAccessUnauthorized(false); + setAccessUnauthorized(true); setAccessUser(true); setAccessAdmin(true); @@ -224,6 +224,23 @@ void CmdHandlerUsefulLinksRetrieve::handle(ModelRequest *pRequest) { } } + // tags + query.prepare("SELECT * FROM useful_links_tags WHERE usefullinkid = :useful_link_id"); + query.bindValue(":useful_link_id", nUsefulLinkId); + + if (!query.exec()) { + pRequest->sendMessageError(cmd(), WsjcppError(500, query.lastError().text().toStdString())); + return; + } + + nlohmann::json jsonLinkTags = nlohmann::json::array(); + while (query.next()) { + QSqlRecord record = query.record(); + std::string sTag = record.value("tagvalue").toString().toHtmlEscaped().toStdString(); + jsonLinkTags.push_back(sTag); + } + jsonLink["tags"] = jsonLinkTags; + nlohmann::json jsonResponse; jsonResponse["data"] = jsonLink; pRequest->sendMessageSuccess(cmd(), jsonResponse); @@ -264,10 +281,11 @@ void CmdHandlerUsefulLinksAdd::handle(ModelRequest *pRequest) { QSqlDatabase db = *(pDatabase->database()); QSqlQuery query(db); // TODO uuid - query.prepare("INSERT INTO useful_links(url,description,author,stars,dt) VALUES(:url, :description, :author, 0, NOW())"); + query.prepare("INSERT INTO useful_links(url,description,author,tags,dt) VALUES(:url, :description, :author, :tags, NOW())"); query.bindValue(":url", QString::fromStdString(sUrl)); query.bindValue(":description", QString::fromStdString(sDescription)); query.bindValue(":author", QString::fromStdString(sAuthor)); + query.bindValue(":tags", QString::fromStdString("")); if (!query.exec()) { pRequest->sendMessageError(cmd(), WsjcppError(500, query.lastError().text().toStdString())); @@ -669,6 +687,8 @@ void CmdHandlerUsefulLinksCommentList::handle(ModelRequest *pRequest) { // --------------------------------------------------------------------- // Useful Links add comment +REGISTRY_CMD(CmdHandlerUsefulLinksCommentAdd) + CmdHandlerUsefulLinksCommentAdd::CmdHandlerUsefulLinksCommentAdd() : CmdHandlerBase("useful_links_comment_add", "Useful Links add comment") { @@ -692,6 +712,8 @@ void CmdHandlerUsefulLinksCommentAdd::handle(ModelRequest *pRequest) { // --------------------------------------------------------------------- // Useful Links remove comment +REGISTRY_CMD(CmdHandlerUsefulLinksCommentDelete) + CmdHandlerUsefulLinksCommentDelete::CmdHandlerUsefulLinksCommentDelete() : CmdHandlerBase("useful_links_comment_delete", "Useful Links remove comment") { @@ -715,6 +737,8 @@ void CmdHandlerUsefulLinksCommentDelete::handle(ModelRequest *pRequest) { // --------------------------------------------------------------------- // Useful Links List of tags +REGISTRY_CMD(CmdHandlerUsefulLinksTagList) + CmdHandlerUsefulLinksTagList::CmdHandlerUsefulLinksTagList() : CmdHandlerBase("useful_links_tag_list", "Useful Links - List of tags") { @@ -735,6 +759,8 @@ void CmdHandlerUsefulLinksTagList::handle(ModelRequest *pRequest) { // --------------------------------------------------------------------- // Useful Links add tag +REGISTRY_CMD(CmdHandlerUsefulLinksTagAdd) + CmdHandlerUsefulLinksTagAdd::CmdHandlerUsefulLinksTagAdd() : CmdHandlerBase("useful_links_tag_add", "Useful Links add tag") { @@ -745,18 +771,56 @@ CmdHandlerUsefulLinksTagAdd::CmdHandlerUsefulLinksTagAdd() setAccessAdmin(true); requireIntegerParam("useful_link_id", "Id of useful link"); - requireStringParam("tagname", "tag name"); + requireStringParam("tag", "Tag Value"); } // --------------------------------------------------------------------- void CmdHandlerUsefulLinksTagAdd::handle(ModelRequest *pRequest) { - pRequest->sendMessageError(cmd(), WsjcppError(501, "Not Implemented Yet")); + int nUsefulLinkId = pRequest->getInputInteger("useful_link_id", 0); + std::string sTagValue = pRequest->getInputString("tag", ""); + sTagValue = WsjcppCore::trim(sTagValue); + if (sTagValue == "") { + pRequest->sendMessageError(cmd(), WsjcppError(400, "tag could not be empty")); + return; + } + + EmployDatabase *pDatabase = findWsjcppEmploy(); + + QSqlDatabase db = *(pDatabase->database()); + QSqlQuery query(db); + + // TODO get from employ + query.prepare("SELECT * FROM useful_links_tags WHERE usefullinkid = :useful_link_id AND tagvalue = :tagvalue"); + query.bindValue(":useful_link_id", nUsefulLinkId); + query.bindValue(":tagvalue", QString::fromStdString(sTagValue)); + + if (!query.exec()) { + pRequest->sendMessageError(cmd(), WsjcppError(500, query.lastError().text().toStdString())); + return; + } + + if (query.next()) { + pRequest->sendMessageError(cmd(), WsjcppError(400, "TAG_ALREADY_EXISTS")); + return; + } else { + query.prepare("INSERT INTO useful_links_tags(usefullinkid, tagvalue) VALUES(:useful_link_id, :tagvalue)"); + query.bindValue(":useful_link_id", nUsefulLinkId); + query.bindValue(":tagvalue", QString::fromStdString(sTagValue)); + if (!query.exec()) { + pRequest->sendMessageError(cmd(), WsjcppError(500, query.lastError().text().toStdString())); + return; + } + } + nlohmann::json jsonResponse; + pRequest->sendMessageSuccess(cmd(), jsonResponse); } // --------------------------------------------------------------------- // Useful Links remove tag +REGISTRY_CMD(CmdHandlerUsefulLinksTagDelete) + CmdHandlerUsefulLinksTagDelete::CmdHandlerUsefulLinksTagDelete() : CmdHandlerBase("useful_links_tag_delete", "Useful Links remove tag") { @@ -765,19 +829,55 @@ CmdHandlerUsefulLinksTagDelete::CmdHandlerUsefulLinksTagDelete() setAccessUnauthorized(false); setAccessUser(false); setAccessAdmin(true); - - requireIntegerParam("useful_link_tag_id", "Tag Id for useful link"); + + requireIntegerParam("useful_link_id", "Useful Link Id"); + requireStringParam("tag", "Tag Value"); } // --------------------------------------------------------------------- void CmdHandlerUsefulLinksTagDelete::handle(ModelRequest *pRequest) { - pRequest->sendMessageError(cmd(), WsjcppError(501, "Not Implemented Yet")); + int nUsefulLinkId = pRequest->getInputInteger("useful_link_id", 0); + std::string sTagValue = pRequest->getInputString("tag", ""); + sTagValue = WsjcppCore::trim(sTagValue); + if (sTagValue == "") { + pRequest->sendMessageError(cmd(), WsjcppError(400, "tag could not be empty")); + return; + } + + EmployDatabase *pDatabase = findWsjcppEmploy(); + + QSqlDatabase db = *(pDatabase->database()); + QSqlQuery query(db); + + // TODO get from employ + query.prepare("SELECT * FROM useful_links_tags WHERE usefullinkid = :useful_link_id AND tagvalue = :tagvalue"); + query.bindValue(":useful_link_id", nUsefulLinkId); + query.bindValue(":tagvalue", QString::fromStdString(sTagValue)); + + if (!query.exec()) { + pRequest->sendMessageError(cmd(), WsjcppError(500, query.lastError().text().toStdString())); + return; + } + + if (query.next()) { + query.prepare("DELETE FROM useful_links_tags WHERE usefullinkid = :useful_link_id AND tagvalue = :tagvalue"); + query.bindValue(":useful_link_id", nUsefulLinkId); + query.bindValue(":tagvalue", QString::fromStdString(sTagValue)); + if (!query.exec()) { + pRequest->sendMessageError(cmd(), WsjcppError(500, query.lastError().text().toStdString())); + return; + } + } + nlohmann::json jsonResponse; + pRequest->sendMessageSuccess(cmd(), jsonResponse); } // --------------------------------------------------------------------- // Useful Links propose link by user +REGISTRY_CMD(CmdHandlerUsefulLinksUserPropose) + CmdHandlerUsefulLinksUserPropose::CmdHandlerUsefulLinksUserPropose() : CmdHandlerBase("useful_links_user_propose", "Useful Links propose link by user") { @@ -801,6 +901,8 @@ void CmdHandlerUsefulLinksUserPropose::handle(ModelRequest *pRequest) { // --------------------------------------------------------------------- // Useful Links propose link by user +REGISTRY_CMD(CmdHandlerUsefulLinksUserProposeApprove) + CmdHandlerUsefulLinksUserProposeApprove::CmdHandlerUsefulLinksUserProposeApprove() : CmdHandlerBase("useful_links_user_propose_approve", "Useful Links Approve propose link by admin") { diff --git a/fhq-server/src/storages/updates/update_722kt134lq_6c8d4zm6u2.cpp b/fhq-server/src/storages/updates/update_722kt134lq_6c8d4zm6u2.cpp new file mode 100644 index 00000000..4f5d4830 --- /dev/null +++ b/fhq-server/src/storages/updates/update_722kt134lq_6c8d4zm6u2.cpp @@ -0,0 +1,18 @@ +#include "update_722kt134lq_6c8d4zm6u2.h" + +REGISTRY_WSJCPP_STORAGE_UPDATE(Update_722kt134lq_6c8d4zm6u2) + +Update_722kt134lq_6c8d4zm6u2::Update_722kt134lq_6c8d4zm6u2() + : WsjcppStorageUpdateBase("722kt134lq", "6c8d4zm6u2", "TODO") { + + WsjcppStorageModifyTable *useful_links_tags = modifyTable("useful_links_tags"); + useful_links_tags->dropColumn("tags"); + useful_links_tags->dropColumn("counter"); + + WsjcppStorageModifyTable *useful_links = modifyTable("useful_links"); + useful_links->addColumn("tags").string(1024).notNull().defaultValue(""); + useful_links->dropColumn("stars"); + + // fill the array with struct changes +} + diff --git a/fhq-server/src/storages/updates/update_722kt134lq_6c8d4zm6u2.h b/fhq-server/src/storages/updates/update_722kt134lq_6c8d4zm6u2.h new file mode 100644 index 00000000..b0375a1d --- /dev/null +++ b/fhq-server/src/storages/updates/update_722kt134lq_6c8d4zm6u2.h @@ -0,0 +1,11 @@ +#ifndef UPDATE_722KT134LQ_6C8D4ZM6U2_H +#define UPDATE_722KT134LQ_6C8D4ZM6U2_H + +#include + +class Update_722kt134lq_6c8d4zm6u2 : public WsjcppStorageUpdateBase { + public: + Update_722kt134lq_6c8d4zm6u2(); +}; + +#endif // UPDATE_722KT134LQ_6C8D4ZM6U2_H diff --git a/web-admin/index.css b/web-admin/index.css index 75c18139..0341aee5 100644 --- a/web-admin/index.css +++ b/web-admin/index.css @@ -27,3 +27,28 @@ pre { cursor: pointer; } +.tag-item { + display: inline-block; + padding: 10px; + background-color: #000; + color: #fff; + border-radius: 20px; + padding-left: 20px; + margin-right: 10px; +} + +.tag-item-remove { + display: inline-block; + width: 20px; + height: 20px; + background-color: red; + color: #000; + border-radius: 20px; + text-align: center; + cursor: pointer; + margin-left: 10px; +} + +.fhq-card { + +} \ No newline at end of file diff --git a/web-admin/js/fhq.admin.useful_links.js b/web-admin/js/fhq.admin.useful_links.js index b12d8d98..40d6fdb2 100644 --- a/web-admin/js/fhq.admin.useful_links.js +++ b/web-admin/js/fhq.admin.useful_links.js @@ -120,7 +120,7 @@ fhq.usefulLinkDelete = function(el) { }) }; -fhq.usefulLinksAdd = function() { +fhq.usefulLinksCreateAndNew = function() { fhq.showLoader(); $('#error_info').hide(); var data = {}; @@ -130,7 +130,27 @@ fhq.usefulLinksAdd = function() { fhq.ws.useful_links_add(data).done(function(r){ fhq.hideLoader(); - fhq.pages['useful_links'](); + fhq.pages['useful_links_add'](); + }).fail(function(err){ + fhq.hideLoader(); + console.error(err); + $('#error_info').show(); + $('#error_info .alert').html('ERROR: ' + err.error); + }) +}; + +fhq.usefulLinksCreateAndEdit = function() { + fhq.showLoader(); + $('#error_info').hide(); + var data = {}; + data["url"] = $("#useful_link_url").val(); + data["description"] = $("#useful_link_description").val(); + data["author"] = ''; + + fhq.ws.useful_links_add(data).done(function(r){ + console.log(r); + fhq.hideLoader(); + fhq.pages['useful_links_edit'](parseInt(r.data.id)); }).fail(function(err){ fhq.hideLoader(); console.error(err); @@ -163,7 +183,8 @@ fhq.pages['useful_links_add'] = function(){ + '
' + ' ' + '
' - + '
Create
' + + '
Create && New
' + + '
Create && Edit
' + '
' + '
' + '