Exemplo n.º 1
0
        /// <summary>
        /// Implementation of substraction method.
        /// </summary>
        /// <param name="substraction"></param>
        /// <param name="trackingId"></param>
        /// <returns></returns>
        public SubstractionResponseModel Substraction(SubstractionModel substraction, string trackingId)
        {
            //calculates the difference between the inputs.
            int difference = substraction.Minuend - substraction.Subtrahend;

            LogModel logModel = new LogModel()
            {
                Operation   = "Sub",
                Calculation = $"{ substraction.Minuend} - {substraction.Subtrahend} = {difference}",
                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::Sub:: {logModel}");

            return(new SubstractionResponseModel()
            {
                Difference = difference
            });
        }
 public ActionResult Sub([FromBody] SubstractionModel substraction)
 {
     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.Substraction(substraction, 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));
     }
 }