예제 #1
0
        public IEnumerable <channel_connection> GetConnectionsWhereUserNotCreator(IDbConnection connection, int userId, byte channelTypeId)
        {
            ThrowIfConnectionIsNull(connection);
            var channelTableName = _provider.GetTableName(nameof(channel));
            var ch    = ChannelExtensions.SqlAliaceChannel;
            var chCon = ChannelExtensions.SqlAliaceChannelConnection;
            var sql   = $"SELECT {ChannelExtensions.SqlSelectFieldsChannelAndChannelConnection} " +
                        $"FROM  {SchemeTableName} as {chCon} " +
                        $"JOIN {channelTableName} as {ch} ON {ch}.Id={chCon}.channelId AND {ch}.creatorId!={userId} " +
                        $"WHERE {chCon}.userId={userId} AND {chCon}.channelType={channelTypeId}";


            var converted = ChannelExtensions.ConvertRowsChannelWhithConnectedChannel(_provider.Text <dynamic>(connection, sql));

            if (!converted.Any())
            {
                return(null);
            }
            var combinded = ChannelExtensions.CombineChannelConnections(converted);

            if (!combinded.Any())
            {
                return(null);
            }
            if (combinded.Count == 1)
            {
                var channel = combinded[0];
                return(channel.HasConnections() ? channel.GetConnections() : null);
            }
            else
            {
                throw new NotImplementedException("combinded.Count !=1");
            }
        }
        public channel GetChannelWithConnectedUsers(IDbConnection connection, int channelId, List <int> conectedUserIds)
        {
            var channelConnectedTableName = _provider.GetTableName(nameof(channel_connection));
            var ch    = ChannelExtensions.SqlAliaceChannel;
            var chCon = ChannelExtensions.SqlAliaceChannelConnection;
            var sql   =
                $"SELECT {ChannelExtensions.SqlSelectFieldsChannelAndChannelConnection} FROM {SchemeTableName} as {ch} " +
                $"JOIN {channelConnectedTableName} AS {chCon} ON {chCon}.channelId={ch}.Id  " +
                $"WHERE {ch}.Id={channelId} ";

            var whereConected = "";

            if (conectedUserIds.Any())
            {
                whereConected = $" AND {chCon}.userId IN(" + conectedUserIds.Aggregate(whereConected, (current, uId) => current + (uId + ","));
                whereConected = whereConected.RemoveLastSimbol() + ") ";
                sql          += whereConected;
            }

            var data = (IList <dynamic>)_provider.Text <dynamic>(connection, sql);

            if (!data.Any())
            {
                return(null);
            }
            var converted = ChannelExtensions.ConvertRowsChannelWhithConnectedChannel(_provider.Text <dynamic>(connection, sql));

            if (!converted.Any())
            {
                return(null);
            }
            var combinded = ChannelExtensions.CombineChannelConnections(converted);

            if (!combinded.Any())
            {
                return(null);
            }
            if (combinded.Count == 1)
            {
                var channel = combinded[0];
                return(channel);
            }
            else
            {
                throw new NotImplementedException("combinded.Count !=1");
            }
        }