public IActionResult Registrate([FromBody] RegistrateViewModel model) { try { var token = userService.Registrate(model); return(Ok(new ResponceViewModel <TokenViewModel>(token))); } catch (Exception ex) { logger.LogError($"Exception thrown in Authorize registrate: {ex}"); return(BadRequest(new ResponceViewModel <string>($"Registrate failed:{ex.Message}"))); } }
public IActionResult Registrate(RegistrateViewModel registrateModel) { try { context.Registrate(registrateModel); return(OpenMessagePage(new MessageViewModel { Message = $"Registration success" })); } catch (Exception ex) { return(OpenMessagePage(new MessageViewModel { Message = $"Registration failed: {ex.Message}", IsError = true })); } }
public void Registrate(RegistrateViewModel registrateModel) { if (registrateModel.UserNumber <= 0) { throw new ArgumentException("Payer number"); } if (string.IsNullOrEmpty(registrateModel.UserPassword) || string.IsNullOrEmpty(registrateModel.UserConfirmPassword)) { throw new ArgumentException("Password or confirm password"); } if (registrateModel.UserPassword != registrateModel.UserConfirmPassword) { throw new ArgumentException("Passwords are not same"); } var targetRegion = localDb.Regions.FirstOrDefault(region => region.Id == registrateModel.UserRegionId); if (targetRegion == null) { throw new Exception("Region not found"); } var existUser = FindUser(registrateModel.UserNumber, registrateModel.UserRegionId); if (existUser != null) { throw new Exception("User already exist"); } var departments = localDb.Departments.Where(department => department.RegionId == targetRegion.Id); PayerState targetPayerState = null; Department targetDepartment = null; foreach (var department in departments) { var payerRepository = GetPayerRepository(department.Url, department.Path); targetPayerState = payerRepository.Get(registrateModel.UserNumber); if (targetPayerState != null) { targetDepartment = department; break; } } if (targetPayerState == null || targetDepartment == null) { throw new Exception($"Payer {registrateModel.UserNumber} in region {targetRegion.Name} is not found"); } var newUser = new User { Number = registrateModel.UserNumber, Name = targetPayerState.Name, DepartmentId = targetDepartment.Id }; newUser.PasswordHash = passwordHasher.HashPassword(newUser, registrateModel.UserPassword); localDb.Users.Add(newUser); localDb.SaveChanges(); }