diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs index f4c2a7a..533cb83 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/TournamentController.cs @@ -21,13 +21,19 @@ namespace LaDOSE.Api.Controllers _service = service; } - - [HttpGet("GetTournaments")] - public async Task> GetChallonges() + //This may be a get , but i dont know what the RFC State for Get request with Body + //As i don't like to populate GET request with body this will be a post (and i think + //it will be easier to proxy. + [HttpPost("GetTournaments")] + public async Task> GetChallonges([FromBody] TimeRangeDTO dto) { - - var tournaments = await _service.GetTournaments(DateTime.Now.AddMonths(-2), null); - return AutoMapper.Mapper.Map>(tournaments); + if (dto.To.HasValue | dto.From.HasValue) + { + var tournaments = await _service.GetTournaments(dto.From, dto.To); + return AutoMapper.Mapper.Map>(tournaments); + } + + return null; } diff --git a/LaDOSE.Src/LaDOSE.DTO/TimeRangeDTO.cs b/LaDOSE.Src/LaDOSE.DTO/TimeRangeDTO.cs new file mode 100644 index 0000000..ba6dd8f --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DTO/TimeRangeDTO.cs @@ -0,0 +1,10 @@ +using System; + +namespace LaDOSE.DTO +{ + public class TimeRangeDTO + { + public DateTime? From { get; set; } + public DateTime? To { get; set; } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs index 536810f..ace8607 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; using System.Windows; +using System.Windows.Input; namespace LaDOSE.DesktopApp.Utils { @@ -14,5 +17,30 @@ namespace LaDOSE.DesktopApp.Utils Application.Current.Dispatcher.BeginInvoke(action); } + public static void Await(Action function,string message=null) + { + Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait; + Task tsk = Task.Factory.StartNew(new Action(function)); + + tsk.ContinueWith(t => + { + Application.Current.Dispatcher.Invoke(() => + System.Windows.Input.Mouse.OverrideCursor = null); + MessageBox.Show(t.Exception.InnerException.Message); + + }, + CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, + TaskScheduler.FromCurrentSynchronizationContext()); + tsk.ContinueWith(task => + { + if (!string.IsNullOrEmpty(message)) + MessageBox.Show(message); + Application.Current.Dispatcher.Invoke(() => + System.Windows.Input.Mouse.OverrideCursor = null); + }); + + + } + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs index b735f5d..46e892a 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/TournamentResultViewModel.cs @@ -6,6 +6,7 @@ using System.Data; using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Windows.Controls; using System.Windows.Forms; using LaDOSE.DesktopApp.Utils; @@ -22,13 +23,45 @@ namespace LaDOSE.DesktopApp.ViewModels private RestService RestService { get; set; } - public TournamentResultViewModel(RestService restService) + #region Properties + private String _selectRegex; + + public String SelectRegex { - this.RestService = restService; - _selectedTournaments = new ObservableCollection(); - Tournaments = new List(); + get { return _selectRegex; } + set + { + _selectRegex = value; + NotifyOfPropertyChange(() => SelectRegex); + } } + + + private DateTime _from; + + public DateTime From + { + get { return _from; } + set + { + _from = value; + NotifyOfPropertyChange(() => From); + } + } + + private DateTime _to; + public DateTime To + { + get { return _to; } + set + { + _to = value; + NotifyOfPropertyChange(() => To); + } + } + + private TournamentsResultDTO _results; public List Tournaments { get; set; } @@ -96,18 +129,36 @@ namespace LaDOSE.DesktopApp.ViewModels } } + #endregion + public TournamentResultViewModel(RestService restService) + { + this.RestService = restService; + _selectedTournaments = new ObservableCollection(); + Tournaments = new List(); + } + + + protected override void OnInitialize() { + this.To=DateTime.Now; + this.From = DateTime.Now.AddMonths(-1); + this.SelectRegex = "Ranking"; LoadTournaments(); base.OnInitialize(); } public void LoadTournaments() { - var tournamentDtos = this.RestService.GetTournaments().ToList(); - this.Tournaments = tournamentDtos; + WpfUtil.Await(() => + { + var tournamentDtos = this.RestService + .GetTournaments(new TimeRangeDTO() {From = this.From, To = this.To}).ToList(); + this.Tournaments = tournamentDtos; + + NotifyOfPropertyChange("Tournaments"); + }); - NotifyOfPropertyChange("Tournaments"); } public DataTable GridDataTable @@ -122,10 +173,34 @@ namespace LaDOSE.DesktopApp.ViewModels public void Select() { - var tournamentsIds = SelectedTournaments.Select(e => e.Id).ToList(); - var resultsDto = this.RestService.GetResults(tournamentsIds); - this.Results = resultsDto; - ComputeDataGrid(); + WpfUtil.Await(() => + { + var tournamentsIds = SelectedTournaments.Select(e => e.Id).ToList(); + var resultsDto = this.RestService.GetResults(tournamentsIds); + this.Results = resultsDto; + ComputeDataGrid(); + }); + + } + + public void SelectYear() + { + this.To = DateTime.Now; + this.From = new DateTime(DateTime.Now.Year,1,1); + + } + public void SelectMonth() + { + this.To = DateTime.Now; + this.From = DateTime.Now.AddMonths(-1); + } + public void SelectRegexp() + { + var selectedTournaments = this.Tournaments.Where(e => Regex.IsMatch(e.Name, this.SelectRegex)).ToList(); + this.SelectedTournaments.Clear(); + if(selectedTournaments.Count>0) + selectedTournaments.ForEach(e=>this.SelectedTournaments.AddUI(e)); + } private void ComputeDataGrid() @@ -138,7 +213,7 @@ namespace LaDOSE.DesktopApp.ViewModels DataTable grid = new DataTable(); grid.Columns.Add("Players"); Results.Games.ForEach(e => grid.Columns.Add(e.Name.Replace('.',' '))); - grid.Columns.Add("Total"); + grid.Columns.Add("Total").DataType = typeof(Int32); for (int i = 0; i < resultsParticipents.Count; i++) @@ -157,17 +232,18 @@ namespace LaDOSE.DesktopApp.ViewModels if (dictionary.ContainsKey(resultsGame.Id)) { int points = dictionary[resultsGame.Id]; - dataRow[resultsGame.Name.Replace('.', ' ')] = points.ToString(); + dataRow[resultsGame.Name.Replace('.', ' ')] = points; total += points; } else dataRow[resultsGame.Name.Replace('.', ' ')] = null; } - - dataRow["Total"] = total.ToString(); + + dataRow["Total"] = total; } + grid.DefaultView.Sort = "Total DESC"; this.GridDataTable = grid; } @@ -182,70 +258,31 @@ namespace LaDOSE.DesktopApp.ViewModels private void ExportToCSV() { - SaveFileDialog sfDialog = new SaveFileDialog() + if (this.GridDataTable != null) { - AddExtension = true - }; - if (sfDialog.ShowDialog() == true) - { - var resultsParticipents = this.Results.Participents.OrderBy(e => e.Name).ToList(); - var computed = ResultsToDataDictionary(resultsParticipents); - StringBuilder sb = new StringBuilder(); - - - sb.AppendLine(Results.Games.Aggregate("Player;", (current, t) => current + (t.Name + ";"))); - - for (int i = 0; i < resultsParticipents.Count; i++) + SaveFileDialog sfDialog = new SaveFileDialog() { - var entry = ""; + AddExtension = true + }; + if (sfDialog.ShowDialog() == true) + { + StringBuilder sb = new StringBuilder(); - var resultsParticipent = resultsParticipents[i]; + IEnumerable columnNames = this.GridDataTable.Columns.Cast() + .Select(column => column.ColumnName); + sb.AppendLine(string.Join(";", columnNames)); - entry = resultsParticipent.Name + ";"; - var gameDtos = Results.Games.Distinct().ToList(); - for (int j = 0; j < gameDtos.Count; j++) + foreach (DataRow row in this.GridDataTable.Rows) { - var resultsGame = Results.Games[j]; - var dictionary = computed[resultsParticipent.Name]; - entry += dictionary.ContainsKey(resultsGame.Id) - ? dictionary[resultsGame.Id].ToString() + ";" - : ";"; + //EXCEL IS A BITCH + IEnumerable fields = row.ItemArray.Select(field => + string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\"")); + sb.AppendLine(string.Join(";", fields)); } - sb.AppendLine(entry); + File.WriteAllText(sfDialog.FileName, sb.ToString()); } - - - //string[][] resultCsv = new string[resultsParticipents.Count + 1][]; - //resultCsv[0] = new string[Results.Games.Count + 1]; - //resultCsv[0][0] = "Player"; - //for (int j = 0; j < Results.Games.Count; j++) - //{ - // resultCsv[0][j + 1] = Results.Games[j].Name; - //} - - //for (int i = 0; i < resultsParticipents.Count; i++) - //{ - // resultCsv[i + 1] = new string[Results.Games.Count + 1]; - - // var resultsParticipent = resultsParticipents[i]; - - // resultCsv[i + 1][0] = resultsParticipent.Name; - // for (int j = 0; j < Results.Games.Count; j++) - // { - // var resultsGame = Results.Games[j]; - // var dictionary = computed[resultsParticipent.Name]; - // if (dictionary.ContainsKey(resultsGame.Id)) - // { - // var i1 = dictionary[resultsGame.Id]; - // resultCsv[i + 1][j + 1] = i1.ToString(); - // } - // } - //} - - //Save - File.WriteAllText(sfDialog.FileName, sb.ToString()); } } diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs index 575b9e3..a5479c8 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs @@ -122,17 +122,7 @@ namespace LaDOSE.DesktopApp.ViewModels public void UpdateDb() { - Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait; - var tsk = Task.Factory.StartNew(new Action(()=>this.RestService.RefreshDb())); - tsk.ContinueWith(t => - { - MessageBox.Show(t.Exception.InnerException.Message); - }, - CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, - TaskScheduler.FromCurrentSynchronizationContext()); - - MessageBox.Show("Database updated"); - + WpfUtil.Await(()=>this.RestService.RefreshDb(), "Updated"); } public void Generate() diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml b/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml index 5134c18..4ad5480 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/TournamentResultView.xaml @@ -9,10 +9,11 @@ xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:behaviors="clr-namespace:LaDOSE.DesktopApp.Behaviors" mc:Ignorable="d" - + d:DesignHeight="450" d:DesignWidth="800"> + @@ -20,30 +21,72 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + - - + - - + - + - + @@ -51,15 +94,16 @@ - + Jeux : - + + IsTextSearchEnabled="True" TextSearch.TextPath="Name" + SelectedItem="{Binding SelectedGame, UpdateSourceTrigger=PropertyChanged}"> @@ -87,21 +131,20 @@ - + - - - + - + Total : - + - + @@ -116,9 +159,9 @@ - - - - + + + + \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.REST/RestService.cs b/LaDOSE.Src/LaDOSE.REST/RestService.cs index f048d63..f5d81b9 100644 --- a/LaDOSE.Src/LaDOSE.REST/RestService.cs +++ b/LaDOSE.Src/LaDOSE.REST/RestService.cs @@ -252,12 +252,11 @@ namespace LaDOSE.REST #region Tournaments - public List GetTournaments() + public List GetTournaments(TimeRangeDTO timeRange) { CheckToken(); - var restRequest = new RestRequest("/api/Tournament/GetTournaments", Method.GET); - var restResponse = Client.Get>(restRequest); - return restResponse.Data; + List tournamentDtos = Post>("/api/Tournament/GetTournaments",timeRange); + return tournamentDtos; } public TournamentsResultDTO GetResults(List ids)