Skip to content

Commit

Permalink
Issue DevChatter#35 Curate the Live Channels Display on Hompage
Browse files Browse the repository at this point in the history
Requested updates to the pull request. Moved the getall method to the channelaggregateservices where it should be. Also changed the properties to Camel Casing
  • Loading branch information
CodeItQuick committed Apr 30, 2019
1 parent a0c672a commit f4e1bdf
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IChannelAggregateService
List<Channel> GetAll();
List<Channel> GetAll(string userId);
Channel GetAggregate(int id);
Task<List<Channel>> GetAllAggregate();
Task<int?> Create(Channel model, string userId);
Task<int> Update(Channel model);
Task<int> Delete(int id);
Expand Down
1 change: 0 additions & 1 deletion src/DevChatter.DevStreams.Core/Data/ICrudRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public interface ICrudRepository
Task<List<T>> GetAll<T>();
Task<List<T>> GetAll<T>(string filter, object args);
Task<List<T>> GetAll<T>(string filter, string orderBy, object args);
Task<List<Channel>> GetAllChannelInfo();
Task<int> Update<T>(T model) where T : DataEntity;
Task<int> Delete<T>(int id) where T : DataEntity;
Task<int> Delete<T>(T model) where T : DataEntity;
Expand Down
4 changes: 2 additions & 2 deletions src/DevChatter.DevStreams.Core/Twitch/ChannelLiveState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class ChannelLiveState
{
public string TwitchId { get; set; }
public bool IsLive { get; set; }
public DateTime startedAt { get; set; }
public int viewerCount { get; set; }
public DateTime StartedAt { get; set; }
public int ViewerCount { get; set; }

}
}
29 changes: 0 additions & 29 deletions src/DevChatter.DevStreams.Infra.Dapper/DapperCrudRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,6 @@ public async Task<List<T>> GetAll<T>(string filter, string orderBy, object args)
}
}

/// <summary>
/// Returns all the channel data for a channel object.
/// </summary>
/// <returns>All Channel Info in a List<Channel> Object </Channel></returns>
public async Task<List<Channel>> GetAllChannelInfo()
{
string sql = "SELECT * FROM Channels;";
string sql2 = "SELECT * FROM TwitchChannels;";

string combinedSQL = sql + sql2;

using (IDbConnection connection = new SqlConnection(_dbSettings.DefaultConnection))
{
var dbQuery = await connection.QueryMultipleAsync(combinedSQL);
List<Channel> channels = dbQuery.Read<Channel>().ToList();
var twitchChannels = dbQuery.Read<TwitchChannel>();

foreach (var channel in channels)
{
if (twitchChannels.Where(x => x?.ChannelId == channel?.Id).Any())
{
channel.Twitch = twitchChannels.Where(x => x?.ChannelId == channel?.Id).First();
}
}

return channels;
}

}

public async Task<int> Update<T>(T model) where T : DataEntity
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ public Channel GetAggregate(int id)
}
}

/// <summary>
/// Returns all the channel data for a channel object.
/// </summary>
/// <returns>All Channel Info in a List<Channel> Object </Channel></returns>
public async Task<List<Channel>> GetAllAggregate()
{
string sql = @"SELECT * FROM Channels;
SELECT * FROM TwitchChannels;";

using (IDbConnection connection = new SqlConnection(_dbSettings.DefaultConnection))
{
var dbQuery = await connection.QueryMultipleAsync(sql);
List<Channel> channels = dbQuery.Read<Channel>().ToList();
List<TwitchChannel> twitchChannels = dbQuery.Read<TwitchChannel>().ToList();

foreach (var channel in channels)
{
if (twitchChannels.Where(x => x?.ChannelId == channel?.Id).Any())
{
channel.Twitch = twitchChannels.Where(x => x?.ChannelId == channel?.Id).First();
}
}

return channels;
}

}

public async Task<int?> Create(Channel model, string userId)
{
using (IDbConnection connection = new SqlConnection(_dbSettings.DefaultConnection))
Expand Down
4 changes: 2 additions & 2 deletions src/DevChatter.DevStreams.Infra.Twitch/TwitchStreamService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public async Task<List<ChannelLiveState>> GetChannelLiveStates(List<string> twit
{
TwitchId = twitchId,
IsLive = liveChannels.Any(x => x.User_id == twitchId),
startedAt = result.Data.Where(x => x.User_id == twitchId).Select(x => x.Started_at.ToUniversalTime()).DefaultIfEmpty().First(),
viewerCount = result.Data.Where(x => x.User_id == twitchId).Select(x => x.Viewer_count).DefaultIfEmpty().First()
StartedAt = result.Data.Where(x => x.User_id == twitchId).Select(x => x.Started_at.ToUniversalTime()).DefaultIfEmpty().First(),
ViewerCount = result.Data.Where(x => x.User_id == twitchId).Select(x => x.Viewer_count).DefaultIfEmpty().First()

})
.ToList();
Expand Down
15 changes: 8 additions & 7 deletions src/DevChatter.DevStreams.Web/Controllers/IsLiveController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,31 @@ public IsLiveController(ICrudRepository crudRepository,
[HttpGet]
public async Task<IActionResult> Get()
{
List<TwitchChannel> channels = await _crudRepository.GetAll<TwitchChannel>();
List<string> twitchIds = channels.Select(x => x.TwitchId).ToList();
List<Channel> channels = await _channelAggregateService.GetAllAggregate();
List<string> twitchIds = channels.Select(x => x?.Twitch?.TwitchId).ToList();
twitchIds.RemoveAll(string.IsNullOrWhiteSpace);
var liveTwitchData = (await _twitchService.GetChannelLiveStates(twitchIds));

var sortedLiveData = liveTwitchData.OrderByDescending(o => o.viewerCount).ToList();
var sortedLiveData = liveTwitchData.OrderByDescending(o => o.ViewerCount).ToList();

var liveTwitchIds = sortedLiveData
.Where(x => x.IsLive)
.Select(x => x.TwitchId)
.ToList();

var liveChannelSorted = sortedLiveData
.OrderByDescending(x => x.viewerCount)
.Select(x => channels.Where(y => y.TwitchId == x.TwitchId).Where(v => x.IsLive).Select(z => z.TwitchName))
.OrderByDescending(x => x.ViewerCount)
.Select(x => channels.Where(y => y?.Twitch?.TwitchId == x?.TwitchId).Where(v => x.IsLive).Select(z => z?.Twitch?.TwitchName))
.Where(x => x.Any())
.ToList();

var timeDifference = sortedLiveData
.Where(x => liveTwitchIds.Contains(x.TwitchId))
.Select(x => (DateTime.UtcNow - x.startedAt.ToUniversalTime()));
.Select(x => (DateTime.UtcNow - x?.StartedAt.ToUniversalTime()));

var viewerCount = sortedLiveData
.Where(x => liveTwitchIds.Contains(x.TwitchId))
.Select(x => x.viewerCount)
.Select(x => x.ViewerCount)
.ToList();

var responseObject = new
Expand Down
2 changes: 1 addition & 1 deletion src/DevChatter.DevStreams.Web/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="col-md-3">
<h2>Live Now</h2> @*TODO: Show channels that are live right now.*@
<ul>
<li v-for="liveChannel, index in liveChannels.channel.slice(0, 10)">
<li v-for="liveChannel, index in liveChannels.channel">
{{liveChannel[0]}}
views: {{ liveChannels.viewCount[index] }}
Online: {{ liveChannels.timeOnline[index] }}
Expand Down
4 changes: 3 additions & 1 deletion src/DevChatter.DevStreams.Web/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public async Task<IActionResult> OnGetAsync()

public async Task<IActionResult> OnGetLuckyAsync()
{
List<Channel> channels = await _repo.GetAllChannelInfo();
List<Channel> channels = await _repo.GetAll<Channel>();



List<string> twitchIds = channels.Select(x => x?.Twitch?.TwitchId)
.Where(x => !string.IsNullOrWhiteSpace(x))
Expand Down

0 comments on commit f4e1bdf

Please sign in to comment.