示例#1
0
        /// <summary>
        /// Either adds the supplied <see cref="TaglistRegisteredUser"/> to <see cref="RegisteredUsers"/>,
        /// or replaces the existing <see cref="TaglistRegisteredUser"/>.
        /// </summary>
        /// <returns>true if <paramref name="ToRegister"/> was added to <see cref="RegisteredUsers"/>, false if <paramref name="ToRegister"/> replaced an existing entry</returns>
        /// <exception cref="AlreadyExistsException">
        /// A <see cref="TaglistRegisteredUser"/> with the specified <see cref="TaglistRegisteredUser.Username"/>
        /// already exists in <see cref="RegisteredUsers"/> with a different <see cref="TaglistRegisteredUser.ID"/>;
        /// in order to Register the new user, the username conflict must be resolved,
        /// such as by UnRegistering the old username.
        /// The exception message will contain further detail.
        /// </exception>
        public bool RegisterUser(TaglistRegisteredUser ToRegister)
        {
            if (RegisteredUsersByName.TryGetValue(ToRegister.Username, out TaglistRegisteredUser ExistingUserByName))
            {
                if (ToRegister.ID != ExistingUserByName.ID)
                {
                    throw new AlreadyExistsException(string.Format(
                                                         "The username '{0}' is already registered under a different user ID of {1:D}",
                                                         ToRegister.Username, ExistingUserByName.ID
                                                         ));
                }
            }
            string UsernameToRemove = ToRegister.Username;

            if (RegisteredUsersByID.TryGetValue(ToRegister.ID, out TaglistRegisteredUser ExistingUserByID))
            {
                if (!ToRegister.Username.Equals(ExistingUserByID.Username, StringComparison.Ordinal))
                {
                    UsernameToRemove = ExistingUserByID.Username;
                }
            }
            bool Added = !RegisteredUsersByID.Remove(ToRegister.ID);

            RegisteredUsersByName.Remove(UsernameToRemove);
            RegisteredUsersByID.Add(ToRegister.ID, ToRegister);

            /*
             * Should always succeed;
             * If ToRegister.Username already exists in RegisteredUsersByName, then ToRegister.ID must also be the same as the existing item
             * otherwise an AlreadyExistsException would have been thrown,
             * meaning that ToRegister.Username must be the same as the existing Username,
             * thus UsernameToRemove will equal ToRegister.Username
             */
            RegisteredUsersByName.Add(ToRegister.Username, ToRegister);
            return(Added);
        }
示例#2
0
 private void Remove(TaglistRegisteredUser ToRemove)
 {
     RegisteredUsersByID.Remove(ToRemove.ID);
     RegisteredUsersByName.Remove(ToRemove.Username);
 }
示例#3
0
 /// <summary>
 /// Returns the <see cref="TaglistRegisteredUser"/> in <see cref="RegisteredUsers"/> with the specified identifying <paramref name="UserID"/>.
 /// </summary>
 /// <returns>false if no <see cref="TaglistRegisteredUser"/> with the specified <paramref name="UserID"/> exists in <see cref="RegisteredUsers"/></returns>
 public bool TryGetRegisteredUser(int UserID, out TaglistRegisteredUser Result)
 {
     return(RegisteredUsersByID.TryGetValue(UserID, out Result));
 }
示例#4
0
 /// <summary>
 /// Returns the <see cref="TaglistRegisteredUser"/> in <see cref="RegisteredUsers"/> with the specified identifying <paramref name="Username"/>.
 /// </summary>
 /// <returns>false if no <see cref="TaglistRegisteredUser"/> with the specified <paramref name="Username"/> exists in <see cref="RegisteredUsers"/></returns>
 public bool TryGetRegisteredUser(string Username, out TaglistRegisteredUser Result)
 {
     return(RegisteredUsersByName.TryGetValue(Username, out Result));
 }