public static async Task<Response> CreateUserAsync(AddUserModel registerModel) { var response = new Response(); var context = new AdminDbContext(); ValidateUser(registerModel, response); if (!response.Success) { return response; } if (context.Users.Any(u => u.Email.Equals(registerModel.Email))) { response.AddMessage(false, "This email is already registered", ResponseMessageType.Warning); } else { var user = new User(); user.Email = registerModel.Email; user.Password = GenerateHashWithSalt(registerModel.Password, registerModel.Email); user.PasswordSalt = Path.GetRandomFileName(); user.RegisterDate = registerModel.RegisterDate; user.Active = registerModel.Active; context.Users.Add(user); await context.SaveChangesAsync(); } return response; }
public async Task<JsonResult> Edit(AddUserModel userModel) { var response = await UserManager.UpdateUserAsync(userModel); return Json(response); }
public static async Task<Response> UpdateUserAsync(AddUserModel registerModel) { var response = new Response(); ValidateUser(registerModel, response); if (!response.Success) { return response; } else { var context = new AdminDbContext(); var user = context.Users.Find(registerModel.Id); if (user != null) { user.Email = registerModel.Email; if (!string.IsNullOrEmpty(registerModel.Password)) { user.Password = GenerateHashWithSalt(registerModel.Password, registerModel.Email); } user.PasswordSalt = Path.GetRandomFileName(); user.RegisterDate = registerModel.RegisterDate; user.Active = registerModel.Active; await context.SaveChangesAsync(); } } return response; }
public static object GetUser(int id) { var context = new AdminDbContext(); var dbUser = context.Users.Find(id); if (dbUser != null) { var user = new AddUserModel(); user.Id = dbUser.Id; user.Email = dbUser.Email; user.RegisterDate = dbUser.RegisterDate; user.Active = dbUser.Active; return user; } return null; }
public static Response ValidateUser(AddUserModel registerModel, Response response = null) { response = response ?? new Response(); if (string.IsNullOrEmpty(registerModel.Email)) { response.AddMessage(false, "Email is mandatory", ResponseMessageType.Warning); } if (registerModel.Id == 0) { if (string.IsNullOrEmpty(registerModel.Password)) { response.AddMessage(false, "Password is mandatory", ResponseMessageType.Warning); } } return response; }