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

Java tutorial

Introduction

Here is the source code for com.epam.ipodromproject.controller.BetsMadeController.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.User;
import com.epam.ipodromproject.service.BetService;
import com.epam.ipodromproject.service.MainService;
import com.epam.ipodromproject.service.UserService;
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 BetsMadeController {

    @Autowired
    MainService mainService;

    @Autowired
    UserService userService;

    @Autowired
    BetService betService;

    @RequestMapping(value = "getActiveBetsPage", method = RequestMethod.GET)
    public String getActiveBetsPage(@RequestParam(value = "page") Integer page,
            @RequestParam(value = "resultsPerPage") Integer resultsPerPage, Model model) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        int totalPages = (int) betService.getTotalActiveBetsPagesMadeByUser(username, resultsPerPage);
        int pageToShow;
        if ((page == null) || (page <= 0)) {
            pageToShow = 1;
        } else if (page > totalPages) {
            pageToShow = totalPages;
        } else {
            pageToShow = page;
        }
        model.addAttribute("betsMade", betService.getActiveBetsMadeByUser(username, pageToShow, resultsPerPage));
        return "lists/activeBetsDropList";
    }

    @RequestMapping(value = "getTotalActiveBetsPages", method = RequestMethod.GET)
    @ResponseBody
    public int getTotalActiveBetsPages(@RequestParam(value = "resultsPerPage") Integer resultsPerPage) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return (int) betService.getTotalActiveBetsPagesMadeByUser(username, resultsPerPage);
    }

    @RequestMapping(value = "cancelBet", method = RequestMethod.POST)
    @ResponseBody
    public String cancelBet(@RequestParam("betID") Long betID) {
        User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
        if (user.getEnabled() == false) {
            return "this_user_is_blocked";
        }
        if (mainService.cancelBet(betID, user)) {
            return "bet_successfully_cancelled";
        } else {
            return "could_not_cancel_bet";
        }
    }

    @RequestMapping(value = "activeBets", method = RequestMethod.GET)
    public String goToBets() {
        return "bets";
    }
}