public ActionResult Sqrt([FromBody] SqrtModel number) { try { // Verifies if the model is valid, if not, returns a bad request response. // the verification is based on the attributes defined in the DTO. if (!ModelState.IsValid) { MicroserviceException badRequestResponse = new MicroserviceException() { ErrorCode = "InternalError", ErrorMessage = "Unable to process request:" + ModelState.Values, ErrorStatus = 400 }; return(BadRequest(badRequestResponse)); } else { // Finds the value of the header with the tracking id StringValues trackingId; Request.Headers.TryGetValue("X-Evi-Tracking-Id", out trackingId); var response = _calculatorService.Sqrt(number, trackingId); return(Ok(response)); } } // Catches and manages all of the server's errors and responds the client with a controlled object. catch (Exception err) { MicroserviceException internalServerErrorResponse = new MicroserviceException() { ErrorCode = "InternalError", ErrorMessage = "An unexpected error condition was triggered which made impossible to fulfill the request. Please try again or contact support" + err.InnerException, ErrorStatus = 500 }; Log.Error($"Calculator::Exception:: {internalServerErrorResponse}"); return(StatusCode(500, internalServerErrorResponse)); } }
/// <summary> /// Implementation of square root method. /// </summary> /// <param name="sqrt"></param> /// <param name="trackingId"></param> /// <returns></returns> public SqrtResponseModel Sqrt(SqrtModel sqrt, string trackingId) { //Finds square root of input number double square = Math.Sqrt(sqrt.Number); LogModel logModel = new LogModel() { Operation = "Sqrt", Calculation = $"sqrt({sqrt.Number}) = {square}", Date = DateTime.Now }; //if there is a tracking id, save operation in journal. if (!string.IsNullOrEmpty(trackingId)) { Journal.Add(new KeyValuePair <string, LogModel>(trackingId, logModel)); } // saves data in the log Log.Information($"Calculator::Sqrt:: {logModel}"); return(new SqrtResponseModel() { Square = square }); }