diff --git a/.idea/misc.xml b/.idea/misc.xml index 71286c2..6619eb6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/db.sql b/db.sql new file mode 100644 index 0000000..13a1d3d --- /dev/null +++ b/db.sql @@ -0,0 +1,156 @@ +-- phpMyAdmin SQL Dump +-- version 4.9.0.1 +-- https://www.phpmyadmin.net/ +-- +-- Host: localhost +-- Erstellungszeit: 06. Dez 2019 um 16:11 +-- Server-Version: 10.3.11-MariaDB +-- PHP-Version: 5.6.40 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET AUTOCOMMIT = 0; +START TRANSACTION; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- Datenbank: `wasteinformation` +-- + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `cities` +-- + +CREATE TABLE `cities` ( + `id` int(11) NOT NULL, + `userid` int(11) NOT NULL, + `name` varchar(256) NOT NULL, + `wastetype` varchar(64) NOT NULL, + `zone` int(11) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Daten für Tabelle `cities` +-- + +INSERT INTO `cities` (`id`, `userid`, `name`, `wastetype`, `zone`) VALUES +(2, 0, 'Steyr', 'Bio', 2), +(3, 0, 'Steyr', 'Bio', 3), +(4, 0, 'Steyr', 'Bio', 4), +(128, 0, 'Steyr', 'Biowaste', 1), +(130, 0, 'abc', 'Biowaste', 1); + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `pickupdates` +-- + +CREATE TABLE `pickupdates` ( + `id` int(11) NOT NULL, + `citywastezoneid` int(11) NOT NULL, + `pickupdate` date NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Daten für Tabelle `pickupdates` +-- + +INSERT INTO `pickupdates` (`id`, `citywastezoneid`, `pickupdate`) VALUES +(6, 4, '2019-12-06'), +(7, 3, '2019-12-06'), +(8, 2, '2019-12-06'), +(9, 130, '2019-12-26'); + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `user` +-- + +CREATE TABLE `user` ( + `id` int(11) NOT NULL, + `username` varchar(150) NOT NULL, + `firstName` varchar(32) NOT NULL, + `secondName` varchar(32) NOT NULL, + `password` varchar(32) NOT NULL, + `permission` int(11) NOT NULL DEFAULT 0, + `email` varchar(64) NOT NULL, + `logindate` timestamp NOT NULL DEFAULT current_timestamp() +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Daten für Tabelle `user` +-- + +INSERT INTO `user` (`id`, `username`, `firstName`, `secondName`, `password`, `permission`, `email`, `logindate`) VALUES +(2, 'lheilige', 'lukas', 'heiligenbrunner', 'c6e2ddf577e0dfdc4366f788f1b27102', 0, 'lukas.heiligenbrunner@gmail.com', '2019-09-20 12:19:31'), +(11, 'meindl', 'emil', 'meindl', '81dc9bdb52d04dc20036dbd8313ed055', 0, 'mailmeindl', '2019-09-27 10:23:46'), +(15, 'horni', 'kk', 'kk', 'c4ca4238a0b923820dcc509a6f75849b', 1, 'kk', '2019-09-27 13:19:09'); + +-- +-- Indizes der exportierten Tabellen +-- + +-- +-- Indizes für die Tabelle `cities` +-- +ALTER TABLE `cities` + ADD PRIMARY KEY (`id`); + +-- +-- Indizes für die Tabelle `pickupdates` +-- +ALTER TABLE `pickupdates` + ADD PRIMARY KEY (`id`), + ADD KEY `citywastezoneid` (`citywastezoneid`); + +-- +-- Indizes für die Tabelle `user` +-- +ALTER TABLE `user` + ADD PRIMARY KEY (`id`); + +-- +-- AUTO_INCREMENT für exportierte Tabellen +-- + +-- +-- AUTO_INCREMENT für Tabelle `cities` +-- +ALTER TABLE `cities` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=131; + +-- +-- AUTO_INCREMENT für Tabelle `pickupdates` +-- +ALTER TABLE `pickupdates` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10; + +-- +-- AUTO_INCREMENT für Tabelle `user` +-- +ALTER TABLE `user` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=16; + +-- +-- Constraints der exportierten Tabellen +-- + +-- +-- Constraints der Tabelle `pickupdates` +-- +ALTER TABLE `pickupdates` + ADD CONSTRAINT `pickupdates_ibfk_1` FOREIGN KEY (`citywastezoneid`) REFERENCES `cities` (`id`); +COMMIT; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/src/java/com/wasteinformationserver/db/JDCB.java b/src/java/com/wasteinformationserver/db/JDCB.java index 3bc644f..61949c9 100644 --- a/src/java/com/wasteinformationserver/db/JDCB.java +++ b/src/java/com/wasteinformationserver/db/JDCB.java @@ -3,10 +3,7 @@ package com.wasteinformationserver.db; import com.wasteinformationserver.basicutils.Log; import java.io.IOException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; public class JDCB { static Connection conn; @@ -68,14 +65,9 @@ public class JDCB { return null; } - public int executeUpdate(String sql) { - try { + public int executeUpdate(String sql) throws SQLException { PreparedStatement stmt = conn.prepareStatement(sql); return stmt.executeUpdate(); - } catch (SQLException e) { - e.printStackTrace(); - } - return -1; } } diff --git a/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java b/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java index d72c020..59efd52 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java @@ -7,6 +7,7 @@ import com.wasteinformationserver.website.basicrequest.PostRequest; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.SQLIntegrityConstraintViolationException; import java.util.HashMap; public class DataRequest extends PostRequest { @@ -42,7 +43,12 @@ public class DataRequest extends PostRequest { if (size == 0) { //doesnt exist System.out.println("doesnt exist"); - int status = jdcb.executeUpdate("INSERT INTO `cities`(`userid`, `name`, `wastetype`, `zone`) VALUES ('0','" + params.get("cityname") + "','" + params.get("wastetype") + "','" + params.get("wastezone") + "');"); + int status = 0; + try { + status = jdcb.executeUpdate("INSERT INTO `cities`(`userid`, `name`, `wastetype`, `zone`) VALUES ('0','" + params.get("cityname") + "','" + params.get("wastetype") + "','" + params.get("wastezone") + "');"); + } catch (SQLException e) { + e.printStackTrace(); + } System.out.println(status); if (status == 1) { sb.append("\"status\" : \"inserted\""); @@ -95,20 +101,27 @@ public class DataRequest extends PostRequest { //DELETE FROM `cities` WHERE `id`=0 StringBuilder sbb = new StringBuilder(); // TODO: 06.12.19 better naming and sb for all - - Log.debug(params.get("id")); - int status= jdcb.executeUpdate("DELETE FROM `cities` WHERE `id`='" + params.get("id")+"'"); - Log.debug(status); - sbb.append("{"); - - if (status == 1){ - //success - sbb.append("\"status\" : \"success\""); - }else { + Log.debug(params.get("id")); + int status = 0; + try{ + status= jdcb.executeUpdate("DELETE FROM `cities` WHERE `id`='" + params.get("id")+"'"); + if (status == 1){ + //success + sbb.append("\"status\" : \"success\""); + }else { + sbb.append("\"status\" : \"error\""); + } + }catch (SQLIntegrityConstraintViolationException e){ + Log.warning("dependencies of deletion exist"); + sbb.append("\"status\" : \"dependenciesnotdeleted\""); + } catch (SQLException e) { + Log.error("sql exception: "+e.getMessage()); sbb.append("\"status\" : \"error\""); } + Log.debug(status); + sbb.append(",\"query\":\"ok\""); sbb.append("}"); result = sbb.toString(); diff --git a/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java b/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java index d9ece0e..e179a07 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java @@ -28,13 +28,13 @@ public class NewDateRequest extends PostRequest { try { String prev = ""; while (sett.next()) { - if (prev.equals(sett.getString("name"))){ + if (prev.equals(sett.getString("name"))) { - }else { - sb.append("{\"cityname\":\"" + sett.getString("name") + "\"}"); - if (!sett.isLast()) { + } else { + if (!sett.isFirst()) { sb.append(","); } + sb.append("{\"cityname\":\"" + sett.getString("name") + "\"}"); } prev = sett.getString("name"); } @@ -44,24 +44,24 @@ public class NewDateRequest extends PostRequest { sb.append("]"); sb.append(",\"query\":\"ok\""); sb.append("}"); + Log.debug(sb.toString()); break; case "getzones": - ResultSet set = jdcb.executeQuery("select * from cities WHERE `name`='"+params.get("cityname")+"' ORDER BY zone ASC"); + ResultSet set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC"); Log.debug(set.toString()); sb.append("{\"data\":["); try { int prev = 42; while (set.next()) { - if (prev == set.getInt("zone")){ + if (prev == set.getInt("zone")) { - }else { + } else { sb.append("{\"zone\":\"" + set.getInt("zone") + "\"}"); if (!set.isLast()) { sb.append(","); } } prev = set.getInt("zone"); - System.out.println(prev); } } catch (SQLException e) { e.printStackTrace(); @@ -70,6 +70,32 @@ public class NewDateRequest extends PostRequest { sb.append(",\"query\":\"ok\""); sb.append("}"); break; + case "newdate": + sb.append("{"); + Log.debug(params); + ResultSet seti = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zone") + "' AND `wastetype`='" + params.get("wastetype") + "'"); + try { + seti.last(); + if (seti.getRow() == 1) { + Log.debug(seti.getInt("id")); + + int status = jdcb.executeUpdate("INSERT INTO `pickupdates`(`citywastezoneid`, `pickupdate`) VALUES ('" + seti.getInt("id") + "','" + params.get("date") + "')"); + if (status == 1) { + sb.append("\"status\" : \"success\""); + } else { + sb.append("\"status\" : \"error\""); + } + } else { + Log.warning("city doesnt exist!"); + sb.append("\"status\" : \"citydoesntexist\""); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + sb.append(",\"query\":\"ok\""); + sb.append("}"); + break; } return sb.toString(); } diff --git a/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java b/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java index 3ca4090..340cc71 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java @@ -6,6 +6,7 @@ import com.wasteinformationserver.website.HttpTools; import com.wasteinformationserver.website.basicrequest.PostRequest; import java.io.IOException; +import java.sql.SQLException; import java.util.HashMap; public class RegisterRequest extends PostRequest { @@ -22,7 +23,11 @@ public class RegisterRequest extends PostRequest { e.printStackTrace(); } //new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation"); - int s = myjd.executeUpdate("INSERT INTO `user` (`username`, `firstName`, `secondName`, `password`, `email`, `logindate`) VALUES ('"+params.get("username")+"', '"+params.get("firstname")+"', '"+params.get("lastname")+"', '"+passhash+"', '"+params.get("email")+"', current_timestamp());"); + try { + int s = myjd.executeUpdate("INSERT INTO `user` (`username`, `firstName`, `secondName`, `password`, `email`, `logindate`) VALUES ('"+params.get("username")+"', '"+params.get("firstname")+"', '"+params.get("lastname")+"', '"+passhash+"', '"+params.get("email")+"', current_timestamp());"); + } catch (SQLException e) { + e.printStackTrace(); + } // TODO: 27.09.19 detect if register process was successful and reply right json return "{\"accept\": true}"; diff --git a/src/resources/wwwroot/dashboard.html b/src/resources/wwwroot/dashboard.html index 4c1fdbb..99b7f5c 100644 --- a/src/resources/wwwroot/dashboard.html +++ b/src/resources/wwwroot/dashboard.html @@ -203,7 +203,28 @@
- + + + + + + + + + + + + + + + + + + + + + +
CityZoneWaste TypeDateX
CityZoneWaste TypeDateX
@@ -241,10 +262,10 @@ Select waste type @@ -258,9 +279,6 @@ New Pick up date -
@@ -288,7 +306,7 @@
-
- - -

Your Data:

- - - - - - - - - - - -
Waste typeDateRegionZone
diff --git a/src/resources/wwwroot/js/dashboard.js b/src/resources/wwwroot/js/dashboard.js index ae4a6d5..8b601b2 100644 --- a/src/resources/wwwroot/js/dashboard.js +++ b/src/resources/wwwroot/js/dashboard.js @@ -12,10 +12,14 @@ $(document).ready(function () { } }, 'json'); - var table; + var citytable; function reloadtable() { $.post('/senddata/wastedata', 'action=getAllCities', function (data) { + if (citytable != null) { + citytable.destroy(); //delete table if already created + } + console.log(data); if (data.query == "ok") { $('#location-table-data').html(""); @@ -35,32 +39,33 @@ $(document).ready(function () { console.log("clicked btn data " + id); $.post('/senddata/wastedata', 'action=deletecity&id=' + id, function (data) { console.log(data); - Swal.fire({ - type: "success", - title: 'Successfully deleted city!', - html: 'This alert closes automatically.', - timer: 1000, - }).then((result) => { - console.log('Popup closed. ') + if (data.status == "success") { + Swal.fire({ + type: "success", + title: 'Successfully deleted city!', + html: 'This alert closes automatically.', + timer: 1000, + }).then((result) => { + console.log('Popup closed. ') - }); - table.destroy(); //todo in reloadtable maybe - reloadtable(); - }); + }); + reloadtable(); + } else if (data.status == "dependenciesnotdeleted") { + Swal.fire({ + type: "warning", + title: 'This city is a dependency of a date', + html: 'Do you want do delete it anyway with all dependencies?', + }).then((result) => { + console.log('Popup closed. ') + + }); + //todo set yes no button here + } + + }, "json"); }); - //todo entweda 1 od 2 - // $("#example2").reload(); - table = $("#example2").DataTable(); - - // $('#example1').DataTable({ - // "paging": true, - // "lengthChange": false, - // "searching": false, - // "ordering": true, - // "info": true, - // "autoWidth": false, - // }); + citytable = $("#example2").DataTable(); } else if (data.query == "nodbconn") { Swal.fire({ type: "error", @@ -74,11 +79,23 @@ $(document).ready(function () { console.log("Error: " + data.query); } - }, 'json'); } + + var DataTable; + function reloadDateTable() { + $.post('/senddata/wastedata', 'action=getAllDates', function (data) { + if (DataTable != null) { + DataTable.destroy(); //delete table if already created + } + + //todo + },"json"); + } + reloadtable(); + reloadDateTable(); //btn listeners @@ -88,7 +105,8 @@ $(document).ready(function () { }, 'json'); }); - $('.dropdown-item').click(function () { + $('.wastetype-citynew-item').click(function (event) { + event.preventDefault(); $('#dropdown-wastetype').html($(this).html()); }); @@ -110,7 +128,6 @@ $(document).ready(function () { console.log('Popup closed. ') }); - table.destroy(); reloadtable(); } else if (data.status == "exists") { Swal.fire({ @@ -122,18 +139,12 @@ $(document).ready(function () { }); } - - }, 'json'); //clear form data $("#new_city_cityname").val(""); $("#new_city_zonename").val(""); $("#dropdown-wastetype").html("select waste type"); - - - //todo reload table. - }); @@ -142,6 +153,7 @@ $(document).ready(function () { event.preventDefault(); var dropdata = $("#dropdown-city-data"); dropdata.html(""); + console.log("loading city names") $.post('/senddata/newdate', 'action=getCitynames', function (data) { console.log(data); @@ -159,7 +171,7 @@ $(document).ready(function () { $("#dropdown-city").html($(this).html()); }); } - }); + }, "json"); }); $("#dropdown-zone").click(function (event) { @@ -167,7 +179,7 @@ $(document).ready(function () { var dropdata = $("#dropdown-zone-data"); dropdata.html(""); - $.post('/senddata/newdate', 'action=getzones&cityname='+$("#dropdown-city").html(), function (data) { + $.post('/senddata/newdate', 'action=getzones&cityname=' + $("#dropdown-city").html(), function (data) { console.log(data); if (data.query == "ok") { var prev = ""; @@ -188,24 +200,47 @@ $(document).ready(function () { $(".dropdown-item-wastetype").click(function (event) { event.preventDefault(); - $("#dropdown-type-data1").html($(this).html()); + $("#dropdown-type-data").html($(this).html()); }); - $('#btn-savelist').click(function () { - console.log("saving list"); - var wastetypearr = $('.td-dropdown-wastetype'); - var wastetime = $('.td-input-wastetime'); - var wasteregionarr = $('.td-input-wasteregion'); - var wastezonearr = $('.td-input-wastezone'); + $('.btn-savelist').click(function () { + console.log("saving date"); - for (var i = 0; i < wastetypearr.length; i++) { - console.log(wastetypearr[i].innerHTML); - $.post('/senddata/wastedata', 'action=senddata&wastetype=' + wastetypearr[i].innerHTML + "&wastetime=" + wastetime[i].innerHTML + "&wasteregion=" + wasteregionarr[i].innerHTML + "&wastezone=" + wastezonearr[i].innerHTML, function (data) { - console.log(data); - }, 'text'); - } + var cityname = $("#dropdown-city"); + var zone = $("#dropdown-zone"); + var wastetype = $("#dropdown-type-data"); + var date = $("#input-wastetime"); + $.post('/senddata/newdate', 'action=newdate&cityname=' + cityname.html() + "&zone=" + zone.html() + "&wastetype=" + wastetype.html() + "&date=" + date.val(), function (data) { + if (data.status == "success") { + Swal.fire({ + type: "success", + title: 'Successfully created Date!', + html: 'This alert closes automatically.', + timer: 1000, + }).then((result) => { + console.log('Popup closed. ') + + }); + + cityname.html("Select City"); + zone.html("Select Zone"); + wastetype.html("Select waste type"); + date.val(""); + } else if (data.status == "citydoesntexist") { + Swal.fire({ + type: "warning", + title: 'city name doesnt exist', + html: 'Close popup.', + }).then((result) => { + console.log('Popup closed. ') + + }); + } + + console.log(data) + }, "json"); }); @@ -213,7 +248,7 @@ $(document).ready(function () { var date_input = $('input[name="date"]'); //our date input has the name "date" var container = $('.bootstrap-iso form').length > 0 ? $('.bootstrap-iso form').parent() : "body"; var options = { - format: 'mm/dd/yyyy', + format: 'yyyy-mm-dd', container: container, todayHighlight: true, autoclose: true,