diff --git a/src/main/java/com/s3ai/controllers/cinemas/CinemaEdit.java b/src/main/java/com/s3ai/controllers/cinemas/CinemaEdit.java index a8e38bf6fbedaba5496c3f610840e2d8020220d3..c5e677854a952b74a6d0778be9315df9950e6725 100644 --- a/src/main/java/com/s3ai/controllers/cinemas/CinemaEdit.java +++ b/src/main/java/com/s3ai/controllers/cinemas/CinemaEdit.java @@ -48,4 +48,19 @@ public class CinemaEdit extends HttpServlet { session.setAttribute("cinema", cinema); dispatcher.forward(req, resp); } + + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String id = req.getParameter("id"); + if (null == id) { + resp.sendError(420, "Object cannot be found"); + return; + } + var cinema = cinemaService.getCinemaById(UUID.fromString(id)); + if (null == cinema) { + resp.sendError(420, "Object cannot be found"); + return; + } + cinemaService.deleteCinema(cinema); + resp.setStatus(200); + } } \ No newline at end of file diff --git a/src/main/java/com/s3ai/controllers/cinemas/CinemasList.java b/src/main/java/com/s3ai/controllers/cinemas/CinemasList.java index 22636be4bcc4b97adf991d722cbb0e7cf428d724..4d76f2092ccf8be93569f8b8b8d5367e4fa6564e 100644 --- a/src/main/java/com/s3ai/controllers/cinemas/CinemasList.java +++ b/src/main/java/com/s3ai/controllers/cinemas/CinemasList.java @@ -1,6 +1,5 @@ package com.s3ai.controllers.cinemas; -import com.s3ai.entities.Cinema; import com.s3ai.services.CinemaService; import javax.servlet.RequestDispatcher; @@ -9,18 +8,11 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.util.UUID; public class CinemasList extends HttpServlet { private CinemaService cinemaService = CinemaService.getInstance(); - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String id = request.getReader().readLine(); - Cinema cinema = cinemaService.getCinemaById(UUID.fromString(id)); - cinemaService.deleteCinema(cinema); - } - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet"); RequestDispatcher dispatcher = request.getRequestDispatcher("pages/cinemas/list.jsp"); diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 90e61c51169d5fec43edec0267c235f92b5df7d8..2638cdb964e8114a55b9690e195b581dc5804964 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -64,12 +64,12 @@ </servlet-mapping> <servlet> - <servlet-name>CinemaEditServlet</servlet-name> + <servlet-name>CinemaUpdateServlet</servlet-name> <servlet-class>com.s3ai.controllers.cinemas.CinemaEdit</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> - <servlet-name>CinemaEditServlet</servlet-name> - <url-pattern>/cinema/edit/*</url-pattern> + <servlet-name>CinemaUpdateServlet</servlet-name> + <url-pattern>/cinema/*</url-pattern> </servlet-mapping> </web-app> diff --git a/src/main/webapp/pages/cinemas/edit.jsp b/src/main/webapp/pages/cinemas/edit.jsp index b0d1ca4403cff865c7ad1bd0876befe1668415c9..36391529335d644531d480d4dd5d14006ade0617 100644 --- a/src/main/webapp/pages/cinemas/edit.jsp +++ b/src/main/webapp/pages/cinemas/edit.jsp @@ -14,7 +14,7 @@ <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/cinemas.css"> <script src="${pageContext.request.contextPath}/static/js/cinemas.js"></script> </head> -<body> +<body class="themed-gradient"> <% Cinema cinema = (Cinema) request.getSession().getAttribute("cinema"); UUID id = cinema.getId(); @@ -25,7 +25,7 @@ <!-- Navigation --> <%@include file='../templates/header.jsp' %> -<div class="editing themed-gradient"> +<div class="editing"> <div class="container justify-content-center"> <form id="cinemaForm" class="d-flex justify-content-center" method="post" action="${pageContext.request.contextPath}/cinema/edit"> @@ -62,7 +62,7 @@ id="cinemaSeats" name="seatsCount" placeholder="Cinema's seats count" - value="<%out.println(seatsCount);%>" required> + value="<%out.print(seatsCount);%>" required> </div> </div> </div> diff --git a/src/main/webapp/pages/cinemas/list.jsp b/src/main/webapp/pages/cinemas/list.jsp index 2bcb334faf17565437fde11f493ab896eaf26184..fc4caa4abbdd6ad277637ba46d58b5ec5c72b1fe 100644 --- a/src/main/webapp/pages/cinemas/list.jsp +++ b/src/main/webapp/pages/cinemas/list.jsp @@ -13,12 +13,11 @@ <title>Cinemas list</title> <%@include file='../templates/imports.jsp' %> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/cinemas.css"> - <script src="${pageContext.request.contextPath}/static/js/cinemas.js"></script> </head> -<body> +<body class="themed-gradient"> <%@include file='../templates/header.jsp' %> -<div class="container-fluid themed-gradient"> +<div class="container-fluid"> <div class="row table-padding justify-content-center"> <table class="table col-6 table-striped table-dark"> <thead> @@ -34,17 +33,65 @@ List<Cinema> cinemas = (List<Cinema>) request.getSession().getAttribute("cinemas"); for (int i = 0; i < cinemas.size(); i++) { Cinema cinema = cinemas.get(i); - out.println("<tr id='" + cinema.getId().toString() + "'>"); + out.println("<tr cinema_id='" + cinema.getId().toString() + "'>"); out.println("<td scope=\"col\">" + (i + 1) + "</td>"); - out.println("<td>" + (cinema.getName() == null ? "" : cinema.getName()) + "</td>"); - out.println("<td>" + (cinema.getLocation() == null ? "" : cinema.getLocation()) + "</td>"); - out.println("<td>" + (cinema.getSeatsCount() == null ? "" : cinema.getSeatsCount()) + "</td>"); + out.println("<td attr_name=\"name\">" + (cinema.getName() == null ? "" : cinema.getName()) + "</td>"); + out.println("<td attr_name=\"location\">" + (cinema.getLocation() == null ? "" : cinema.getLocation()) + "</td>"); + out.println("<td attr_name=\"seats\">" + (cinema.getSeatsCount() == null ? "" : cinema.getSeatsCount()) + "</td>"); out.println("</tr>"); } %> </tbody> </table> </div> + <div class="modal fade" id="cinemaModal" tabindex="-1" role="dialog" + aria-labelledby="cinemaModalTitle" aria-hidden="true"> + <div class="modal-dialog modal-dialog-centered" role="document"> + <div class="modal-content"> + <div class="modal-header"> + <h5 class="modal-title" id="exampleModalLongTitle">Cinema info</h5> + <button type="button" class="close" data-dismiss="modal" aria-label="Close"> + <span aria-hidden="true">×</span> + </button> + </div> + <div class="modal-body"> + <div class="d-flex flex-column"> + <div class="d-flex flex-row justify-content-between"> + <p>ID:</p> + <p id="modal_id"></p> + </div> + <div class="d-flex flex-row justify-content-between"> + <p>Name:</p> + <p id="modal_name"></p> + </div> + <div class="d-flex flex-row justify-content-between"> + <p>Location:</p> + <p id="modal_location">value</p> + </div> + <div class="d-flex flex-row justify-content-between"> + <p>Seats count:</p> + <p id="modal_seats">value</p> + </div> + </div> + </div> + <div class="modal-footer"> + <button type="button" id="edit_cinema" class="btn btn-primary"><i class="fas fa-edit"></i> Edit + cinema + </button> + <button type="button" id="delete_cinema" class="btn btn-danger"><i class="fas fa-trash-alt"></i> + Delete cinema + </button> + <button type="button" class="btn btn-secondary" data-dismiss="modal"><i class="fas fa-times"></i> + Close + </button> + </div> + </div> + </div> + </div> </div> </body> +<script> + let context_url = "${pageContext.request.contextPath}" +</script> +<script src="${pageContext.request.contextPath}/static/js/cinemas.js"></script> </html> diff --git a/src/main/webapp/pages/templates/imports.jsp b/src/main/webapp/pages/templates/imports.jsp index 72afc8f694cfceff1bc51d32e435252064535ead..c1fcbe66c86b85c2783a0e3bfa957ddb07179f8f 100644 --- a/src/main/webapp/pages/templates/imports.jsp +++ b/src/main/webapp/pages/templates/imports.jsp @@ -11,3 +11,4 @@ <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/jquery.toast.min.css"> <script src="${pageContext.request.contextPath}/static/js/jquery-3.4.1.min.js"></script> <script src="${pageContext.request.contextPath}/static/js/jquery.toast.min.js"></script> +<script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script> diff --git a/src/main/webapp/static/coffee/cinemas.coffee b/src/main/webapp/static/coffee/cinemas.coffee index ec60c880634a6007eb5f5d273be60c628e910ff2..6c22ba16dfd0ab22f8da0f56a69d4e7f30f5f1e6 100644 --- a/src/main/webapp/static/coffee/cinemas.coffee +++ b/src/main/webapp/static/coffee/cinemas.coffee @@ -1,4 +1,31 @@ +fill_modal = (event) -> + target = $(this) + cinema_id = target.attr 'cinema_id' + if undefined != cinema_id and "" != cinema_id + $("#modal_id").text cinema_id + $("#modal_name").text target.find("[attr_name='name']").text() + $("#modal_location").text target.find("[attr_name='location']").text() + $("#modal_seats").text target.find("[attr_name='seats']").text() + $("#cinemaModal").modal('toggle') + +hrefToEdit = (event) -> + cinema_id = $("#modal_id").text() + window.location.replace "#{context_url}/cinema/?id=#{cinema_id}" + +removeCinema = (event) -> + cinema_id = $("#modal_id").text() + $.ajax + url: "#{context_url}/cinema/?id=#{cinema_id}", + type: 'DELETE' + error: (jqXHR, textStatus, errorThrown) -> + console.log "AJAX Error: #{textStatus}" + success: (data, textStatus, jqXHR) -> + $("tr[cinema_id='#{cinema_id}").remove() + $("#cinemaModal").modal('toggle') + addListeners = -> - console.log('Testing') + $('tr').on 'click', fill_modal + $('#edit_cinema').on 'click', hrefToEdit + $('#delete_cinema').on 'click', removeCinema $(document).ready(addListeners) \ No newline at end of file diff --git a/src/main/webapp/static/js/cinemas.js b/src/main/webapp/static/js/cinemas.js index ce3ce203ae5ca519909984045cd49a272893b568..46c7d97974aeef1b8445a536bb46bdca77d3f12c 100644 --- a/src/main/webapp/static/js/cinemas.js +++ b/src/main/webapp/static/js/cinemas.js @@ -1,9 +1,46 @@ // Generated by CoffeeScript 2.4.1 (function() { - var addListeners; + var addListeners, fill_modal, hrefToEdit, removeCinema; + + fill_modal = function(event) { + var cinema_id, target; + target = $(this); + cinema_id = target.attr('cinema_id'); + if (void 0 !== cinema_id && "" !== cinema_id) { + $("#modal_id").text(cinema_id); + $("#modal_name").text(target.find("[attr_name='name']").text()); + $("#modal_location").text(target.find("[attr_name='location']").text()); + $("#modal_seats").text(target.find("[attr_name='seats']").text()); + return $("#cinemaModal").modal('toggle'); + } + }; + + hrefToEdit = function(event) { + var cinema_id; + cinema_id = $("#modal_id").text(); + return window.location.replace(`${context_url}/cinema/?id=${cinema_id}`); + }; + + removeCinema = function(event) { + var cinema_id; + cinema_id = $("#modal_id").text(); + return $.ajax({ + url: `${context_url}/cinema/?id=${cinema_id}`, + type: 'DELETE', + error: function(jqXHR, textStatus, errorThrown) { + return console.log(`AJAX Error: ${textStatus}`); + }, + success: function(data, textStatus, jqXHR) { + $(`tr[cinema_id='${cinema_id}`).remove(); + return $("#cinemaModal").modal('toggle'); + } + }); + }; addListeners = function() { - return console.log('Testing'); + $('tr').on('click', fill_modal); + $('#edit_cinema').on('click', hrefToEdit); + return $('#delete_cinema').on('click', removeCinema); }; $(document).ready(addListeners);