Example #1
0
        /// <summary>
        /// 添加给定的用户到给定的队伍中。
        /// </summary>
        /// <param name="team">队伍句柄。</param>
        /// <param name="user">用户句柄。</param>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="TeamNotFoundException"/>
        /// <exception cref="UserNotFoundException"/>
        public void AddUserToTeam(TeamHandle team, UserHandle user)
        {
            if (team == null)
            {
                throw new ArgumentNullException(nameof(team));
            }
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            m_factory.WithContext(context =>
            {
                TeamProfileEntity teamEntity = context.QueryTeamProfileEntity(team.TeamId);
                if (teamEntity == null)
                {
                    throw new TeamNotFoundException(team);
                }

                UserProfileEntity userEntity = context.QueryUserProfileEntity(user.Username);
                if (userEntity == null)
                {
                    throw new UserNotFoundException(user);
                }

                teamEntity.Members.Add(userEntity);
                context.SaveChanges();
            });
        }
Example #2
0
        /// <summary>
        /// 根据指定的查询参数查询用户句柄。
        /// </summary>
        /// <param name="query">查询参数。</param>
        /// <returns>一个包含了所有满足查询条件的用户句柄的查询结果对象。</returns>
        /// <exception cref="ArgumentNullException"/>
        public ReadOnlyCollection <UserHandle> QueryUsers(UserQueryParameter query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (query.QueryByOrganization && query.Organization == null)
            {
                throw new ArgumentNullException(nameof(query.Organization));
            }

            bool hasQuery = query.QueryByOrganization || query.QueryByUsergroup;

            if (!hasQuery)
            {
                // 没有查询参数。返回空列表。
                return(new ReadOnlyCollection <UserHandle>(new List <UserHandle>()));
            }
            else
            {
                return(m_factory.WithContext(context =>
                {
                    IQueryable <UserProfileEntity> profiles = context.GetAllUserProfiles();
                    if (query.QueryByOrganization)
                    {
                        profiles = UserDataContext.QueryUserProfileEntitiesByOrganization(profiles, query.Organization);
                    }
                    if (query.QueryBySex)
                    {
                        profiles = UserDataContext.QueryUserProfileEntitiesBySex(profiles, (NativeUserSex)query.Sex);
                    }
                    if (query.QueryByUsergroup)
                    {
                        profiles = UserDataContext.QueryUserProfileEntitiesByUsergroup(profiles,
                                                                                       (NativeUserGroup)query.UserGroup);
                    }

                    // 对查询结果进行排序以准备随时分页。
                    profiles.OrderBy(item => item.Username);

                    if (query.EnablePageQuery)
                    {
                        profiles = profiles.Page(query.PageQuery);
                    }

                    List <UserHandle> handles = new List <UserHandle>();
                    foreach (UserProfileEntity ent in profiles)
                    {
                        handles.Add(UserHandle.FromUserProfileEntity(ent));
                    }

                    return new ReadOnlyCollection <UserHandle>(handles);
                }));
            }
        }
Example #3
0
        /// <summary>
        /// 确定当前的用户句柄是否与另外一个对象等同。
        /// </summary>
        /// <param name="obj">要比较的对象。</param>
        /// <returns>一个值,该值指示当前的用户句柄是否与另外一个对象等同。</returns>
        public override bool Equals(object obj)
        {
            UserHandle another = obj as UserHandle;

            if (another == null)
            {
                return(false);
            }
            else
            {
                return(string.Compare(Username, another.Username, false) == 0);
            }
        }
        /// <summary>
        /// 使用指定的用户名查询用户。
        /// </summary>
        /// <param name="username">要查询的用户名。</param>
        /// <returns>查找到的用户的用户句柄。若未找到这样的用户,返回 null。</returns>
        /// <exception cref="ArgumentNullException"/>
        public UserHandle QueryUserByName(string username)
        {
            if (username == null)
            {
                throw new ArgumentNullException(nameof(username));
            }

            UserProfileEntity entity = m_context.QueryUserProfileEntity(username);

            if (entity == null)
            {
                return(null);
            }

            return(UserHandle.FromUserProfileEntity(entity));
        }
Example #5
0
        /// <summary>
        /// 确定给定的用户是否在当前的队伍中。
        /// </summary>
        /// <param name="user">用户句柄。</param>
        /// <returns>一个值,该值指示给定的用户是否在当前队伍中。</returns>
        public bool IsUserIn(UserHandle user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            foreach (UserHandle handle in GetMembers())
            {
                if (user == handle)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #6
0
        /// <summary>
        /// 从给定的队伍中移除给定的用户。
        /// </summary>
        /// <param name="team">队伍句柄。</param>
        /// <param name="user">用户句柄。</param>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="TeamNotFoundException"/>
        /// <exception cref="UserNotFoundException"/>
        public void RemoveUserFromTeam(TeamHandle team, UserHandle user)
        {
            if (team == null)
            {
                throw new ArgumentNullException(nameof(team));
            }
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            m_factory.WithContext(context =>
            {
                TeamProfileEntity teamEntity = context.QueryTeamProfileEntity(team.TeamId);
                if (teamEntity == null)
                {
                    throw new TeamNotFoundException(team);
                }

                UserProfileEntity userEntity = context.QueryUserProfileEntity(user.Username);
                if (userEntity == null)
                {
                    throw new UserNotFoundException(user);
                }

                foreach (UserProfileEntity teamUser in teamEntity.Members)
                {
                    if (string.Compare(teamUser.Username, userEntity.Username, false) == 0)
                    {
                        teamEntity.Members.Remove(teamUser);
                        break;
                    }
                }

                context.SaveChanges();
            });
        }
        /// <summary>
        /// 根据指定的查询参数查询用户句柄。
        /// </summary>
        /// <param name="query">查询参数。</param>
        /// <returns>一个列表,列表中包含了所有满足查询条件的用户句柄。</returns>
        /// <exception cref="ArgumentNullException"/>
        public IList <UserHandle> QueryUsers(UserQueryParameter query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (query.QueryByUsername && query.Username == null)
            {
                throw new ArgumentNullException(nameof(query.Username));
            }
            if (query.QueryByOrganization && query.Organization == null)
            {
                throw new ArgumentNullException(nameof(query.Organization));
            }

            if (query.QueryByUsername)
            {
                // 从用户名开始查找。
                // 由于满足条件的实体对象至多一个,做特殊化处理。
                UserProfileEntity profile = m_context.QueryUserProfileEntity(query.Username);
                if (profile == null)
                {
                    // 未找到符合要求的实体对象。
                    return(new List <UserHandle>());
                }
                else
                {
                    // 检查其他条件是否满足。
                    if (query.QueryByOrganization && string.Compare(query.Organization, profile.Organization, false) != 0)
                    {
                        return(new List <UserHandle>());
                    }

                    if (query.QueryByUsergroup && profile.UserGroup != (NativeUserGroup)query.UserGroup)
                    {
                        return(new List <UserHandle>());
                    }

                    // 唯一的用户信息实体对象满足条件。
                    return(new List <UserHandle>()
                    {
                        UserHandle.FromUserProfileEntity(profile)
                    });
                }
            }
            else    // query.QueryByUsername == false
            {
                bool hasQuery = query.QueryByOrganization || query.QueryByUsergroup;
                if (!hasQuery)
                {
                    // 没有查询参数。返回空列表。
                    return(new List <UserHandle>());
                }
                else
                {
                    IQueryable <UserProfileEntity> profiles = m_context.GetAllUserProfiles();
                    if (query.QueryByOrganization)
                    {
                        profiles = UserDataContext.QueryUserProfileEntitiesByOrganization(profiles, query.Organization);
                    }
                    if (query.QueryByUsergroup)
                    {
                        profiles = UserDataContext.QueryUserProfileEntitiesByUsergroup(profiles,
                                                                                       (NativeUserGroup)query.UserGroup);
                    }

                    List <UserHandle> handles = new List <UserHandle>();
                    foreach (UserProfileEntity entity in profiles)
                    {
                        handles.Add(UserHandle.FromUserProfileEntity(entity));
                    }

                    return(handles);
                }
            }
        }
Example #8
0
 /// <summary>
 /// 使用给定的用户名和异常消息创建 UserException 类的一个新实例。
 /// </summary>
 /// <param name="user">引发异常的用户句柄。</param>
 /// <param name="message">异常消息。</param>
 public UserException(UserHandle user, string message) : base(message)
 {
     UserHandle = user;
 }
Example #9
0
 /// <summary>
 /// 使用给定的用户名创建 UserException 类的一个新实例。
 /// </summary>
 /// <param name="user">引发异常的用户句柄。</param>
 public UserException(UserHandle user) : base($"BITOJ 用户 \"{user.Username}\" 引发了一个异常。")
 {
     UserHandle = user;
 }