Skip to content

Added delete functionality #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
24 changes: 20 additions & 4 deletions bookstore-frontend/src/main/java/acme/frontend/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
*/
Expand Down