Example #1
0
        public HttpResponseMessage Post([FromBody] DivisionRequest request)
        {
            var    jsonFormatter = new JsonMediaTypeFormatter();
            string trackingId    = null;

            if (Request.Headers.Contains("X-Evi-Tracking-Id"))
            {
                trackingId = Request.Headers.GetValues("X-Evi-Tracking-Id").First();
            }

            try
            {
                var response = new HttpResponseDto <DivisionResponse>
                {
                    Status  = HttpStatusCode.OK.ToString(),
                    Code    = (int)HttpStatusCode.OK,
                    Message = repository
                              .Divide(new int[] { request.Dividend, request.Divisor }, trackingId)
                };

                // In case of success, return a Json with the corresponding calculation total
                return(Request.CreateResponse(HttpStatusCode.OK, response, jsonFormatter));
            }
            catch (Exception ex)
            {
                var response = new HttpResponseDto <string>
                {
                    Status  = HttpStatusCode.BadRequest.ToString(),
                    Code    = (int)HttpStatusCode.BadRequest,
                    Message = ex.Message
                };

                // In case of error, return a Json with the corresponding message to be shown
                return(Request.CreateResponse(HttpStatusCode.BadRequest, response, jsonFormatter));
            }
        }