The task is to develop an admin panel for an online parts store. There is a table of products and orders in MySQL, the administrator can delete the product only if the product id is not used in the order database.

This is all clear. I made the deletion, now I want to display a mistake if the product is in the order, and I accidentally wanted to delete it. In the try / catch service layer, I catch and display the error message. But I would like to implement a redirect to the product page and display an error in the client.

I do not quite understand how to implement this action. Tell me please. I would be very grateful.

Removal Controller Code:

public class DeleteOrdController implements Controller { private OrdService ordService = OrdServiceIpl.getInstance(); private Logger logger = Logger.getLogger(DeleteOrdController.class); @Override public void execute(HttpServletRequest req, HttpServletResponse resp) { try{ String id = req.getParameter("deleteOrd"); long ordId = Long.parseLong(id); ordService.delete(ordId); String contextPath = req.getContextPath(); resp.sendRedirect(contextPath + "/frontController?command=orders"); } catch (IOException e) { logger.error(e); } } } 

Product table:

  <table> <tr> <th>№</th> <th>Parts id</th> <th>Producer</th> <th>Category</th> <th>Name parts</th> <th>Chatacteristics</th> <th>Price</th> <th>Delete</th> </tr> <c:forEach var="batteries" items="${parts}" varStatus="status"> <tr> <td>${status.index + 1}</td> <td>${batteries.id}</td> <td>${batteries.producer}</td> <td>${batteries.category}</td> <td>${batteries.name}</td> <td>${batteries.chatacteristics}</td> <td>${batteries.price}</td> <td><form action="frontController?command=deleteproduct" method="post"> <button value="${batteries.id}" name="delete" class="btn">Delete</button> </form></td> </tr> </c:forEach> </table> <br/> 

Service layer:

 @Override public int delete(Serializable id) { try { return partsDao.delete(id); } catch (SQLException e) { throw new ServiceException("Ошибка удаления Parts по id " + id); } } 

DAO

 @Override public int delete(Serializable id) throws SQLException { psDelete.setLong(1, (long) id); return psDelete.executeUpdate(); } 
  • I would like more information and then psychics are all on vacation. What do you use on UI and are you using back end? With such a question, you can just answer that where you catch an exception, redirect to the page. - Denis
  • Added by. So far, everything is primitive - Andrei Yuraga

1 answer 1

those. Before removing the goods (parts), you need to check its availability in the table (orders) and, if it is there, then display a warning ...

Another option is to create a column in the table of goods in how many orders the product participates ... and for example (it depends on your business logic) each new order increases this number by 1, each completed order decreases. If the value is greater than 0, then the item cannot be deleted

those. both in DAO and in the Service, special methods are needed that are responsible for checking

Correspondingly, the controller, having received the information that such a product is in orders, sends an indication to the output page, and there under a special tag

 <c:if test = "${canDelete eq false}"> сообщение об ошибке </c:if>