Skip to content

Commit

Permalink
Fully integrate ISBNValidation into service
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Philip authored and Jan Philip committed Oct 18, 2021
1 parent af57f12 commit 21e57bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.hswhameln.isbnvalidator.exceptions.BookAlreadyExistsException;
import de.hswhameln.isbnvalidator.exceptions.BookNotFoundException;
import de.hswhameln.isbnvalidator.exceptions.ISBNNotValidException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
Expand All @@ -12,6 +13,8 @@

import de.hswhameln.isbnvalidator.beans.Book;
import de.hswhameln.isbnvalidator.repositories.BookRepository;
import de.hswhameln.isbnvalidator.utils.ISBNAPI;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -39,24 +42,28 @@ public BookService(BookRepository repository) {
* @return Bookobjekt
*/
public Optional<Book> findBook(String isbn) {
// BookValidation.checkBook(isbn);

if(!ISBNAPI.validateISBN(isbn)) {
throw new ISBNNotValidException(isbn);
}
return repository.findByisbn(isbn);
}

/**
* Fügt ein neues Buch in der DB hinzu.
* Dabei wird geprüft ob diese Buch bereits in der DB existiert.
* Dabei wird geprüft ob diese Buch bereits in der DB existiert
* und ob die ISBN des Buches korrekt ist.
* In diesem Fall wird ein Fehler ausgegeben.
*
* @param book Book as Entitdy
*/
public void createBook(Book book) {
if(!ISBNAPI.validateISBN(book.getIsbn())) {
throw new ISBNNotValidException(book.getIsbn());
}
if(this.repository.findByisbn(book.getIsbn()).isPresent()) {
throw new BookAlreadyExistsException(book.getIsbn());
} else {
this.repository.save(book);
}
this.repository.save(book);
}

/**
Expand Down Expand Up @@ -85,32 +92,4 @@ public void deleteBook(Book book){
throw new BookNotFoundException(book.getIsbn());
}
}

/**
* Prüft die ISBN eines Buches auf Korrektheit.
*
* @param isbn
* @return
*/
private boolean validateISBN(String isbn) {
String url = "localhost:3000/validateISBN";

Map<String, String> params = new HashMap<String, String>();
params.put("isbn", isbn);

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity( url, params, String.class );

//BodyCall
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("email", "[email protected]");

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);

ResponseEntity<String> response1 = restTemplate.postForEntity( url, request , String.class );
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@

public class ISBNAPI {

private static RestConsumer isbnValidator;

public static boolean validateISBN(String isbn) {
if(isbnValidator == null) {
isbnValidator = new RestConsumer("http://localhost:3000");
}
JSONObject response = isbnValidator.getRequest("/validateISBN", isbn);
RestConsumer isbnValidator = RestConsumer.getInstance();
JSONObject response = isbnValidator.getRequest("/ValidateISBN", isbn);
try {
return response.getBoolean("valid");
} catch (JSONException e) {
Expand All @@ -21,9 +17,7 @@ public static boolean validateISBN(String isbn) {
}

public static int getCheckDigit(String isbn) {
if(isbnValidator == null) {
isbnValidator = new RestConsumer("http://localhost:3000");
}
RestConsumer isbnValidator = RestConsumer.getInstance();
JSONObject response = isbnValidator.getRequest("/checkDigit", isbn);
try {
return response.getInt("checkDigit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@
import org.json.JSONObject;

public class RestConsumer {
private static RestConsumer instance;

public static RestConsumer getInstance() {
if(instance == null) {
instance = new RestConsumer("https://validateisbn.azurewebsites.net/api/");
}
return instance;
}

private String url;

public RestConsumer(String url) {
private RestConsumer(String url) {
this.url = url;
}

Expand Down

0 comments on commit 21e57bb

Please sign in to comment.