public double Addition(CalculatorViewModel model)
 {
     model.Operation = "Addition";
     model.Result    = model.Value1 + model.Value2;
     _context.CalculatorViewModels.Add(model);
     _context.SaveChanges();
     return(model.Result);
 }
Esempio n. 2
0
        public IActionResult Index(FormData data)
        {
            if (string.IsNullOrEmpty(data.Expression) == false && data.TypeButton == "Расчитать")
            {
                var note = new Note();
                note.Expression = data.Expression;
                note.Id         = Guid.NewGuid();

                int errorCode;
                var calc = new Calculator();
                note.Result    = calc.Evaluate(note.Expression, out errorCode);
                note.ErrorCode = errorCode;

                if (note.Result == null)
                {
                    ViewData["Result"] = "Неудалось подсчтитать ответ.";
                }
                else
                {
                    ViewData["Result"] = "Ответ равен: " + note.Result.ToString();
                }

                note.DateAndTime = DateTime.Now;
                note.HostName    = Request.Host.ToString();

                _db.Notes.Add(note);
                _db.SaveChanges();
            }

            var notes = _db.Notes.OrderByDescending(c => c.DateAndTime).AsQueryable();

            if (string.IsNullOrEmpty(data.ExpressionFilter) == false)
            {
                notes = notes.Where(note => EF.Functions.Like(note.Expression, "%" + data.ExpressionFilter + "%"));
            }

            if (string.IsNullOrEmpty(data.HostFilter) == false)
            {
                notes = notes.Where(note => EF.Functions.Like(note.HostName, "%" + data.HostFilter + "%"));
            }

            var historyList = new List <History>();

            foreach (var note in notes)
            {
                string message;
                switch (note.ErrorCode)
                {
                case 0:
                    message = note.Result.ToString();
                    break;

                case 1:
                    message = "Вы ввели неизвестную операцию.";
                    break;

                case 2:
                    message = "Неверный формат строки.";
                    break;

                case 3:
                    message = "Неверное соотношение цифр и арифметических операций.";
                    break;

                default:
                    message = "Неизвестная ошибка.";
                    break;
                }

                historyList.Add(new History(note.Expression, message, note.HostName));
            }

            ViewBag.History = historyList;
            return(View());
        }