/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.grpc;

import bisq.core.payment.PaymentAccount;

import bisq.proto.grpc.CreatePaymentAccountReply;
import bisq.proto.grpc.CreatePaymentAccountRequest;
import bisq.proto.grpc.GetPaymentAccountsReply;
import bisq.proto.grpc.GetPaymentAccountsRequest;
import bisq.proto.grpc.PaymentAccountsGrpc;

import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;

import javax.inject.Inject;

import java.util.stream.Collectors;


public class GrpcPaymentAccountsService extends PaymentAccountsGrpc.PaymentAccountsImplBase {

    private final CoreApi coreApi;

    @Inject
    public GrpcPaymentAccountsService(CoreApi coreApi) {
        this.coreApi = coreApi;
    }

    @Override
    public void createPaymentAccount(CreatePaymentAccountRequest req,
                                     StreamObserver<CreatePaymentAccountReply> responseObserver) {
        try {
            coreApi.createPaymentAccount(req.getAccountName(), req.getAccountNumber(), req.getFiatCurrencyCode());
            var reply = CreatePaymentAccountReply.newBuilder().build();
            responseObserver.onNext(reply);
            responseObserver.onCompleted();
        } catch (Exception cause) {
            var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));
            responseObserver.onError(ex);
            throw ex;
        }
    }

    @Override
    public void getPaymentAccounts(GetPaymentAccountsRequest req,
                                   StreamObserver<GetPaymentAccountsReply> responseObserver) {
        try {
            var tradeStatistics = coreApi.getPaymentAccounts().stream()
                    .map(PaymentAccount::toProtoMessage)
                    .collect(Collectors.toList());
            var reply = GetPaymentAccountsReply.newBuilder().addAllPaymentAccounts(tradeStatistics).build();
            responseObserver.onNext(reply);
            responseObserver.onCompleted();
        } catch (Exception cause) {
            var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));
            responseObserver.onError(ex);
            throw ex;
        }
    }
}

@@ -991,70 +991,8 @@
 c;%0A%0A
-import io.grpc.Status;%0Aimport io.grpc.StatusRuntimeException;%0A
 impo
@@ -1513,34 +1513,16 @@
 rver) %7B%0A
-        try %7B%0A    
         
@@ -1624,36 +1624,32 @@
 ode());%0A        
-    
 var reply = Crea
@@ -1692,36 +1692,32 @@
 uild();%0A        
-  
-  
 responseObserver
@@ -1724,36 +1724,32 @@
 .onNext(reply);%0A
-    
         response
@@ -1776,219 +1776,8 @@
 ();%0A
-        %7D catch (Exception cause) %7B%0A            var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));%0A            responseObserver.onError(ex);%0A            throw ex;%0A        %7D%0A
     
@@ -1958,26 +1958,8 @@
 ) %7B%0A
-        try %7B%0A    
     
@@ -2038,20 +2038,16 @@
         
-  
-  
 .map(Pay
@@ -2091,20 +2091,16 @@
         
-    
 .collect
@@ -2126,28 +2126,24 @@
 ));%0A        
-  
-  
 var reply = 
@@ -2227,36 +2227,32 @@
 uild();%0A        
-    
 responseObserver
@@ -2267,36 +2267,32 @@
 reply);%0A        
- 
-   
 responseObserver
@@ -2311,219 +2311,8 @@
 ();%0A
-        %7D catch (Exception cause) %7B%0A            var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));%0A            responseObserver.onError(ex);%0A            throw ex;%0A        %7D%0A
     

Remove try catch block