示例#1
0
        public async Task <ActionResult <double> > Get([FromQuery] string expression,
                                                       [FromServices] HistoryService historyService, [FromServices] CalculatorService calculator)
        {
            string history = string.Empty;

            try
            {
                double result = calculator.Calculate(expression);
                history = $"{expression} = {result}";
                return(Ok(result));
            }
            catch (DivideByZeroException ex)
            {
                history = ex.Message;
                return(BadRequest(ex.Message));
            }
            catch (ArgumentException ex)
            {
                history = ex.Message;
                return(BadRequest($"Incorrect input, please, try again\n{expression}\n{ex.Message}"));
            }
            catch (Exception ex)
            {
                return(BadRequest($"Something went wrong, please, try again later\n{ex.Message}"));
            }
            finally
            {
                await historyService.Append(history);
            }
        }