From de17e88d373500588737045675314f0bdca996d9 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Fri, 2 Jun 2023 00:36:21 +0300 Subject: [PATCH] v0.0.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + Реализовано: обновление/добавление/удаление номера + Добавлена кнопка обновления активной вкладки - Доработать проверки на обновление/добавление/удаление номера --- js/script.js | 116 ++++++++++++++++++++++++++++----- public/style.css | 18 +++-- source/daster.d | 31 ++++----- source/data.d | 73 +++++++++++++++++++-- source/requests/addnumber.d | 18 +++++ source/requests/delnumber.d | 24 +++++++ source/requests/editnumber.d | 6 +- source/requests/groupnumbers.d | 2 +- source/requests/listsgroups.d | 2 +- source/requests/updatenumber.d | 16 +++++ source/requests/writenumber.d | 16 +++++ source/version_.d | 2 +- views/index.dt | 15 +++-- 13 files changed, 283 insertions(+), 56 deletions(-) create mode 100644 source/requests/addnumber.d create mode 100644 source/requests/delnumber.d create mode 100644 source/requests/updatenumber.d create mode 100644 source/requests/writenumber.d diff --git a/js/script.js b/js/script.js index 845d167..ca55e13 100644 --- a/js/script.js +++ b/js/script.js @@ -6,23 +6,32 @@ $(document).ready(function () { $("button").button(); $("#tabs").tabs(); - // $(".addNumber").click(() => { - // numberAdd() - // }); + $("#update").button("option", "icon", "ui-icon-refresh"); + $("#add-number").button("option", "icon", "ui-icon-plusthick"); + $("#logout").button("option", "icon", "ui-icon-power"); + + // За каждым индексом закреплена функция для выполнения действия + // на активной вкладке при нажатии на кнопку обновления + $("#update").click(() => { + ({ + 0: () => { generateGroupNumbers($("#accordion-numbers .ui-accordion-content-active")) }, + 1: () => { noticer.success('Вкладка "SMS"') }, + 2: () => { noticer.success('Вкладка "USSD"') }, + 3: () => { noticer.success('Вкладка "Сервер"') } + })[$("#tabs").tabs( "option", "active" )]() + }); + + $("#add-number").click(() => { + addNumber($("#accordion-numbers .ui-accordion-content-active")) + }); $("body").fadeTo(500, 1); - $(".search").on("input", function () { + $("#search").on("input", function () { showNumbers( $("#accordion-numbers .ui-accordion-content-active"), numbers.filter(e => e.number.includes($(this).val())) ) - }).on("focus", function () { - if ($(this).val()) - showNumbers( - $("#accordion-numbers .ui-accordion-content-active"), - numbers.filter(e => e.number.includes($(this).val())) - ); }).keydown(function (e) { e.key == "Escape" && ($(this).val(""), showNumbers($("#accordion-numbers .ui-accordion-content-active"))) }); @@ -91,7 +100,7 @@ function generateGroupNumbers(panel) { }); } -function showNumbers(panel, data = numbers) { +function showNumbers(panel, data = numbers.filter(e => e.number.includes($("#search").val()))) { (new divNotFoundNumbers).remove(); let body = panel.find('.body').html(''); $(data).each((i, j) => { @@ -105,7 +114,7 @@ function showNumbers(panel, data = numbers) { body.append(row); row.click(function() { - numberEdit($(this).data('number')); + editNumber(panel, $(this).data('number')); }); }); @@ -134,7 +143,7 @@ function divNotFoundNumbers() { } } -function numberEdit(number) { +function editNumber(panel, number) { request('editnumber', 'text', {number: number}).then(data => { if (isJSON(data) && JSON.parse(data).error) noticer.error(JSON.parse(data).message); @@ -145,7 +154,7 @@ function numberEdit(number) { text: "Сохранить", icon: "ui-icon-check", click: function() { - actionNumber($(this), 'update'); + actionNumber(panel, $(this), 'updatenumber'); } }, { @@ -153,7 +162,7 @@ function numberEdit(number) { text: "Удалить", icon: "ui-icon-trash", click: function() { - // removeNumber($(this)); + delNumber(panel, $(this)); } } ], `Редактирование номера ${number}`); @@ -170,8 +179,6 @@ function showEditNumber(data, actionButton, title) { title: title, height: 'auto', width: 'auto', - maxHeight: 500, - minHeight: 50, resizable: false, modal: true, show: { effect: "fade", duration: 500 }, @@ -195,7 +202,10 @@ function showEditNumber(data, actionButton, title) { }); } -function actionNumber(currentWindow, query) { +function actionNumber(panel, currentWindow, query) { + // Только числа, начинающие с >0 или только 0 + let regexp = /^(?=\d)(\d|([^0]\d+))$/g; + let number = $('#number-number').val(); let group = $('#number-group').val(); let list = $('#number-list').val(); @@ -203,5 +213,75 @@ function actionNumber(currentWindow, query) { let white_cc = $('#number-white-cc').val(); let black_cc = $('#number-black-cc').val(); let comment = $('#number-comment').val(); + + let error = false; + + if (!number.length) { noticer.warning('Номер не может быть пуст'); error = true; } + if (all_cc.match(regexp) === null) { noticer.warning("Не верно указано общее количество звонков"); error = true; } + if (white_cc.match(regexp) === null) { noticer.warning("Не верно указано белое количество звонков"); error = true; } + if (black_cc.match(regexp) === null) { noticer.warning("Не верно указано черное количество звонков"); error = true; } + + if (error) return; + + request(query, 'json', { + number: number, + group: group, + list: list, + all_cc: parseInt(all_cc), + white_cc: parseInt(white_cc), + black_cc: parseInt(black_cc), + comment: comment + }).then(data => { + if (data.error) + noticer.error(data.message); + else + { + query == 'write' ? noticer.success(`Номер ${number} был добавлен`) : noticer.success(`Номер ${number} был обновлен`); + generateGroupNumbers(panel); + currentWindow.dialog("close"); + } + }).catch(error => { + noticer.error(error.message); + }); } +function addNumber(panel) { + request('addnumber', 'text', { group: panel.data("group-name") }).then(data => { + if (isJSON(data) && JSON.parse(data).error) + noticer.error(JSON.parse(data).message); + else { + showEditNumber(data, [{ + id: "btnSave", + text: "Добавить", + icon: "ui-icon-check", + click: function() { + actionNumber(panel, $(this), 'writenumber'); + } + }], "Добавление нового номера"); + } + }).catch(error => { + noticer.error(error.message); + }); +} + +function delNumber(panel, currentWindow) { + let number = $('#number-number').val(); + + let error = false; + if (!number.length) { noticer.warning('Номер не может быть пуст'); error = true; } + if (error) return; + + request('delnumber', 'json', { + number: number + }).then(data => { + if (data.error) + noticer.error(data.message); + else { + noticer.success(`Номер ${number} был удален`); + generateGroupNumbers(panel); + currentWindow.dialog("close"); + } + }).catch(error => { + noticer.error(error.message); + }); +} diff --git a/public/style.css b/public/style.css index 767e236..a1de265 100644 --- a/public/style.css +++ b/public/style.css @@ -25,9 +25,9 @@ div.div-header { /* HEADER */ -/* div.div-add { +div.main-button { margin-right: 20px -} */ +} div.div-search { display: flex; @@ -107,10 +107,16 @@ td { background: #fff; } +tr.row { + border-top: 1px solid #fff; +} + tr.row:hover, tr.row:nth-child(even):hover { - background-color: #c5c5c5; - color: #fff; + background-color: #f6f6f6; + color: #003eff; cursor: pointer; + border-bottom: 1px solid #c5c5c5; + border-top: 1px solid #c5c5c5; } /* EDIT NUMBER */ @@ -148,6 +154,10 @@ tr.row:hover, tr.row:nth-child(even):hover { justify-content: center; } +.comment-name { + padding: 15px 0 5px 0; +} + .comment-content { width: 100%; height: 100%; diff --git a/source/daster.d b/source/daster.d index 14a7121..f93f834 100644 --- a/source/daster.d +++ b/source/daster.d @@ -19,6 +19,10 @@ import structures; import requests.listsgroups; import requests.groupnumbers; import requests.editnumber; +import requests.updatenumber; +import requests.addnumber; +import requests.delnumber; +import requests.writenumber; static ServerInfo serverInfo; @@ -181,24 +185,21 @@ void postReq(HTTPServerRequest req, HTTPServerResponse res) { // case "logout": // logout(req, res); // break; - // case "numbers": - // numbers(req, res); - // break; - // case "add": - // addNumber(req, res); - // break; - // case "write": - // writeNumber(req, res); - // break; + case "addnumber": + addNumber(req, res); + break; + case "writenumber": + writeNumber(req, res); + break; case "editnumber": editNumber(req, res); break; - // case "update": - // updateNumber(req, res); - // break; - // case "remove": - // removeNumber(req, res); - // break; + case "updatenumber": + updateNumber(req, res); + break; + case "delnumber": + delNumber(req, res); + break; default: res.redirect("/"); } diff --git a/source/data.d b/source/data.d index 0b6eaaa..e5fe6a2 100644 --- a/source/data.d +++ b/source/data.d @@ -6,7 +6,7 @@ import structures; import std.conv; -GroupDB[] getListGroups() { +GroupDB[] sqlGetListGroups() { GroupDB[] groups; try { auto queryResult = pgsql.sql( @@ -31,7 +31,7 @@ GroupDB[] getListGroups() { return groups; } -NumberDB[] getListNumbers(string group) { +NumberDB[] sqlGetListNumbers(string group) { NumberDB[] numbers; try { auto queryResult = pgsql.sql( @@ -44,7 +44,8 @@ NumberDB[] getListNumbers(string group) { dan.da_comment from da_numbers dan left join da_lists dal on dal.da_name = dan.da_list - where dan.da_group = ?", + where dan.da_group = ? + order by dan.da_number", group ); foreach (row; queryResult) { @@ -66,7 +67,7 @@ NumberDB[] getListNumbers(string group) { return numbers; } -NumberDB getDataNumber(string number) { +NumberDB sqlGetDataNumber(string number) { NumberDB data; try { auto queryResult = pgsql.sql( @@ -98,7 +99,7 @@ NumberDB getDataNumber(string number) { return data; } -GroupDB[] getGroups() { +GroupDB[] sqlGetGroups() { GroupDB[] groups; try { auto queryResult = pgsql.sql( @@ -119,7 +120,7 @@ GroupDB[] getGroups() { return groups; } -ListDB[] getLists() { +ListDB[] sqlGetLists() { ListDB[] lists; try { auto queryResult = pgsql.sql( @@ -139,3 +140,63 @@ ListDB[] getLists() { return lists; } + +bool sqlUpdateNumber(NumberDB number) { + try { + pgsql.sql( + "update da_numbers set + da_group = ?, + da_list = ?, + da_all_cc = ?, + da_white_cc = ?, + da_black_cc = ?, + da_comment = ? + where da_number = ?", + number.group, + number.list, + number.all_cc, + number.white_cc, + number.black_cc, + number.comment, + number.number + ); + } catch (Exception e) { + log.e("Ошибка обновления номера в БД. " ~ e.msg); + return false; + } + return true; +} + +bool sqlDeleteNumber(string number) { + try { + pgsql.sql( + "delete from da_numbers where da_number = ?", number + ); + } catch (Exception e) { + log.e("Ошибка удаления номера в БД. " ~ e.msg); + return false; + } + return true; +} + +bool sqlInsertNumber(NumberDB number) { + try { + pgsql.sql( + "insert into da_numbers + (da_number, da_group, da_list, da_all_cc, da_white_cc, da_black_cc, da_comment) + values + (?, ?, ?, ?, ?, ?, ?)", + number.number, + number.group, + number.list, + number.all_cc, + number.white_cc, + number.black_cc, + number.comment + ); + } catch (Exception e) { + log.error("Ошибка добавления номера телефона в БД. " ~ e.msg); + return false; + } + return true; +} diff --git a/source/requests/addnumber.d b/source/requests/addnumber.d new file mode 100644 index 0000000..2117b22 --- /dev/null +++ b/source/requests/addnumber.d @@ -0,0 +1,18 @@ +module requests.addnumber; + +import vibe.vibe; +import response; +import structures; +import data; +import singlog; + +void addNumber(HTTPServerRequest req, HTTPServerResponse res) { + auto jsr = req.json; + bool edit = false; + NumberDB dataNumber; + dataNumber.group = jsr["group"].get!string; + // auto dataNumber = sqlGetDataNumber(jsr["number"].get!string); + auto groups = sqlGetGroups(); + auto lists = sqlGetLists(); + render!("edit-number.dt", edit, dataNumber, groups, lists)(res); +} diff --git a/source/requests/delnumber.d b/source/requests/delnumber.d new file mode 100644 index 0000000..1f26183 --- /dev/null +++ b/source/requests/delnumber.d @@ -0,0 +1,24 @@ +module requests.delnumber; + +import vibe.vibe; +import response; +import structures; +import data; +import singlog; + +void delNumber(HTTPServerRequest req, HTTPServerResponse res) { + auto jsr = req.json; + string number = jsr["number"].get!string; + + if (!number.length) { + res.send(true, "Номер не может быть пуст"); + return; + } + + if (!sqlDeleteNumber(number)) { + res.send(true, "Не удалось удалить номер"); + return; + } + + res.send(); +} diff --git a/source/requests/editnumber.d b/source/requests/editnumber.d index 4d8be89..719027b 100644 --- a/source/requests/editnumber.d +++ b/source/requests/editnumber.d @@ -8,8 +8,8 @@ import singlog; void editNumber(HTTPServerRequest req, HTTPServerResponse res) { auto jsr = req.json; bool edit = true; - auto dataNumber = getDataNumber(jsr["number"].get!string); - auto groups = getGroups(); - auto lists = getLists(); + auto dataNumber = sqlGetDataNumber(jsr["number"].get!string); + auto groups = sqlGetGroups(); + auto lists = sqlGetLists(); render!("edit-number.dt", edit, dataNumber, groups, lists)(res); } diff --git a/source/requests/groupnumbers.d b/source/requests/groupnumbers.d index 1d7f73a..6163e2b 100644 --- a/source/requests/groupnumbers.d +++ b/source/requests/groupnumbers.d @@ -7,5 +7,5 @@ import singlog; void groupNumbers(HTTPServerRequest req, HTTPServerResponse res) { auto jsr = req.json; - res.writeJsonBody(getListNumbers(jsr["group"].get!string).serializeToJson()); + res.writeJsonBody(sqlGetListNumbers(jsr["group"].get!string).serializeToJson()); } diff --git a/source/requests/listsgroups.d b/source/requests/listsgroups.d index 94e6dbb..2512743 100644 --- a/source/requests/listsgroups.d +++ b/source/requests/listsgroups.d @@ -5,6 +5,6 @@ import response; import data; void listsGroups(HTTPServerRequest req, HTTPServerResponse res) { - auto listGroups = getListGroups(); + auto listGroups = sqlGetListGroups(); render!("group-numbers-list.dt", listGroups)(res); } diff --git a/source/requests/updatenumber.d b/source/requests/updatenumber.d new file mode 100644 index 0000000..edfdebb --- /dev/null +++ b/source/requests/updatenumber.d @@ -0,0 +1,16 @@ +module requests.updatenumber; + +import vibe.vibe; +import response; +import data; +import singlog; +import structures; + +void updateNumber(HTTPServerRequest req, HTTPServerResponse res) { + NumberDB number = deserializeJson!NumberDB(req.json); + if (!sqlUpdateNumber(number)) { + res.send(true, "Не удалось обновить номер"); + return; + } + res.send(); +} diff --git a/source/requests/writenumber.d b/source/requests/writenumber.d new file mode 100644 index 0000000..7ffa747 --- /dev/null +++ b/source/requests/writenumber.d @@ -0,0 +1,16 @@ +module requests.writenumber; + +import vibe.vibe; +import response; +import structures; +import data; +import singlog; + +void writeNumber(HTTPServerRequest req, HTTPServerResponse res) { + NumberDB number = deserializeJson!NumberDB(req.json); + if (!sqlInsertNumber(number)) { + res.send(true, "Не удалось записать номер"); + return; + } + res.send(); +} diff --git a/source/version_.d b/source/version_.d index 7164ef0..263b019 100644 --- a/source/version_.d +++ b/source/version_.d @@ -1,3 +1,3 @@ module version_; -enum dasterVersion = "v0.0.2"; +enum dasterVersion = "v0.0.4"; diff --git a/views/index.dt b/views/index.dt index e32e093..5b3f98f 100644 --- a/views/index.dt +++ b/views/index.dt @@ -10,15 +10,16 @@ head script(src='script.js') body div.div-header - // div.div-add - // button.addNumber Добавить номер + div.div-update.main-button + button#update + div.div-add.main-button + button#add-number Добавить номер div.div-search - input.input-focus.search(name='search', type='text', value='', placeholder='Найти номер') + input.input-focus#search(name='search', type='text', value='', placeholder='Найти номер') // div.div-user Вы вошли как #{user.name} - div.div-user Вы вошли как Александр - div.div-button - button Выход - // button(onclick='logout()') Выход + div.div-user + div.div-logout + button#logout Выход div.content div#tabs ul