public ActionResult Index(UserCalcViewModel model, FormCollection formCollection) { try { if (formCollection.Count >= 2) { model.Input = formCollection[1].ToString(); if (!string.IsNullOrEmpty(model.Input)) { model.IpAddress = Request.UserHostAddress; model = service.Caltulate(model).GetAwaiter().GetResult(); model.Input = string.Empty; } } if (formCollection.Count == 1) { var strId = formCollection.Keys[0].ToString(); var id = long.Parse(strId); model.IpAddress = Request.UserHostAddress; model.RepeatId = id; model = service.GetCalcById(model).GetAwaiter().GetResult(); } return(View(model)); } catch { model.Message = "Введенные данные не являются математической формулой"; model.IpAddress = Request.UserHostAddress; service.GetUserHistory(model).GetAwaiter().GetResult(); return(View(model)); } }
/// <summary> /// главная /// </summary> /// <returns></returns> public ActionResult Index() { var model = new UserCalcViewModel(); model.IpAddress = Request.UserHostAddress; model = service.GetUserHistory(model).GetAwaiter().GetResult(); return(View(model)); }
/// <summary> /// получить расчет по ид /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <UserCalcViewModel> GetCalcById(UserCalcViewModel model) { using (var db = new BaseContext()) { var calc = db.UserCalculations.FirstOrDefault(x => x.Id == model.RepeatId); model.Input = calc.Input; model = await GetUserHistory(model); return(model); } }
/// <summary> /// получить историю расчетов пользователя /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <UserCalcViewModel> GetUserHistory(UserCalcViewModel model) { using (var db = new BaseContext()) { var dateFrom = DateTime.UtcNow.Date; var dateTo = dateFrom.AddDays(1); model.Calculations = db.UserCalculations .Where(x => x.IpAddress == model.IpAddress && x.DateCreate >= dateFrom && x.DateCreate <= dateTo) .OrderByDescending(o => o.DateCreate) .ToList(); } return(model); }
/// <summary> /// произвести расчет /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <UserCalcViewModel> Caltulate(UserCalcViewModel model) { using (var db = new BaseContext()) { var input = model.Input.Replace(",", "."); var calResult = Calculation.Calculate(input).ToString(); var calc = new UserCalculation { IpAddress = model.IpAddress, Input = model.Input, Result = calResult, DateCreate = DateTime.UtcNow }; model.Result = calc.Result; db.UserCalculations.Add(calc); db.SaveChanges(); model = await GetUserHistory(model); } return(model); }