zarg.bank.rest.web.BankRestController.java Source code

Java tutorial

Introduction

Here is the source code for zarg.bank.rest.web.BankRestController.java

Source

/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package zarg.bank.rest.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import zarg.bank.domain.Customer;
import zarg.bank.services.CustomerManager;
import zarg.bank.services.TellerService;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class BankRestController {

    @Autowired
    private TellerService tellerService;

    @Autowired
    private CustomerManager customerManager;

    @RequestMapping(value = "/register", method = RequestMethod.PUT)
    public Customer register(@RequestBody RegisterRequest request) {
        Customer customer = customerManager.registerCustomer(request.getEmail(), request.getPassword());
        return customer;
    }

    @RequestMapping(value = "/teller/deposit", method = RequestMethod.POST)
    @CountCalls
    public Callable<BalanceResponse> deposit(@RequestBody final TellerRequest request) {
        return () -> {
            Future<Integer> future = tellerService.deposit(request.getAccountId(), request.getAmount());
            return new BalanceResponse(request.getAccountId(), future.get());
        };
    }

    @RequestMapping(value = "/teller/withdraw", method = RequestMethod.POST)
    @CountCalls
    public Callable<BalanceResponse> withdraw(@RequestBody final TellerRequest request) {
        return () -> {
            Future<Integer> future = tellerService.withdraw(request.getAccountId(), request.getCustomerId(),
                    request.getAmount());
            return new BalanceResponse(request.getAccountId(), future.get());
        };
    }

    @RequestMapping("/transfer")
    public String transfer() {
        return "";
    }

    @RequestMapping("/balance")
    public String balance() {
        return "";
    }

    @ExceptionHandler(ExecutionException.class)
    public ResponseEntity<String> handleIOException(final ExecutionException ex) {
        Throwable cause = ex;
        while (cause.getCause() != null) {
            cause = cause.getCause();
        }
        return new ResponseEntity<>("{\"error\": \"" + cause.getMessage() + "\"}", HttpStatus.BAD_REQUEST);
    }
}