Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions KillTeam/Controllers/TeamsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
using System.Windows.Input;
using KillTeam.Commands;
using KillTeam.Commands.Handlers;

using KillTeam.Queries;
using KillTeam.Queries.Handlers;
using KillTeam.Services;
using KillTeam.ViewModels;
using Microsoft.EntityFrameworkCore;
Expand All @@ -23,9 +24,11 @@ public class TeamsController
public ICommand OpenTeam { get; private set; }
public ICommand DeleteTeam { get; private set; }

public TeamsController(IList<ToolbarItem> toolbarItems,
public TeamsController(
IList<ToolbarItem> toolbarItems,
IHandleCommands<DeleteTeamCommand> deleteTeamCommandHandler,
IHandleCommands<ReorderTeamsCommand> reorderTeamsCommandHandler)
IHandleCommands<ReorderTeamsCommand> reorderTeamsCommandHandler,
IQueryProcessor queryProcessor)
{
Items = new ObservableCollection<TeamsViewModel>();

Expand All @@ -34,6 +37,7 @@ public TeamsController(IList<ToolbarItem> toolbarItems,

_reorderTeamsCommandHandler = reorderTeamsCommandHandler;
_deleteTeamCommandHandler = deleteTeamCommandHandler;
_queryProcessor = queryProcessor;
}

public async Task Refresh()
Expand Down Expand Up @@ -76,13 +80,10 @@ private void InitializeCommands()
private async Task UpdateItems()
{
Items.Clear();
var teams = await KTContext.Db.Teams
.Include(e => e.Faction)
.Include(e => e.Members)
.AsNoTracking()
.OrderBy(post => post.Position)
.ToListAsync();
teams.ForEach(i => Items.Add(new TeamsViewModel(i.Id, i.Name, i.Cost, i.FactionNameAndMembersCount)));

var teams = await _queryProcessor.Execute<AllTeamsQuery, List<TeamsViewModel>>(new AllTeamsQuery());

teams.ForEach(t => Items.Add(t));
}

private void AddTeamExecuted()
Expand Down Expand Up @@ -124,5 +125,6 @@ private async Task VersionExecuted()

private readonly IHandleCommands<ReorderTeamsCommand> _reorderTeamsCommandHandler;
private readonly IHandleCommands<DeleteTeamCommand> _deleteTeamCommandHandler;
private readonly IQueryProcessor _queryProcessor;
}
}
10 changes: 0 additions & 10 deletions KillTeam/Models/Team.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,6 @@ public List<string> Errors
return liste;
}
}

[JsonIgnore]
public string FactionNameAndMembersCount
{
get
{
int count = GetSelectedMembers().Count();
return Faction.Name + " - " + count + " " + (count <= 1 ? Properties.Resources.Membre : Properties.Resources.Membres);
}
}

#endregion Calculated Properties

Expand Down
7 changes: 7 additions & 0 deletions KillTeam/Queries/AllTeamsQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace KillTeam.Queries
{
public class AllTeamsQuery : IQuery
{

}
}
42 changes: 42 additions & 0 deletions KillTeam/Queries/Handlers/AllTeamsQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using KillTeam.Services;
using KillTeam.ViewModels;
using Microsoft.EntityFrameworkCore;

namespace KillTeam.Queries.Handlers
{
public class AllTeamsQueryHandler : IQueryHandler<AllTeamsQuery, List<TeamsViewModel>>
{
public async Task<List<TeamsViewModel>> Execute(AllTeamsQuery query)
{
var teams = await KTContext.Db.Teams
.Include(e => e.Faction)
.Include(e => e.Members)
.AsNoTracking()
.OrderBy(post => post.Position)
.Select(t => new
{
t.Id,
t.Name,
t.Members,
t.Faction,
t.Roster
})
.ToListAsync();

return teams
.Select(t =>
{
var selectedMembers = t.Members.Where(m => m.Selected || !t.Roster).ToList();
var count = selectedMembers.Count();
var cost = selectedMembers.Sum(m => m.Cost);
var subtitle = $"{t.Faction.Name} - {count} {(count <= 1 ? Properties.Resources.Membre : Properties.Resources.Membres)}";

return new TeamsViewModel(t.Id, t.Name, cost, subtitle);
})
.ToList();
}
}
}
9 changes: 9 additions & 0 deletions KillTeam/Queries/Handlers/IQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace KillTeam.Queries.Handlers
{
public interface IQueryHandler<in TQuery, TResponse> where TQuery : IQuery
{
Task<TResponse> Execute(TQuery query);
}
}
35 changes: 35 additions & 0 deletions KillTeam/Queries/Handlers/IQueryProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace KillTeam.Queries.Handlers
{
public interface IQueryProcessor
{
Task<TResult> Execute<TQuery, TResult>(TQuery query) where TQuery : IQuery;
}

public class QueryProcessor : IQueryProcessor
{
public static IQueryProcessor Instance() => _queryProcessor;

private static readonly IQueryProcessor _queryProcessor = new QueryProcessor();

public async Task<TResult> Execute<TQuery, TResult>(TQuery query) where TQuery : IQuery
{
var handlerType =
Assembly
.GetAssembly(typeof(TQuery))
.DefinedTypes
.FirstOrDefault(t => t.ImplementedInterfaces.Contains(typeof(IQueryHandler<TQuery, TResult>)));

if (handlerType is null)
throw new InvalidOperationException($"No query handler for Query {typeof(TQuery)} for result {typeof(TResult)}");

var handler = Activator.CreateInstance(handlerType) as IQueryHandler<TQuery, TResult>;

return await handler.Execute(query);
}
}
}
7 changes: 7 additions & 0 deletions KillTeam/Queries/IQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace KillTeam.Queries
{
public interface IQuery
{

}
}
7 changes: 6 additions & 1 deletion KillTeam/Views/TeamsView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using KillTeam.Commands.Handlers;
using KillTeam.Controllers;
using KillTeam.Queries.Handlers;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using Xamarin.Forms.Xaml;

Expand All @@ -13,7 +14,11 @@ public TeamsView()
InitializeComponent();
On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);

BindingContext = new TeamsController(ToolbarItems, new DeleteTeamCommandHandler(), new ReorderTeamsCommandHandler());
BindingContext = new TeamsController(
ToolbarItems,
new DeleteTeamCommandHandler(),
new ReorderTeamsCommandHandler(),
QueryProcessor.Instance());
}

protected override async void OnAppearing()
Expand Down