Created
December 16, 2019 01:01
-
-
Save IntegerMan/1f5bfe536ddfa79d5823c7e615f723af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpGet] | |
[Route("add/{x}/{y}")] | |
public ObjectResult Add(string x, string y) | |
{ | |
try | |
{ | |
ValidateInputs(x, y); | |
var xVal = decimal.Parse(x); | |
var yVal = decimal.Parse(y); | |
return Ok(xVal + yVal); | |
} | |
catch (ValidationException ex) | |
{ | |
return BadRequest(ex.Message); | |
} | |
} | |
private void ValidateInputs(string x, string y) | |
{ | |
if (!decimal.TryParse(x, out _)) | |
{ | |
throw new ValidationException($"Cannot parse '{x}' to a number"); | |
} | |
if (!decimal.TryParse(y, out _)) | |
{ | |
throw new ValidationException($"Cannot parse '{y}' to a number"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment