Exemplo n.º 1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "answers/trolleyTotal")]
            HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Exercise3 triggered.");

            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            var trolley = JsonConvert.DeserializeObject <Trolley>(requestBody);

            string isExpert = req.Query["isExpert"];

            log.LogInformation(JsonConvert.SerializeObject(trolley, Formatting.Indented));

            decimal total;

            if ((isExpert ?? "").Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                total = _trolleyServices.CalculateTotal(trolley);
            }
            else
            {
                total = await _trolleyServices.GetTotal(trolley);
            }


            return(new OkObjectResult(total));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Calculate([FromBody] Trolley trolley)
        {
            // validation and exception has not been implemented as this is only time with time contraints.
            // usually you would have at least 3 paths, model error, remote api error, unexpected error.
            decimal total = await _trolleySerivce.CalculateTotal(_token, trolley);

            return(Ok(total));
        }
            public Task <decimal> Handle(GetTrolleyTotalQuery request, CancellationToken cancellationToken)
            {
                var total = _trolleyService.CalculateTotal(
                    request.ProductPrices,
                    request.Specials,
                    request.ProductQuantities,
                    cancellationToken);

                return(total);
            }
Exemplo n.º 4
0
        public IActionResult Total([FromBody] TrolleyModel trolleyModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var trolleyModelJson = JsonConvert.SerializeObject(trolleyModel);

            System.Diagnostics.Trace.WriteLine(trolleyModelJson);
            logger.LogError(trolleyModelJson);

            try
            {
                var trolley = CreateTrolley(trolleyModel);
                var total   = trolleyService.CalculateTotal(trolley);
                return(Ok(total));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError, "Operation failed."));
            }
        }