public int RegUser(string username, Credentials credentials, bool isTeacher, int regKey) { Ensure.String.IsNotNullOrWhiteSpace(username); Ensure.Any.IsNotNull(credentials); Ensure.Bool.IsFalse(_userRepository.GetAll().Any(u => u.Credentials.Email.Equals(credentials.Email)), nameof(RegUser), opt => opt.WithException(new UserAlreadyExistsException(credentials.Email))); var key = _keysRepository.GetKey(regKey); Ensure.Bool.IsTrue(key.UserEmail == credentials.Email, nameof(key.UserEmail), opt => opt.WithException(new InappropriateEmailException(key.UserEmail, credentials.Email))); CheckKey(key, KeyAppointment.BecomeAdmin, KeyAppointment.BecomeModerator); var userType = key.Appointment.Equals(KeyAppointment.BecomeAdmin) ? UserType.Admin : UserType.Moderator; var user = new User(username, credentials, isTeacher, userType, _userSettings.DefaultAvatar); _userRepository.Add(user); return(user.Id); }