Skip to content

Commit

Permalink
Merge pull request DevChatter#70 from essenbee/additional-graphql
Browse files Browse the repository at this point in the history
Add new root queries to GraphQL
  • Loading branch information
benrick authored Apr 20, 2019
2 parents 64b53a6 + cb8c3f9 commit 1a95c67
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using DevChatter.DevStreams.Core.Model;

namespace DevChatter.DevStreams.Core.Services
{
public interface IChannelSearchService
{
List<Channel> Find();
Task<Channel> GetChannelSoundex(string standardizedChannelName);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Dapper;
using DevChatter.DevStreams.Core.Model;
using DevChatter.DevStreams.Core.Services;
using DevChatter.DevStreams.Core.Settings;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using DevChatter.DevStreams.Core.Services;
using System.Threading.Tasks;

namespace DevChatter.DevStreams.Infra.Dapper.Services
{
Expand Down Expand Up @@ -41,5 +42,18 @@ public List<Channel> Find()
return channels;
}
}

public async Task<Channel> GetChannelSoundex(string standardizedChannelName)
{
var sql = @"SELECT TOP 1 * FROM Channels WHERE SOUNDEX(Name) = SOUNDEX(@standardizedChannelName)
ORDER BY DIFFERENCE(Name, @standardizedChannelName) DESC";
using (IDbConnection connection = new SqlConnection(_dbSettings.DefaultConnection))
{
using (var multi = await connection.QueryMultipleAsync(sql, new { standardizedChannelName }))
{
return (await multi.ReadAsync<Channel>()).SingleOrDefault();
}
}
}
}
}
57 changes: 56 additions & 1 deletion src/DevChatter.DevStreams.Infra.GraphQL/DevStreamsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
using DevChatter.DevStreams.Infra.GraphQL.Types;
using GraphQL.Types;
using System.Linq;
using DevChatter.DevStreams.Core.Services;
using System.Threading.Tasks;
using DevChatter.DevStreams.Core.Twitch;

namespace DevChatter.DevStreams.Infra.GraphQL
{
public class DevStreamsQuery : ObjectGraphType
{
public DevStreamsQuery(IChannelRepository channelRepo)
private readonly IChannelRepository _channelRepo;
private readonly ITwitchStreamService _twitchService;

public DevStreamsQuery(IChannelRepository channelRepo, ITwitchStreamService twitchService,
IChannelSearchService channelSearchService)
{
_channelRepo = channelRepo;
_twitchService = twitchService;

Field<ListGraphType<ChannelType>>("channels",
arguments: new QueryArguments(new QueryArgument<ListGraphType<IdGraphType>>
{
Expand All @@ -37,6 +47,51 @@ public DevStreamsQuery(IChannelRepository channelRepo)
var id = ctx.GetArgument<int>("id");
return channelRepo.Get<Channel>(id);
});

Field<ChannelType>("channelSoundex",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>>
{
Name = "name"
}),
resolve: ctx =>
{
var name = ctx.GetArgument<string>("name");
return channelSearchService.GetChannelSoundex(name);
});
Field<ListGraphType<ChannelType>>("liveChannels",
resolve: ctx =>
{
return GetLiveChannels();
});
}

private async Task<List<Channel>> GetLiveChannels()
{
var channels = await _channelRepo.GetAll<Channel>();
var liveChannelIds = await GetLiveTwitchChannels();

return channels
.Where( c=> liveChannelIds.Contains(c.Id))
.ToList();
}

private async Task<List<int>> GetLiveTwitchChannels()
{
var twitchChannels = await _channelRepo.GetAll<TwitchChannel>();

var twitchIds = twitchChannels.Select(t => t.TwitchId)
.Where(x => !string.IsNullOrWhiteSpace(x))
.ToList();

var liveChannelTwitchIds = (await _twitchService.GetChannelLiveStates(twitchIds))
.Where(x => x.IsLive)
.Select(x => x.TwitchId)
.ToList();

return twitchChannels
.Where(c => liveChannelTwitchIds.Contains(c.TwitchId))
.Select(c => c.ChannelId)
.ToList();
}
}
}

0 comments on commit 1a95c67

Please sign in to comment.