public async Task <ActionResult <UserViewModel> > Create(UserViewModel loginViewModel) { if (ModelState.IsValid) { if (await db.Users.AnyAsync(x => x.Email == loginViewModel.Login)) { return(BadRequest("Такой пользователь уже существует")); } logger.LogInformation($"Create user {loginViewModel.Login}:{loginViewModel.Password}"); var role = db.Roles.FirstOrDefault(x => x.Name == loginViewModel.Role); var lang = db.Languages.First(); var newUser = new User { Id = 0, Email = loginViewModel.Login, Nickname = loginViewModel.Login, Role = role, Language = lang, LanguageId = lang.Id, RoleId = role.Id, PasswordHash = CryptService.CreateHash(loginViewModel.Password) }; db.Add(newUser); db.SaveChanges(); return(Ok()); } return(loginViewModel); }
public async Task <LoginResult> RegisterAsync(RegisterModel registerModel) { var exists = authDataProvider.Users.FirstOrDefault(x => x.Email == registerModel.Email); if (exists != null) { return(null); } var newUser = new User { Email = registerModel.Email, Nickname = registerModel.Nickname, PasswordHash = cryptService.CreateHash(registerModel.Password), Role = authDataProvider.Roles.FirstOrDefault(x => x.Name == "User") }; authDataProvider.Languages.Load(); authDataProvider.Add(newUser); await authDataProvider.SaveChangesAsync(); var res = await GenerateTokenAndResult(newUser); return(res); }