public async Task <IActionResult> Create(Profile obj, string roleName, int tariffId) { await ServerSideValidation(obj, roleName, tariffId); if (ModelState.IsValid) { obj.Tariff = await DbContext.Tariffs .FirstOrDefaultAsync(t => t.Id.Equals(tariffId)); var result = await UserMngr.CreateAsync(obj.User); if (result.Succeeded) { var role = await RoleMngr.FindByNameAsync(roleName); await UserMngr.AddToRoleAsync(obj.User, role.Name); } await DbContext.Profiles.AddAsync(obj); await DbContext.SaveChangesAsync(); return(RedirectToAction("Index")); } return(await AddUpdateForm()); }
public async Task <IActionResult> Update(Profile obj, string roleName, int tariffId) { await ServerSideValidation(obj, roleName, tariffId); if (ModelState.IsValid) { var accountExisting = await DbContext.Users .FirstOrDefaultAsync(u => u.Id.Equals(obj.User.Id)); var profileExisting = await DbContext.Profiles.AsNoTracking() .FirstOrDefaultAsync(p => p.Id.Equals(obj.Id)); accountExisting.UserName = obj.User.UserName; accountExisting.Email = obj.User.Email; obj.User = accountExisting; obj.Tariff = await DbContext.Tariffs .FirstOrDefaultAsync(t => t.Id.Equals(tariffId)); var role = await RoleMngr.FindByNameAsync(roleName); await UserMngr.RemoveFromRoleAsync(obj.User, role.Name); await UserMngr.AddToRoleAsync(obj.User, role.Name); await Task.Run(() => { if (profileExisting.IsEnabled != obj.IsEnabled) { _tariffRegulator.StartToUseOfTariff(obj); } obj.DateOfUpdate = DateTime.Now; DbContext.Update(obj); DbContext.SaveChanges(); }); return(RedirectToAction("Index")); } return(await AddUpdateForm(obj.Id)); }