/// <summary>
        /// Implementation of multiplication method.
        /// </summary>
        /// <param name="factors"></param>
        /// <param name="trackingId"></param>
        /// <returns></returns>
        public MultiplicationResponseModel Multiplication(MultiplicationModel factors, string trackingId)
        {
            //Calculates the product of the numbers in the array
            int product = 1;

            foreach (int i in factors.Factors)
            {
                product *= i;
            }

            LogModel logModel = new LogModel()
            {
                Operation   = "Mul",
                Calculation = $"{string.Join("*", factors.Factors)}",
                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::Mul:: {logModel}");

            return(new MultiplicationResponseModel()
            {
                Product = product
            });
        }
Esempio n. 2
0
        public IActionResult Index(MultiplicationModel model)
        {
            if (ModelState.IsValid)
            {
                var primes = _firstNPrimes.GetPrimes(model.InputNumber);
                var multiplicationTable = _multiplicationTable.GetTable(primes);
                ViewBag.InputNumber  = model.InputNumber;
                ViewBag.PrimeNumbers = primes;
                ViewBag.Table        = multiplicationTable;
            }

            return(View("Index", model));
        }
 public ActionResult Mult([FromBody] MultiplicationModel factors)
 {
     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.Multiplication(factors, 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));
     }
 }
 public double Get([FromBody] MultiplicationModel multiplicationModel)
 {
     return(multiplicationModel.X * multiplicationModel.Y);
 }