com.mtech.easyexchange.mvc.exchange.ExchangeController.java Source code

Java tutorial

Introduction

Here is the source code for com.mtech.easyexchange.mvc.exchange.ExchangeController.java

Source

/*
 * This file is part of EasyExchange.
 *
 * (c) 2014 - Machiel Molenaar <machiel@machiel.me>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

package com.mtech.easyexchange.mvc.exchange;

import com.mtech.easyexchange.manager.order.IOrderManager;
import com.mtech.easyexchange.model.Currency;
import com.mtech.easyexchange.model.Order;
import com.mtech.easyexchange.web.converter.CurrencyEnumConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class ExchangeController {

    @Autowired
    protected IOrderManager orderManager;

    @InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Currency.class, new CurrencyEnumConverter());
    }

    @RequestMapping(value = "/exchange/{currencyOne}/{currencyTwo}", method = RequestMethod.GET)
    public String index(@PathVariable("currencyOne") Currency buyCurrency,
            @PathVariable("currencyTwo") Currency sellCurrency, ModelMap map) {

        map.addAttribute("currencyOne", buyCurrency);
        map.addAttribute("currencyTwo", sellCurrency);

        List<Order> buyOrders = orderManager.getBuyOrders(buyCurrency, sellCurrency);
        List<Order> sellOrders = orderManager.getSellOrders(buyCurrency, sellCurrency);

        map.addAttribute("buyOrders", buyOrders);
        map.addAttribute("sellOrders", sellOrders);

        return "exchange/index";
    }

}