-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5 Add a controller for teams statistic.
- Loading branch information
Adrian Fürschuß
committed
Nov 23, 2016
1 parent
ee1213b
commit cef90c4
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Wuzlstats.Models; | ||
using Wuzlstats.Services; | ||
using Wuzlstats.ViewModels.Teams; | ||
|
||
namespace Wuzlstats.Controllers | ||
{ | ||
public class TeamsController : Controller | ||
{ | ||
private readonly Db _db; | ||
private readonly AppSettings _settings; | ||
private readonly TeamStatisticsService _statisticsService; | ||
|
||
public TeamsController(Db db, AppSettings settings) | ||
{ | ||
_settings = settings; | ||
_db = db; | ||
_statisticsService = new TeamStatisticsService(_db); | ||
} | ||
|
||
[Route("~/League/{league}/Teams")] | ||
public async Task<IActionResult> Index(string league, string sort, bool recent) | ||
{ | ||
var leagueEntity = _db.Leagues.FirstOrDefault(x => x.Name.ToLowerInvariant() == league.ToLowerInvariant()); | ||
if (leagueEntity == null) | ||
{ | ||
return RedirectToAction("Index", "Leagues"); | ||
} | ||
ViewBag.CurrentLeague = leagueEntity.Name; | ||
var teams = await _statisticsService.FindTeamsOfLeagueAsync(leagueEntity.Id, recent ? _settings.DaysForStatistics : default(int?)); | ||
|
||
switch (sort) | ||
{ | ||
case "best": | ||
teams = teams.OrderByDescending(x => x.Rank); | ||
break; | ||
case "worst": | ||
teams = teams.OrderBy(x => x.Rank); | ||
break; | ||
case "activity": | ||
teams = teams.OrderByDescending(x => x.Wins + x.Losses); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return View(new IndexViewModel | ||
{ | ||
ActiveFilter = sort, | ||
Recent = recent, | ||
Days = _settings.DaysForStatistics, | ||
Teams = teams | ||
}); | ||
} | ||
} | ||
} |