public async Task <JsonResult> CreateCalf(MeasurementViewModel viewModel) { var user = await userManager.FindByIdAsync(User.Identity.GetUserId()); if (user == null) { return(Json(HttpStatusCode.Unauthorized.ToString())); } if (ModelState.IsValid) { var measurement = new CalfMeasurement { MeasurementDate = viewModel.MeasurementDate, Value = Convert.ToDouble(viewModel.Value) }; var oldMeasurement = calfMeasurementService.Find(m => m.ApplicationUser.Id == user.Id && m.MeasurementDate == measurement.MeasurementDate).ToList(); if (oldMeasurement.Count == 0) { measurement.ApplicationUser = user; calfMeasurementService.Create(measurement); return(Json("Created successfully")); } else { oldMeasurement.First().Value = measurement.Value; calfMeasurementService.Update(oldMeasurement.First()); return(Json("Updated successfully")); } } string errors = ""; foreach (var modelState in ModelState.Values) { foreach (var error in modelState.Errors) { errors += error.ErrorMessage + ". "; } } // error return(Json(errors)); }
private void SeedMeasurements(ApplicationDbContext context) { var userStore = new UserStore <ApplicationUser>(context); var userManager = new ApplicationUserManager(userStore); var weightMeasurementService = new WeightMeasurementService(context); var heightMeasurementService = new HeightMeasurementService(context); var upperArmMeasurementService = new UpperArmMeasurementService(context); var foreArmMeasurementService = new ForeArmMeasurementService(context); var neckMeasurementService = new NeckMeasurementService(context); var chestMeasurementService = new ChestMeasurementService(context); var waistMeasurementService = new WaistMeasurementService(context); var hipsMeasurementService = new HipsMeasurementService(context); var thighMeasurementService = new ThighMeasurementService(context); var calfMeasurementService = new CalfMeasurementService(context); // adding Measurements var weightMeasurements = new List <WeightMeasurement>(InitialData.WeightMeasurement); var heightMeasurements = new List <HeightMeasurement>(InitialData.HeightMeasurement); var upperArmMeasurements = new List <UpperArmMeasurement>(InitialData.UpperArmMeasurement); var foreArmMeasurements = new List <ForeArmMeasurement>(InitialData.ForeArmMeasurement); var neckMeasurements = new List <NeckMeasurement>(InitialData.NeckMeasurement); var chestMeasurements = new List <ChestMeasurement>(InitialData.ChestMeasurement); var waistMeasurements = new List <WaistMeasurement>(InitialData.WaistMeasurement); var hipsMeasurements = new List <HipsMeasurement>(InitialData.HipsMeasurement); var thighMeasurements = new List <ThighMeasurement>(InitialData.ThighMeasurement); var calfMeasurements = new List <CalfMeasurement>(InitialData.CalfMeasurement); var applicationUsers = new List <ApplicationUser>(userManager.Users); foreach (var u in applicationUsers) { foreach (var m in weightMeasurements) { m.ApplicationUser = u; // bind to user weightMeasurementService.Create(m); // add to database } foreach (var m in heightMeasurements) { m.ApplicationUser = u; heightMeasurementService.Create(m); } foreach (var m in upperArmMeasurements) { m.ApplicationUser = u; upperArmMeasurementService.Create(m); } foreach (var m in foreArmMeasurements) { m.ApplicationUser = u; foreArmMeasurementService.Create(m); } foreach (var m in neckMeasurements) { m.ApplicationUser = u; neckMeasurementService.Create(m); } foreach (var m in chestMeasurements) { m.ApplicationUser = u; chestMeasurementService.Create(m); } foreach (var m in waistMeasurements) { m.ApplicationUser = u; waistMeasurementService.Create(m); } foreach (var m in hipsMeasurements) { m.ApplicationUser = u; hipsMeasurementService.Create(m); } foreach (var m in thighMeasurements) { m.ApplicationUser = u; thighMeasurementService.Create(m); } foreach (var m in calfMeasurements) { m.ApplicationUser = u; calfMeasurementService.Create(m); } } }