Skip to content

Instantly share code, notes, and snippets.

@limadelrey
Last active January 20, 2021 23:33
Show Gist options
  • Save limadelrey/fe461ed060c01e527a5f0e2bc26b54e3 to your computer and use it in GitHub Desktop.
Save limadelrey/fe461ed060c01e527a5f0e2bc26b54e3 to your computer and use it in GitHub Desktop.
BookHandler
public class BookHandler {
private static final String ID_PARAMETER = "id";
private static final String PAGE_PARAMETER = "page";
private static final String LIMIT_PARAMETER = "limit";
private final BookService bookService;
public BookHandler(BookService bookService) {
this.bookService = bookService;
}
/**
* Read all books
* It should return 200 OK in case of success
* It should return 400 Bad Request, 404 Not Found or 500 Internal Server Error in case of failure
*
* @param rc Routing context
* @return BookGetAllResponse
*/
public Future<BookGetAllResponse> readAll(RoutingContext rc) {
final String page = rc.queryParams().get(PAGE_PARAMETER);
final String limit = rc.queryParams().get(LIMIT_PARAMETER);
return bookService.readAll(page, limit)
.onSuccess(success -> ResponseUtils.buildOkResponse(rc, success))
.onFailure(throwable -> ResponseUtils.buildErrorResponse(rc, throwable));
}
/**
* Read one book
* It should return 200 OK in case of success
* It should return 400 Bad Request, 404 Not Found or 500 Internal Server Error in case of failure
*
* @param rc Routing context
* @return BookGetByIdResponse
*/
public Future<BookGetByIdResponse> readOne(RoutingContext rc) {
final String id = rc.pathParam(ID_PARAMETER);
return bookService.readOne(Integer.parseInt(id))
.onSuccess(success -> ResponseUtils.buildOkResponse(rc, success))
.onFailure(throwable -> ResponseUtils.buildErrorResponse(rc, throwable));
}
/**
* Create one book
* It should return 201 Created in case of success
* It should return 400 Bad Request, 404 Not Found or 500 Internal Server Error in case of failure
*
* @param rc Routing context
* @return BookGetByIdResponse
*/
public Future<BookGetByIdResponse> create(RoutingContext rc) {
final Book book = rc.getBodyAsJson().mapTo(Book.class);
return bookService.create(book)
.onSuccess(success -> ResponseUtils.buildCreatedResponse(rc, success))
.onFailure(throwable -> ResponseUtils.buildErrorResponse(rc, throwable));
}
/**
* Update one book
* It should return 200 OK in case of success
* It should return 400 Bad Request, 404 Not Found or 500 Internal Server Error in case of failure
*
* @param rc Routing context
* @return BookGetByIdResponse
*/
public Future<BookGetByIdResponse> update(RoutingContext rc) {
final String id = rc.pathParam(ID_PARAMETER);
final Book book = rc.getBodyAsJson().mapTo(Book.class);
return bookService.update(Integer.parseInt(id), book)
.onSuccess(success -> ResponseUtils.buildOkResponse(rc, success))
.onFailure(throwable -> ResponseUtils.buildErrorResponse(rc, throwable));
}
/**
* Delete one book
* It should return 204 No Content in case of success
* It should return 400 Bad Request, 404 Not Found or 500 Internal Server Error in case of failure
*
* @param rc Routing context
* @return BookGetByIdResponse
*/
public Future<Void> delete(RoutingContext rc) {
final String id = rc.pathParam(ID_PARAMETER);
return bookService.delete(Integer.parseInt(id))
.onSuccess(success -> ResponseUtils.buildNoContentResponse(rc))
.onFailure(throwable -> ResponseUtils.buildErrorResponse(rc, throwable));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment