Implementing the Order Receipt End Point in Orga App
In the Open Event Orga App, I have implemented the Order Receipt endpoint with the help of which the organizer will be able to send the receipt of the ‘completed’ orders to the attendee via email. Initially the API was made in the server and then it was implemented in the the app.
Following steps were followed:
- Firstly a method named sendReceipt was made in the OrderDetailFragment as follows. We pass in the orderIdentifier string as a parameter.
private void sendReceipt() { orderDetailViewModel.sendReceipt(orderIdentifier); } |
- Now we implement 2 classes for OrderReceiptRequest and OrderReceiptResponse. The implementation is as follows. In the OrderReceiptRequest class we add just the orderIdentifier instance variable as the request involves just the order identifier parameter.
@Data @JsonNaming(PropertyNamingStrategy.KebabCaseStrategy.class) public class OrderReceiptRequest { public String orderIdentifier; } |
- Now we implement the OrderReceiptResponse class which will consist of 2 parameters message and error.
public class OrderReceiptResponse { public String message; public String error; } |
- In the OrderDetailsViewModel we add the following method. We create an object OrderReceipt where we pass the orderIdentifier. In the following statements we call the sendReceipts method of OrderRepositorry which takes in this OrderReceiptRequest as parameter.
public void sendReceipt(String orderIdentifier) { OrderReceiptRequest orderReceipt = new OrderReceiptRequest(); orderReceipt.setOrderIdentifier(orderIdentifier); compositeDisposable.add(orderRepository.sendReceipt(orderReceipt) .doOnSubscribe(disposable -> progress.setValue(true)) .doFinally(() -> progress.setValue(false)) .subscribe(() -> success.setValue(“Email Sent!”), throwable -> error.setValue(ErrorUtils.getMessage(throwable).toString()))); } |
- We then add the method sendReceipt in Order Repository which returns a Completable.
- Now we implement the sendReceipt methid in OrderRepositoryImpl as follows. First we check whether the repository is connected or not. If not then a network error message is sent.Then the sendReceiptEmail method present in the Orderapi class is called where we pass the orderReceiptRequest object. The next step will show the adding of the API for this particular end point.
@Override public Completable sendReceipt(OrderReceiptRequest orderReceiptRequest) { if (!repository.isConnected()) return Completable.error(new Throwable(Constants.NO_NETWORK)); return orderApi .sendReceiptEmail(orderReceiptRequest) .flatMapCompletable( var -> Completable.complete()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); } |
- Now in the OrdersAPI interface the following API call is written. We pass the OrderReceiptRequest in the body and the respinse is collected in the OrderReceiptRequest class and diplayed as the outcome.
@POST(“attendees/send-receipt”) Observable<OrderReceiptResponse> sendReceiptEmail(@Body OrderReceiptRequest orderReceiptRequest); |
- Certain UI changes also had to be done which are shown below.
<LinearLayout android:id=“@+id/emailReceiptLl” android:layout_width=“0dp” android:layout_height=“wrap_content” android:layout_weight=“1” android:layout_gravity=“center” android:gravity=“center” android:orientation=“vertical”> <Button android:id=“@+id/emailReceipt” android:layout_width=“30dp” android:layout_height=“30dp” android:background=“@drawable/ic_email” app:backgroundTint=“@color/materialcolorpicker__white” /> <TextView android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“@string/receipt” android:textAllCaps=“true” android:textSize=“10sp” android:textColor=“@color/materialcolorpicker__white”/> </LinearLayout> |
Resources
- Using Observables and Completables https://github.com/ReactiveX/RxJava/wiki/What’s-different-in-2.0
- Medium article on RxJava https://blog.aritraroy.in/the-missing-rxjava-2-guide-to-supercharge-your-android-development-part-1-624ef326bff4