public async Task <ActionResult> EditAppUser(AdminModel adminModel, IFormFile Image) { string Id = HttpContext.Session.GetObjectFromJson <string>("UserId"); AppUser user = userManager.Users.Where(x => x.Id == Id).First(); user.PhoneNumber = adminModel.PhoneNumber; user.FirstName = adminModel.FirstName; user.SecondName = adminModel.SecondName; user.Birth = adminModel.Birthday.ToUniversalTime(); user.Adress = adminModel.Address; if (ModelState.IsValid) { if (adminModel.OldPassword != null) { IList <string> str = await userManager.GetRolesAsync(user); PasswordVerificationResult result = passwordHasher.VerifyHashedPassword(user, user.PasswordHash, adminModel.OldPassword); if (PasswordVerificationResult.Success == result) { if (Image != null) { if (Image.IsFilePhotoFormat()) { string fullPath = hostingEnvironment.GetFolder(); string format = Image.GetFileFormat(); string fileName = nameGenerator.GetFileName(format); string fullFilePath = Path.Combine(fullPath, fileName); await Image.SaveFileAsync(fullFilePath); user.Photo = fileName; ImageRemove.PhotoPathDelete(user.Photo, fullPath); if (adminModel.NewPassword == null) { ModelState.AddModelError("", "New Password empty"); } else { user.PasswordHash = passwordHasher.HashPassword(user, adminModel.NewPassword); dbContext.Update <AppUser>(user); await dbContext.SaveChangesAsync(); ModelState.AddModelError("", "Succes"); } } else { ModelState.AddModelError("", "Photo is not Format and format (jpg,png)"); } } else { if (adminModel.NewPassword == null) { ModelState.AddModelError("", "New Password empty"); } else { user.PasswordHash = passwordHasher.HashPassword(user, adminModel.NewPassword); dbContext.Update <AppUser>(user); await dbContext.SaveChangesAsync(); ModelState.AddModelError("", "Succes"); } } } else { ModelState.AddModelError("", "Old Password Incorrect"); } } else { IList <string> str = await userManager.GetRolesAsync(user); if (Image != null) { if (Image.IsFilePhotoFormat()) { string fullPath = hostingEnvironment.GetFolder(); string format = Image.GetFileFormat(); string fileName = nameGenerator.GetFileName(format); string fullFilePath = Path.Combine(fullPath, fileName); ImageRemove.PhotoPathDelete(user.Photo, fullPath); await Image.SaveFileAsync(fullFilePath); user.Photo = fileName; dbContext.Update <AppUser>(user); await dbContext.SaveChangesAsync(); ModelState.AddModelError("", "Succes"); } else { ModelState.AddModelError("", "Photo is not Format and format (jpg,png)"); } } else { dbContext.Update <AppUser>(user); await dbContext.SaveChangesAsync(); ModelState.AddModelError("", "Succes"); } } } return(View(adminModel)); }
public async Task <ActionResult> EditEmployee(Employee employee, IFormFile Photo) { if (ModelState.IsValid) { Employee data = dbContext.Employees.Where(x => x.Email == employee.Email).FirstOrDefault(); if (data != null) { if (Photo != null) { if (Photo.IsFilePhotoFormat()) { string fullPath = hostingEnvironment.GetFolder(); string format = Photo.GetFileFormat(); string fileName = nameGenerator.GetFileName(format); string fullFilePath = Path.Combine(fullPath, fileName); ImageRemove.PhotoPathDelete(data.Photo, fullPath); await Photo.SaveFileAsync(fullFilePath); data.Name = employee.Name; data.Surname = employee.Surname; data.FatherName = employee.FatherName; data.Gender = employee.Gender; data.IDCardExparyDate = employee.IDCardExparyDate; data.IDCardSerialNumber = employee.IDCardSerialNumber; data.MaritalStatus = employee.MaritalStatus; data.Nationally = employee.Nationally; data.Number = employee.Number; data.Photo = fileName; data.BirthDay = employee.BirthDay; data.DistrictRegistration = employee.DistrictRegistration; data.Email = employee.Email; data.PlasiyerCode = employee.PlasiyerCode; dbContext.Update <Employee>(data); await dbContext.SaveChangesAsync(); } else { ModelState.AddModelError("", "Photo is format (jpg,png)"); } } else { data.Name = employee.Name; data.Surname = employee.Surname; data.FatherName = employee.FatherName; data.Gender = employee.Gender; data.IDCardExparyDate = employee.IDCardExparyDate; data.IDCardSerialNumber = employee.IDCardSerialNumber; data.MaritalStatus = employee.MaritalStatus; data.Nationally = employee.Nationally; data.Number = employee.Number; data.BirthDay = employee.BirthDay; data.DistrictRegistration = employee.DistrictRegistration; data.Email = employee.Email; data.PlasiyerCode = employee.PlasiyerCode; dbContext.Update <Employee>(data); await dbContext.SaveChangesAsync(); } } else { ModelState.AddModelError("", "User is not exists"); } } return(View()); }