From e5644264b661afde556981d5f57395fe4ef01ba3 Mon Sep 17 00:00:00 2001 From: Joe Beeton Date: Thu, 3 Feb 2022 11:37:25 +0000 Subject: [PATCH] Added delete functionality which was missing from the fontend and backend service. But was mentioned in the readme. --- .../datamanager/BookstoreDataService.java | 6 +++++ .../main/java/acme/frontend/BookService.java | 24 +++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/bookstore-data-manager/src/main/java/acme/datamanager/BookstoreDataService.java b/bookstore-data-manager/src/main/java/acme/datamanager/BookstoreDataService.java index 60d2f36..47ac047 100644 --- a/bookstore-data-manager/src/main/java/acme/datamanager/BookstoreDataService.java +++ b/bookstore-data-manager/src/main/java/acme/datamanager/BookstoreDataService.java @@ -42,6 +42,12 @@ public void addBook(@RequestBody final Book book) { books.add(book); } + @RequestMapping(path = "/delete", method = RequestMethod.POST) + public void deleteBook(@RequestBody final Book book) { + System.out.println("Deleting book: " + book.getTitle()); + books.removeIf(existingBook -> existingBook.getTitle().equals(book.getTitle())); + } + /** * This is how our legacy systems update a book when the page count needs to be updated. */ diff --git a/bookstore-frontend/src/main/java/acme/frontend/BookService.java b/bookstore-frontend/src/main/java/acme/frontend/BookService.java index 2452a1b..45bdb5c 100644 --- a/bookstore-frontend/src/main/java/acme/frontend/BookService.java +++ b/bookstore-frontend/src/main/java/acme/frontend/BookService.java @@ -41,17 +41,17 @@ public Response addBook(final InputStream body) throws JAXBException, IOExceptio final Unmarshaller unmarshaller = jaxb.createUnmarshaller(); final Book book = (Book) unmarshaller.unmarshal(body); System.out.println("Forwarding new book " + book.title + " to data-manager"); - sendToDataManager(book); + sendToDataManager(book,"add"); return Response.ok().build(); } /** - * Send the book to the microservice that actually stores it. + * Send the book to the microservice that actually actions request. */ - private void sendToDataManager(final Book book) throws IOException { + private void sendToDataManager(final Book book,String path) throws IOException { String bookJson = gson.toJson(book); final CloseableHttpClient client = HttpClientBuilder.create().build(); - final HttpUriRequest request = RequestBuilder.post(ServicePaths.DATA_MANAGER_URL + "add") + final HttpUriRequest request = RequestBuilder.post(ServicePaths.DATA_MANAGER_URL + path) .setHeader("Content-Type", "application/json") .setEntity(new StringEntity(bookJson)).build(); final CloseableHttpResponse response = client.execute(request); @@ -61,6 +61,22 @@ private void sendToDataManager(final Book book) throws IOException { } } + /** + * Take a request to add a delete a book, send it to the backend microservice asynchronously. This + * endpoint expects the book in XML form. Validate it's a valid book before forwarding on. + */ + @Path("/delete") + @POST + public Response deleteBook(final InputStream body) throws JAXBException, IOException { + final JAXBContext jaxb = JAXBContext.newInstance(Book.class); + final Unmarshaller unmarshaller = jaxb.createUnmarshaller(); + final Book book = (Book) unmarshaller.unmarshal(body); + System.out.println("Forwarding book to delete " + book.title + " to data-manager"); + sendToDataManager(book,"delete"); + return Response.ok().build(); + } + + /** * Call the bookstore-data-manager service to get the books. */