public NewCalculation GetCalculationData(ClientsData.NewClientData client, int userType) { NewCalculation x = new NewCalculation(); x.bmi = Bmi(client); x.bmiPercentile = GetBmiPercentile(client.agemos, client.gender.value, client.percentileSrc); client.bmiPercentile = x.bmiPercentile; x.whr = Whr(client); x.waist = Waist(client); x.bmr = Bmr(client); x.tee = Tee(client); x.recommendedEnergyIntake = RecommendedEnergyIntake(client); x.recommendedEnergyExpenditure = RecommendedEnergyExpenditure(client); x.recommendedWeight = RecommendedWeight(client); x.goal = RecommendedGoal(client); x.bmrEquations = GetBmrEquations(userType); BodyFat BF = new BodyFat(); x.bodyFat = BF.GetBodyFat(client); x.percentileSrc = GetPercentileSrc(); x.myCalculation = GetMyCalculation(client); return(x); }
public IHttpActionResult Post(BodyFat value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (value.source == null || value.humanId == null) { return(BadRequest()); } using (var dbContextTransaction = db.Database.BeginTransaction()) { try { tSourceService sourceServiceObj = db.tSourceServices .SingleOrDefault(x => x.ServiceName == value.source && x.SourceID == 5); if (sourceServiceObj == null) { sourceServiceObj = new tSourceService(); sourceServiceObj.ServiceName = value.source; sourceServiceObj.TypeID = 2; //Wellness sourceServiceObj.SourceID = 5; //HumanAPI db.tSourceServices.Add(sourceServiceObj); } tUserSourceService userSourceServiceObj = null; //Get credentials tCredential credentialObj = db.tCredentials.SingleOrDefault(x => x.SourceID == 5 && x.SourceUserID == value.humanId && x.SystemStatusID == 1); if (credentialObj == null) { throw new NoUserCredentialsException("Unable to find any matching HAPI user credentials"); } else { userSourceServiceObj = db.tUserSourceServices.SingleOrDefault( x => x.SourceServiceID == sourceServiceObj.ID && x.CredentialID == credentialObj.ID && x.SystemStatusID == 1); if (userSourceServiceObj == null) { userSourceServiceObj = new tUserSourceService(); userSourceServiceObj.SourceServiceID = sourceServiceObj.ID; userSourceServiceObj.UserID = credentialObj.UserID; userSourceServiceObj.CredentialID = credentialObj.ID; userSourceServiceObj.ConnectedOnDateTime = DateTime.Now; userSourceServiceObj.LastSyncDateTime = DateTime.Now; userSourceServiceObj.LatestDateTime = value.updatedAt; userSourceServiceObj.StatusID = 3; //connected userSourceServiceObj.SystemStatusID = 1; //valid userSourceServiceObj.tCredential = credentialObj; db.tUserSourceServices.Add(userSourceServiceObj); } else { //update LatestDateTime to the most recent datetime if (userSourceServiceObj.LatestDateTime == null || userSourceServiceObj.LatestDateTime < value.updatedAt) { userSourceServiceObj.LatestDateTime = value.updatedAt; } } } tUserVital userVitals = null; userVitals = db.tUserVitals .SingleOrDefault(x => x.SourceObjectID == value.id); if (userVitals == null) { //insert userVitals = new tUserVital(); userVitals.SourceObjectID = value.id; userVitals.UserID = credentialObj.UserID; userVitals.tUserSourceService = userSourceServiceObj; userVitals.UserSourceServiceID = userSourceServiceObj.ID; userVitals.SystemStatusID = 1; userVitals.Name = "Heart Rate"; userVitals.Value = value.value; //Dates DateTimeOffset dtoStart; if (RESTfulBAL.Models.DynamoDB.Utilities.ConvertToDateTimeOffset(value.timestamp, value.tzOffset, out dtoStart)) { userVitals.ResultDateTime = dtoStart; } else { userVitals.ResultDateTime = value.timestamp; } //UOM if (value.unit != null) { tUnitsOfMeasure uom = null; uom = db.tUnitsOfMeasures.SingleOrDefault(x => x.UnitOfMeasure == value.unit); if (uom == null) { uom = new tUnitsOfMeasure(); uom.UnitOfMeasure = value.unit; db.tUnitsOfMeasures.Add(uom); } userVitals.tUnitsOfMeasure = uom; userVitals.UOMID = uom.ID; } db.tUserVitals.Add(userVitals); } else { //update //Dates DateTimeOffset dtoStart; if (RESTfulBAL.Models.DynamoDB.Utilities.ConvertToDateTimeOffset(value.timestamp, value.tzOffset, out dtoStart)) { userVitals.ResultDateTime = dtoStart; } else { userVitals.ResultDateTime = value.timestamp; } userVitals.Value = value.value; //UOM if (value.unit != null) { tUnitsOfMeasure uom = null; uom = db.tUnitsOfMeasures.SingleOrDefault(x => x.UnitOfMeasure == value.unit); if (uom == null) { uom = new tUnitsOfMeasure(); uom.UnitOfMeasure = value.unit; db.tUnitsOfMeasures.Add(uom); } if (!uom.UnitOfMeasure.Equals(value.unit)) { userVitals.tUnitsOfMeasure = uom; userVitals.UOMID = uom.ID; } } userVitals.LastUpdatedDateTime = DateTime.Now; userVitals.tUserSourceService = userSourceServiceObj; } db.SaveChanges(); dbContextTransaction.Commit(); return(Ok(userVitals)); } catch (Exception ex) { dbContextTransaction.Rollback(); //Insert Error Log tUserDataErrLog userErrorLog = new tUserDataErrLog(); userErrorLog.ErrTypeID = (int)ErrorLogging.enumErrorType.Application; userErrorLog.ErrSourceID = (int)AuditLogging.enumApplication.SFCBAL; userErrorLog.Code = ex.HResult.ToString(); userErrorLog.Description = ex.Message; userErrorLog.Trace = ex.StackTrace; dbErr.tUserDataErrLogs.Add(userErrorLog); dbErr.SaveChanges(); string ErrMsg = "An error occured and we have logged the error. Please try again later."; Exception Err = new Exception(ErrMsg, ex); return(InternalServerError(Err)); } } }
/********* https://completehumanperformance.com/2013/10/08/calorie-needs/ ************/ public double Bmr(ClientsData.NewClientData x) { double BMR = 0; string type = x.bmrEquation; if (type == HarrisBenedicts) { /***** The original Harris–Benedict equations published in 1918 and 1919 *****/ if (x.gender.value == 0) { BMR = 66.5 + 13.75 * x.weight + 5.003 * x.height - 6.755 * x.age; // Men } else { BMR = 655.1 + 9.563 * x.weight + 1.85 * x.height - 4.676 * x.age; // Women } } else if (type == HarrisBenedictsRozaAndShizgal) { /***** The Harris–Benedict equations revised by Roza and Shizgal in 1984 *****/ //Men BMR = 88.362 + (13.397 × weight in kg) +(4.799 × height in cm) -(5.677 × age in years) //Women BMR = 447.593 + (9.247 × weight in kg) +(3.098 × height in cm) -(4.330 × age in years) if (x.gender.value == 0) { BMR = 88.362 + (13.397 * x.weight) + (4.799 * x.height) - (5.677 * x.age); // Men } else { BMR = 447.593 + (9.247 * x.weight) + (3.098 * x.height) - (4.330 * x.age); // Women } } else if (type == MifflinStJeor) { //BMR (Men) = (10 × weight in kg) +(6.25 × height in cm) − (5 × age in years) +5 //BMR (Women) = (10 × weight in kg) + (6.25 × height in cm) − (5 × age in years) − 161 int a = x.gender.value == 0 ? 5 : -161; BMR = 10 * x.weight + 6.25 * x.height - 5 * x.age + a; } else if (type == KatchMcArdle) { //TODO: // Katch-Mcardle BMR Formula: //BMR = 370 + (21.6 x Lean Body Mass(kg) ) //Lean Body Mass = (Weight(kg) x(100-(Body Fat)))/100 BodyFat bf = new BodyFat(); BMR = 370 + 21.6 * bf.GetBodyFat(x).lbm; } else if (type == Cunningham) { //TODO: /****** Cunninghams = 500 + 22(lean body mass[LBM] in kg) ******/ } else if (type == Owen) { //Men: RMR = 879 + 10.2 X weight //Women: RMR = 795 + 7.18 X weight if (x.gender.value == 0) { BMR = 879 + 10.2 * x.weight; // Men } else { BMR = 795 + 7.18 * x.weight; // Women } } else { /****** DEFAULT: Mifflin - St.Jeor = 5 + 10(weight in kg) + 6.25(height in cm) − 5(age) ******/ int a = x.gender.value == 0 ? 5 : -161; BMR = 10 * x.weight + 6.25 * x.height - 5 * x.age + a; } return(BMR); }