Ejemplo n.º 1
0
        public ActionResult Calculation(string mathExpression)
        {
            // ::1 is also ok, when it's running on local machine (it's equal to 127.0.0.1)
            string userIpAddress = Request.UserHostAddress;

            //Create user or get it frod database if exists
            User user = _userRepo.GetUserByIpAddress(userIpAddress);

            if (user == null)
            {
                _userRepo.Create(new User {
                    IpAddress = userIpAddress
                });
                user = _userRepo.GetUserByIpAddress(userIpAddress);
            }

            //Form calculation, put it into the database
            try
            {
                Calculation userCalculation = mathExpression.ToCalculaton();
                userCalculation.CreationDate = DateTime.Now;
                userCalculation.UserId       = user.UserId;
                _calcRepo.Create(userCalculation);

                return(Json(userCalculation.ToCalculationDto(), JsonRequestBehavior.AllowGet));
            }
            catch (ArgumentException)
            {
                return(View("Cannot parse such expression. Please update the page and try again."));
            }
            catch (Exception)
            {
                return(View("Connection problems...Please update the page and try again."));
            }
        }