Java tutorial
/* * 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.annotation.ActiveUser; import com.mtech.easyexchange.manager.exception.InsufficientFundsException; import com.mtech.easyexchange.manager.order.IOrderManager; import com.mtech.easyexchange.model.Currency; import com.mtech.easyexchange.model.Order; import com.mtech.easyexchange.model.User; import com.mtech.easyexchange.web.converter.CurrencyEnumConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller public class CreateOrderController { @Autowired protected IOrderManager orderManager; @InitBinder public void initBinder(WebDataBinder dataBinder) { dataBinder.registerCustomEditor(Currency.class, new CurrencyEnumConverter()); } @RequestMapping(value = "/exchange/create-order/{currencyOne}/{currencyTwo}", method = RequestMethod.POST) public String process(@ActiveUser User user, @ModelAttribute("order") Order order, @RequestParam("action") String action, @PathVariable Currency currencyOne, @PathVariable Currency currencyTwo, RedirectAttributes redirectAttributes) { if (action.equals("buy")) { order.setBuyCurrency(Currency.BTC); order.setSellCurrency(Currency.LTC); } else { order.setBuyCurrency(Currency.LTC); order.setSellCurrency(Currency.BTC); } order.setPlacedBy(user); try { orderManager.createOrder(order); redirectAttributes.addFlashAttribute("success", "You succesfully placed an order."); } catch (InsufficientFundsException e) { redirectAttributes.addFlashAttribute("error", "You do not have sufficient funds to place this order."); } return "redirect:/exchange/" + currencyOne.toString().toLowerCase() + "/" + currencyTwo.toString().toLowerCase(); } }