com.epam.ipodromproject.controller.MakeABetControllec.java Source code

Java tutorial

Introduction

Here is the source code for com.epam.ipodromproject.controller.MakeABetControllec.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epam.ipodromproject.controller;

import com.epam.ipodromproject.domain.BetType;
import com.epam.ipodromproject.domain.Competition;
import com.epam.ipodromproject.domain.User;
import com.epam.ipodromproject.service.CompetitionService;
import com.epam.ipodromproject.service.HorseService;
import com.epam.ipodromproject.service.MainService;
import com.epam.ipodromproject.service.UserService;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Oleh
 */
@Controller
public class MakeABetControllec {

    @Autowired
    MainService mainService;

    @Autowired
    UserService userService;

    @Autowired
    CompetitionService competitionService;

    @Autowired
    HorseService horseService;

    @RequestMapping(value = "betting", method = RequestMethod.GET)
    public String goToMakeABet(Model model) {
        model.addAttribute("types", BetType.values());
        return "makeABet";
    }

    @RequestMapping(value = "makeABet", method = RequestMethod.POST)
    @ResponseBody
    public String tryToMakeABet(@RequestParam("competitionID") Long competitionID,
            @RequestParam("moneyToBet") String moneyToBet, @RequestParam("horseID") String horseID,
            @RequestParam("typeOfBet") String typeOfBet, Model model) {
        User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
        if (user.getEnabled() == false) {
            return "this_user_is_blocked";
        }
        Competition competitionToBet = competitionService.getCompetitionByID(competitionID);
        if (!mainService.makeBet(user, competitionToBet, moneyToBet, BetType.valueOf(typeOfBet), horseID)) {
            return "could_not_make_bet";
        } else {
            return "bet_was_successfully_made";
        }
    }

    @RequestMapping(value = "getCompetitionsToMakeABetPage", method = RequestMethod.GET)
    public String getCompetitionsToMakeByPage(@RequestParam("page") Integer page,
            @RequestParam("resultsPerPage") Integer resultsPerPage, Model model) {
        model.addAttribute("competitions",
                competitionService.getCompetitionsToMakeABetAfterDate(new Date(), page, resultsPerPage));
        model.addAttribute("types", BetType.values());
        model.addAttribute("firstNumber", (page - 1) * resultsPerPage);
        return "lists/competitionsToMakeABetDropList";
    }

}