package com.richashworth.planningpoker.controller;

import com.richashworth.planningpoker.model.Estimate;
import com.richashworth.planningpoker.service.SessionManager;
import com.richashworth.planningpoker.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

/**
 * Created by Rich Ashworth on 09/04/2016.
 */
@RestController
public class VoteController {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final SessionManager sessionManager;

    @Autowired
    public VoteController(SessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

    @RequestMapping(value = "vote", method = RequestMethod.POST)
    public void vote(
            @RequestParam(name = "sessionId") Long sessionId,
            @RequestParam(name = "userName") String userName,
            @RequestParam(name = "estimateValue") Double estimateValue
    ) {
        String estimateStr = String.valueOf(estimateValue).replaceAll(".0", "");
        logger.info(userName + " has voted " + estimateStr + " in session " + sessionId);
        if (!sessionManager.isSessionActive(sessionId)) {
            throw new IllegalArgumentException("Session not active");
        }
        Estimate estimate = new Estimate(StringUtils.formatUserName(userName), estimateValue);
        sessionManager.registerEstimate(sessionId, estimate);
    }
}


@@ -587,16 +587,49 @@
 oller;%0A%0A
+import java.text.DecimalFormat;%0A%0A
 /**%0A * C
@@ -718,16 +718,98 @@
 ller %7B%0A%0A
+    public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat(%22#.#%22);%0A%0A
     priv
@@ -1342,89 +1342,8 @@
 ) %7B%0A
-        String estimateStr = String.valueOf(estimateValue).replaceAll(%22.0%22, %22%22);%0A
     
@@ -1385,24 +1385,46 @@
 ted %22 + 
+DECIMAL_FORMAT.format(
 estimate
 Str + %22 
@@ -1415,19 +1415,22 @@
 estimate
-Str
+Value)
  + %22 in 

use DecimalFormat for formatting estimates