public ListChannelsModel ListChannels(string username) { var user = this.context.Users.SingleOrDefault(p => p.Username == username); if (user == null) { return(null); } var channels = context.UserChannels.Where(p => p.UserId == user.Id) .Select(p => new ListChannelModel { Name = p.Channel.Name, Type = p.Channel.Type.ToString(), Followers = context.UserChannels.Count(k => k.ChannelId == p.Id), Id = p.ChannelId }) .ToArray(); var followerIds = this.context.UserChannels .Where(p => channels.Any(k => k.Id == p.ChannelId) && p.UserId != user.Id) .Select(p => p.UserId).ToArray(); var suggestedChannels = this.context.UserChannels .Where(p => followerIds.Contains(p.UserId) && !channels.Any(k => k.Id == p.ChannelId)) .Select(p => new ListChannelModel { Name = p.Channel.Name, Type = p.Channel.Type.ToString(), Followers = p.Channel.Followers.Count, Id = p.ChannelId }) .ToArray(); var seeOthers = this.context.Channels .Where(p => !suggestedChannels.Any(k => k.Id == p.Id) && !channels.Any(k => k.Id == p.Id)) .Select(p => new ListChannelModel { Name = p.Name, Type = p.Type.ToString(), Followers = p.Followers.Count(), Id = p.Id }) .ToArray(); var model = new ListChannelsModel { Following = channels, Suggested = suggestedChannels, SeeOther = seeOthers }; return(model); }
public ListChannelsModel ListNotFollowed(int userId) { var channels = this.context.Channels.Where(k => !k.Followers.Select(p => p.UserId).Contains(userId)) .Select(p => new ListChannelModel { Name = p.Name, Type = p.Type.ToString(), Followers = p.Followers.Count(), Id = p.Id }) .ToArray(); var model = new ListChannelsModel { Suggested = channels }; return(model); }