package lci.biz.springboot.resources;

import lci.biz.springboot.model.Calculation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tlanders on 5/3/2017.
 */
@RestController
@RequestMapping("/calculation")
public class CalculationController {
    private static final String PATTERN = "^-?+\\d+\\.?+\\d*$";

    @RequestMapping("/power")
    public Calculation pow(@RequestParam(value = "base") String b, @RequestParam(value = "exponent") String exp) {
        List<String> input = new ArrayList<>();
        input.add(b);
        input.add(exp);

        List<String> output = new ArrayList<>();
        if(b != null  && exp != null && b.matches(PATTERN) && exp.matches(PATTERN)) {
            output.add(String.valueOf(Math.pow(Double.valueOf(b), Double.valueOf(exp))));
        } else {
            output.add("base and/or exponent are not numeric");
        }
        return new Calculation("power", input, output);
    }

    @RequestMapping("/sqrt/{num}")
    public Calculation sqrt(@PathVariable(value = "num") String num) {
        List<String> input = new ArrayList<>();
        input.add(num);

        List<String> output = new ArrayList<>();
        if(num != null  && num.matches(PATTERN)) {
            try {
                output.add(String.valueOf(Math.sqrt(Double.valueOf(num))));
            } catch(Exception ex) {
                output.add("num is negative");
            }
        } else {
            output.add("num is not numeric");
        }
        return new Calculation("sqrt", input, output);
    }
}

@@ -128,207 +128,9 @@
 ion.
-PathVariable;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestParam;%0Aimport org.springframework.web.bind.annotation.RestController
+*
 ;%0A%0Ai
@@ -398,16 +398,52 @@
 ing(
-%22/power%22
+value = %22/power%22, method = RequestMethod.GET
 )%0A  
@@ -1049,16 +1049,24 @@
 Mapping(
+value = 
 %22/sqrt/%7B
@@ -1072,10 +1072,41 @@
 %7Bnum
-%7D%22
+:.+%7D%22, method = RequestMethod.GET
 )%0A  

fix to allow decimal points for sqrt values