コード例 #1
0
        public JsonResult Calculate()
        {
            // Deserializing
            string json;

            using (var reader = new StreamReader(Request.InputStream))
            {
                json = reader.ReadToEnd();
            }
            var jsonObject = JsonConvert.DeserializeObject(json);

            // Generating
            var bodyGenerator = new BodyGenerator();
            var body          = bodyGenerator.GenerateBody(jsonObject);
            var id            = Guid.NewGuid();
            var calculator    = new BodyCalculator(id, body);
            var result        = calculator.GenerateBodyMasses();

            // Error calculation
            double? error     = null;
            decimal decError  = 0;
            bool    toCompare = false;

            if (body.Weight.HasValue)
            {
                error     = result.BodyMass.TotalMass - body.Weight;
                decError  = Math.Round(decimal.Divide((decimal)error, (decimal)body.Weight) * 100, 2);
                toCompare = true;
            }

            // Recording
            var user = User.Identity.IsAuthenticated ? User.Identity.GetUserName() : string.Empty;

            this.bodyRecorder.RecordBody(body, result, decError, toCompare, user, User.Identity.IsAuthenticated);

            // Send to clients
            var jsonResult = string.Empty;

            if ((toCompare && decError <= 5) || !toCompare)
            {
                // Serializing
                jsonResult = JsonConvert.SerializeObject(result.BodyMass);
            }
            else
            {
                // Error message
                jsonResult = "Error";
            }

            // Returning to client
            return(Json(jsonResult, JsonRequestBehavior.AllowGet));
        }