From ac9614a70acbc53717e31bce1ac0bc6760ff3ea9 Mon Sep 17 00:00:00 2001 From: Darkstack <1835601+darkstack@users.noreply.github.com> Date: Sat, 9 Mar 2019 14:03:25 +0100 Subject: [PATCH] Test Creation DesktopApp --- .../LaDOSE.Api/Controllers/EventController.cs | 15 +- .../Controllers/GenericController.cs | 2 +- .../LaDOSE.Api/Controllers/UtilController.cs | 28 --- .../Controllers/WordPressController.cs | 48 +++- LaDOSE.Src/LaDOSE.Api/Startup.cs | 7 +- LaDOSE.Src/LaDOSE.DTO/Game.cs | 2 + LaDOSE.Src/LaDOSE.DTO/WPEvent.cs | 16 +- LaDOSE.Src/LaDOSE.DesktopApp/App.config | 7 +- LaDOSE.Src/LaDOSE.DesktopApp/App.xaml | 3 +- .../LaDOSE.DesktopApp.csproj | 19 +- .../LaDOSE.DesktopApp/Services/RestService.cs | 93 +++++++- .../UserControls/BookingUserControl.xaml | 30 +++ .../UserControls/BookingUserControl.xaml.cs | 75 ++++++ .../LaDOSE.DesktopApp/Utils/PhpSerialize.cs | 214 ++++++++++++++++++ .../Utils/WpEventDeserialize.cs | 44 ++++ LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs | 15 ++ .../ViewModels/GameViewModel.cs | 38 +++- .../ViewModels/ShellViewModel.cs | 11 +- .../ViewModels/WordPressViewModel.cs | 166 +++++++++++++- .../LaDOSE.DesktopApp/Views/GameView.xaml | 36 ++- .../LaDOSE.DesktopApp/Views/ShellView.xaml | 14 +- .../LaDOSE.DesktopApp/Views/WpView.xaml | 135 +++++++++-- .../LaDOSE.DesktopApp/Views/WpView.xaml.cs | 2 + LaDOSE.Src/LaDOSE.Entity/Game.cs | 3 +- .../LaDOSE.Service/Helper/Reservation.cs | 8 + .../Helper/WpEventDeserialize.cs | 37 +++ .../LaDOSE.Service/Interface/IBaseService.cs | 2 + .../LaDOSE.Service/Interface/IEventService.cs | 4 +- .../LaDOSE.Service/Interface/IUtilService.cs | 7 - .../Interface/IWordPressService.cs | 5 + .../LaDOSE.Service/Service/BaseService.cs | 19 +- .../LaDOSE.Service/Service/EventService.cs | 71 +----- .../LaDOSE.Service/Service/UtilService.cs | 24 -- .../Service/WordPressService.cs | 120 +++++++++- 34 files changed, 1131 insertions(+), 189 deletions(-) delete mode 100644 LaDOSE.Src/LaDOSE.Api/Controllers/UtilController.cs create mode 100644 LaDOSE.Src/LaDOSE.DesktopApp/UserControls/BookingUserControl.xaml create mode 100644 LaDOSE.Src/LaDOSE.DesktopApp/UserControls/BookingUserControl.xaml.cs create mode 100644 LaDOSE.Src/LaDOSE.DesktopApp/Utils/PhpSerialize.cs create mode 100644 LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpEventDeserialize.cs create mode 100644 LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs create mode 100644 LaDOSE.Src/LaDOSE.Service/Helper/Reservation.cs create mode 100644 LaDOSE.Src/LaDOSE.Service/Helper/WpEventDeserialize.cs delete mode 100644 LaDOSE.Src/LaDOSE.Service/Interface/IUtilService.cs delete mode 100644 LaDOSE.Src/LaDOSE.Service/Service/UtilService.cs diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/EventController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/EventController.cs index e801e22..d7db738 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/EventController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/EventController.cs @@ -23,19 +23,6 @@ namespace LaDOSE.Api.Controllers this._gameService = gameService; } - [HttpGet("GetUsers/{eventId}/{wpEventId}/{gameId}")] - public List GetUsers(int eventId, int wpEventId,int gameId) - { - var game = _gameService.GetById(gameId); - return _service.GetBooking(eventId, wpEventId,game); - - } - - [HttpGet("Generate/{eventId}/{wpEventId}")] - public bool GenerateChallonge(int eventId, int wpEventId) - { - return _service.CreateChallonge(eventId, wpEventId); - - } + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/GenericController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/GenericController.cs index 3b3dad9..afd7840 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/GenericController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/GenericController.cs @@ -18,7 +18,7 @@ namespace LaDOSE.Api.Controllers [HttpPost] public TU Post([FromBody]TU dto) { - return _service.Create(dto); + return _service.AddOrUpdate(dto); } [HttpGet] public List Get() diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/UtilController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/UtilController.cs deleted file mode 100644 index d7764a0..0000000 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/UtilController.cs +++ /dev/null @@ -1,28 +0,0 @@ -using LaDOSE.Business.Interface; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; - -namespace LaDOSE.Api.Controllers -{ - [Authorize] - [Produces("application/json")] - [Route("api/[controller]")] - public class UtilController : Controller - { - private IUtilService _service; - - public UtilController(IUtilService service) - { - _service = service; - } - - - - [HttpGet("UpdateBooking")] - public bool UpdateBooking() - { - return _service.UpdateBooking(); - - } - } -} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/WordPressController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/WordPressController.cs index fea67c3..7fa07c4 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/WordPressController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/WordPressController.cs @@ -11,20 +11,62 @@ namespace LaDOSE.Api.Controllers [Route("api/[controller]")] public class WordPressController : Controller { + public IGameService GameService { get; } + private IWordPressService _service; // GET - public WordPressController(IWordPressService service) + public WordPressController(IWordPressService service, IGameService gameService ) { + GameService = gameService; _service = service; } - private IWordPressService _service; + [HttpGet("WPEvent")] public List Event() { - return _service.GetWpEvent(); + var wpEvents = _service.GetWpEvent(); + foreach (var wpEvent in wpEvents) + { + foreach (var wpEventWpBooking in wpEvent.WPBookings) + { + wpEventWpBooking.WPEvent = null; + wpEventWpBooking.WPUser.WPBookings = null; + } + } + return wpEvents; + } + + + [HttpGet("GetUsers/{wpEventId}/{gameId}")] + public List GetUsers(int wpEventId, int gameId) + { + var game = GameService.GetById(gameId); + return _service.GetBooking(wpEventId, game); + + } + [HttpGet("GetUsersOptions/{wpEventId}/{gameId}")] + public List GetUsersOptions(int wpEventId, int gameId) + { + var game = GameService.GetById(gameId); + return _service.GetBookingOptions(wpEventId, game); + + } + + + [HttpGet("UpdateDb")] + public bool UpdateDb() + { + return _service.UpdateBooking(); + + } + + [HttpGet("CreateChallonge/{gameId:int}/{wpEventId:int}")] + public bool CreateChallonge(int gameId, int wpEventId) + { + return _service.CreateChallonge(gameId, wpEventId); } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Api/Startup.cs b/LaDOSE.Src/LaDOSE.Api/Startup.cs index e838a4c..4368aa4 100644 --- a/LaDOSE.Src/LaDOSE.Api/Startup.cs +++ b/LaDOSE.Src/LaDOSE.Api/Startup.cs @@ -44,7 +44,11 @@ namespace LaDOSE.Api services.AddCors(); - services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); + services.AddMvc().AddJsonOptions(x => + { + x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; + x.SerializerSettings.MaxDepth= 4; + }); services.AddDbContextPool( // replace "YourDbContext" with the class name of your DbContext options => options.UseMySql($"Server={MySqlServer};Database={MySqlDatabase};User={MySqlUser};Password={MySqlPassword};", // replace with your Connection String mysqlOptions => @@ -99,7 +103,6 @@ namespace LaDOSE.Api services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); services.AddScoped(); services.AddTransient(p => new ChallongeProvider(this.Configuration["ApiKey:ChallongeApiKey"])); } diff --git a/LaDOSE.Src/LaDOSE.DTO/Game.cs b/LaDOSE.Src/LaDOSE.DTO/Game.cs index dab82f5..65e9468 100644 --- a/LaDOSE.Src/LaDOSE.DTO/Game.cs +++ b/LaDOSE.Src/LaDOSE.DTO/Game.cs @@ -5,5 +5,7 @@ public int Id { get; set; } public string Name { get; set; } public string ImgUrl { get; set; } + public string WordPressTag { get; set; } + public string WordPressTagOs { get; set; } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DTO/WPEvent.cs b/LaDOSE.Src/LaDOSE.DTO/WPEvent.cs index 735f58c..f5f547e 100644 --- a/LaDOSE.Src/LaDOSE.DTO/WPEvent.cs +++ b/LaDOSE.Src/LaDOSE.DTO/WPEvent.cs @@ -12,6 +12,20 @@ namespace LaDOSE.DTO public string Name { get; set; } public string Slug { get; set; } public DateTime? Date { get; set; } - + public List WpBookings { get; set; } } + + public class WPBooking + { + public WPUser WpUser { get; set; } + public string Message { get; set; } + + public string Meta { get; set; } + } + + public class WPUser + { + public string Name { get; set; } + } + } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/App.config b/LaDOSE.Src/LaDOSE.DesktopApp/App.config index 731f6de..29bd873 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/App.config +++ b/LaDOSE.Src/LaDOSE.DesktopApp/App.config @@ -1,6 +1,11 @@  - + + + + + + \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/App.xaml b/LaDOSE.Src/LaDOSE.DesktopApp/App.xaml index 7af8590..09ab519 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/App.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/App.xaml @@ -4,7 +4,8 @@ xmlns:local="clr-namespace:LaDOSE.DesktopApp" > - + + diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj b/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj index f680129..d72efd6 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj +++ b/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj @@ -48,6 +48,7 @@ ..\packages\RestSharp.106.6.5\lib\net452\RestSharp.dll + @@ -72,7 +73,13 @@ Designer + + + BookingUserControl.xaml + + + @@ -89,6 +96,10 @@ App.xaml Code + + Designer + MSBuild:Compile + MSBuild:Compile Designer @@ -127,7 +138,9 @@ - + + Designer + @@ -135,6 +148,8 @@ LaDOSE.DTO - + + + \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Services/RestService.cs b/LaDOSE.Src/LaDOSE.DesktopApp/Services/RestService.cs index 8f55da6..d858414 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Services/RestService.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Services/RestService.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Configuration; using System.Windows; using LaDOSE.DTO; using RestSharp; using RestSharp.Authenticators; using RestSharp.Serialization.Json; +using DataFormat = RestSharp.DataFormat; namespace LaDOSE.DesktopApp.Services { @@ -14,9 +16,14 @@ namespace LaDOSE.DesktopApp.Services public RestService() { - Client = new RestClient("http://localhost:5000/"); + + var appSettings = ConfigurationManager.AppSettings; + string url = (string) appSettings["ApiUri"]; + string user = (string)appSettings["ApiUser"]; + string password = (string) appSettings["ApiPassword"]; + Client = new RestClient(url); var restRequest = new RestRequest("users/auth", Method.POST); - restRequest.AddJsonBody(new { username = "****", password = "*****" }); + restRequest.AddJsonBody(new { username = user, password = password }); var response = Client.Post(restRequest); if (response.IsSuccessful) { @@ -26,10 +33,42 @@ namespace LaDOSE.DesktopApp.Services } else { - throw new Exception("No Service Avaliable"); + MessageBox.Show("Unable to contact services, i m useless, BYEKTHX","Error",MessageBoxButton.OK,MessageBoxImage.Error); + Application.Current.Shutdown(-1); } } + #region PostFix + + private T Post(string resource,T entity) + { + var json = new RestSharp.Serialization.Json.JsonSerializer(); + var jsonD = new RestSharp.Serialization.Json.JsonDeserializer(); + var request = new RestRequest(); + request.Method = Method.POST; + request.Resource = resource; + request.AddHeader("Accept", "application/json"); + request.AddHeader("Content-type", "application/json"); + request.Parameters.Clear(); + request.AddParameter("application/json; charset=utf-8", json.Serialize(entity), ParameterType.RequestBody); + request.AddObject(entity); + var response = Client.Execute(request); + //var content = response.Content; // raw content as string + try + { + return jsonD.Deserialize(response); + } + catch (Exception) + { + return default(T); + } + + + } + + #endregion + + #region WordPress public List GetEvents() { var restRequest = new RestRequest("/api/wordpress/WPEvent", Method.GET); @@ -37,11 +76,59 @@ namespace LaDOSE.DesktopApp.Services return restResponse.Data; } + + public bool CreateChallonge(int gameId, int eventId) + { + var restRequest = new RestRequest($"/api/wordpress/CreateChallonge/{gameId}/{eventId}", Method.GET); + var restResponse = Client.Get(restRequest); + return restResponse.Data; + } + public bool RefreshDb() + { + var restRequest = new RestRequest("/api/Wordpress/UpdateDb", Method.GET); + var restResponse = Client.Get(restRequest); + return restResponse.Data; + } + + public List GetUsers(int wpEventId, int gameId) + { + var restRequest = new RestRequest($"/api/Wordpress/GetUsers/{wpEventId}/{gameId}", Method.GET); + var restResponse = Client.Get>(restRequest); + return restResponse.Data; + } + + public List GetUsersOptions(int wpEventId, int gameId) + { + var restRequest = new RestRequest($"/api/Wordpress/GetUsersOptions/{wpEventId}/{gameId}", Method.GET); + var restResponse = Client.Get>(restRequest); + return restResponse.Data; + } + + + #endregion + + #region Games public List GetGames() { var restRequest = new RestRequest("/api/Game", Method.GET); var restResponse = Client.Get>(restRequest); return restResponse.Data; } + + public Game UpdateGame(Game eventUpdate) + { + return Post("Api/Game", eventUpdate); + } + #endregion + + #region Events + + + #endregion + + + + + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/UserControls/BookingUserControl.xaml b/LaDOSE.Src/LaDOSE.DesktopApp/UserControls/BookingUserControl.xaml new file mode 100644 index 0000000..0774619 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DesktopApp/UserControls/BookingUserControl.xaml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/UserControls/BookingUserControl.xaml.cs b/LaDOSE.Src/LaDOSE.DesktopApp/UserControls/BookingUserControl.xaml.cs new file mode 100644 index 0000000..9928d84 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DesktopApp/UserControls/BookingUserControl.xaml.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using LaDOSE.DesktopApp.Utils; +using LaDOSE.DTO; + +namespace LaDOSE.DesktopApp.UserControls +{ + /// + /// Interaction logic for BookingUserControl.xaml + /// + public partial class BookingUserControl : UserControl + { + + public readonly String[] EventManagerField = new[] {"HR3", "HR2", "COMMENT", "BOOKING_COMMENT"}; + + public String Current + { + get { return (String)GetValue(CurrentProperty); } + set { SetValue(CurrentProperty, value); } + } + + // Using a DependencyProperty as the backing store for Current. This enables animation, styling, binding, etc... + public static readonly DependencyProperty CurrentProperty = + DependencyProperty.Register("Current", typeof(String), typeof(BookingUserControl), new PropertyMetadata("",PropertyChangedCallback)); + + private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + BookingUserControl uc = d as BookingUserControl; + if (uc != null) + { + uc.Parse((string) e.NewValue); + } + } + + + public ObservableCollection Reservation { get; set; } + + + private void Parse(string value) + { + Reservation.Clear(); + var games = WpEventDeserialize.Parse(value); + if (games != null) games.OrderBy(e => e.Name).ToList().ForEach(res => Reservation.Add(res)); + } + + + + public BookingUserControl() + { + InitializeComponent(); + Reservation = new ObservableCollection(); + this.DataContext = this; + + } + + + } + + +} diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Utils/PhpSerialize.cs b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/PhpSerialize.cs new file mode 100644 index 0000000..9dbaa4a --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/PhpSerialize.cs @@ -0,0 +1,214 @@ +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +namespace LaDOSE.DesktopApp.Utils +{ + /// + /// PhpSerializer Class. + /// + public class PhpSerializer + { + private readonly NumberFormatInfo nfi; + + private int pos; //for unserialize + + private Dictionary seenArrayLists; //for serialize (to infinte prevent loops) lol + //types: + // N = null + // s = string + // i = int + // d = double + // a = array (hashtable) + + private Dictionary seenHashtables; //for serialize (to infinte prevent loops) + //http://www.w3.org/TR/REC-xml/#sec-line-ends + + public Encoding StringEncoding = new UTF8Encoding(); + + public bool + XMLSafe = true; //This member tells the serializer wether or not to strip carriage returns from strings when serializing and adding them back in when deserializing + + public PhpSerializer() + { + nfi = new NumberFormatInfo(); + nfi.NumberGroupSeparator = ""; + nfi.NumberDecimalSeparator = "."; + } + + public string Serialize(object obj) + { + seenArrayLists = new Dictionary(); + seenHashtables = new Dictionary(); + + return Serialize(obj, new StringBuilder()).ToString(); + } //Serialize(object obj) + + private StringBuilder Serialize(object obj, StringBuilder sb) + { + if (obj == null) return sb.Append("N;"); + + if (obj is string) + { + var str = (string) obj; + if (XMLSafe) + { + str = str.Replace("\r\n", "\n"); //replace \r\n with \n + str = str.Replace("\r", "\n"); //replace \r not followed by \n with a single \n Should we do this? + } + + return sb.Append("s:" + StringEncoding.GetByteCount(str) + ":\"" + str + "\";"); + } + + if (obj is bool) return sb.Append("b:" + ((bool) obj ? "1" : "0") + ";"); + + if (obj is int) + { + var i = (int) obj; + return sb.Append("i:" + i.ToString(nfi) + ";"); + } + + if (obj is double) + { + var d = (double) obj; + + return sb.Append("d:" + d.ToString(nfi) + ";"); + } + + if (obj is ArrayList) + { + if (seenArrayLists.ContainsKey((ArrayList) obj)) + return sb.Append("N;"); //cycle detected + seenArrayLists.Add((ArrayList) obj, true); + + var a = (ArrayList) obj; + sb.Append("a:" + a.Count + ":{"); + for (var i = 0; i < a.Count; i++) + { + Serialize(i, sb); + Serialize(a[i], sb); + } + + sb.Append("}"); + return sb; + } + + if (obj is Hashtable) + { + if (seenHashtables.ContainsKey((Hashtable) obj)) + return sb.Append("N;"); //cycle detected + seenHashtables.Add((Hashtable) obj, true); + + var a = (Hashtable) obj; + sb.Append("a:" + a.Count + ":{"); + foreach (DictionaryEntry entry in a) + { + Serialize(entry.Key, sb); + Serialize(entry.Value, sb); + } + + sb.Append("}"); + return sb; + } + + return sb; + } //Serialize(object obj) + + public object Deserialize(string str) + { + pos = 0; + return this.deserialize(str); + } //Deserialize(string str) + + private object deserialize(string str) + { + if (str == null || str.Length <= pos) + return new object(); + + int start, end, length; + string stLen; + switch (str[pos]) + { + case 'N': + pos += 2; + return null; + case 'b': + char chBool; + chBool = str[pos + 2]; + pos += 4; + return chBool == '1'; + case 'i': + string stInt; + start = str.IndexOf(":", pos) + 1; + end = str.IndexOf(";", start); + stInt = str.Substring(start, end - start); + pos += 3 + stInt.Length; + return int.Parse(stInt, nfi); + case 'd': + string stDouble; + start = str.IndexOf(":", pos) + 1; + end = str.IndexOf(";", start); + stDouble = str.Substring(start, end - start); + pos += 3 + stDouble.Length; + return double.Parse(stDouble, nfi); + case 's': + start = str.IndexOf(":", pos) + 1; + end = str.IndexOf(":", start); + stLen = str.Substring(start, end - start); + var bytelen = int.Parse(stLen); + length = bytelen; + //This is the byte length, not the character length - so we migth + //need to shorten it before usage. This also implies bounds checking + if (end + 2 + length >= str.Length) length = str.Length - 2 - end; + var stRet = str.Substring(end + 2, length); + while (StringEncoding.GetByteCount(stRet) > bytelen) + { + length--; + stRet = str.Substring(end + 2, length); + } + + pos += 6 + stLen.Length + length; + if (XMLSafe) stRet = stRet.Replace("\n", "\r\n"); + return stRet; + case 'a': + //if keys are ints 0 through N, returns an ArrayList, else returns Hashtable + start = str.IndexOf(":", pos) + 1; + end = str.IndexOf(":", start); + stLen = str.Substring(start, end - start); + length = int.Parse(stLen); + var htRet = new Hashtable(length); + var alRet = new ArrayList(length); + pos += 4 + stLen.Length; //a:Len:{ + for (var i = 0; i < length; i++) + { + //read key + var key = deserialize(str); + //read value + var val = deserialize(str); + + if (alRet != null) + { + if (key is int && (int) key == alRet.Count) + alRet.Add(val); + else + alRet = null; + } + + htRet[key] = val; + } + + pos++; //skip the } + if (pos < str.Length && str[pos] == ';' + ) //skipping our old extra array semi-colon bug (er... php's weirdness) + pos++; + if (alRet != null) + return alRet; + else + return htRet; + default: + return ""; + } //switch + } //unserialzie(object) + } +} //class PhpSerializer \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpEventDeserialize.cs b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpEventDeserialize.cs new file mode 100644 index 0000000..5f0f85b --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpEventDeserialize.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using LaDOSE.DesktopApp.UserControls; + +namespace LaDOSE.DesktopApp.Utils +{ + public class Reservation + { + public string Name { get; set; } + public bool Valid { get; set; } + } + public class WpEventDeserialize + { + public static readonly string[] EventManagerField = new[] { "HR3", "HR2", "COMMENT", "BOOKING_COMMENT" }; + public static List Parse(string meta) + { + if (meta == null) return new List(); + PhpSerializer p = new PhpSerializer(); + var b = p.Deserialize(meta); + Hashtable Wpbook = b as Hashtable; + + var games = new List(); + if (Wpbook != null) + { + Hashtable reg2 = Wpbook["booking"] as Hashtable; + foreach (string reg2Key in reg2.Keys) + { + if (!EventManagerField.Contains(reg2Key.ToUpperInvariant())) + games.Add(new Reservation() + { + Name = reg2Key, + Valid = (string)reg2[reg2Key] == "1" + }); + } + + return games; + } + + return null; + } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs new file mode 100644 index 0000000..40323fc --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Utils/WpfUtil.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Windows; + +namespace LaDOSE.DesktopApp.Utils +{ + public static class WpfUtil + { + public static void AddUI(this ICollection collection, T item) + { + Action addMethod = collection.Add; + Application.Current.Dispatcher.BeginInvoke(addMethod, item); + } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/GameViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/GameViewModel.cs index e41f4e6..7cfdfc9 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/GameViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/GameViewModel.cs @@ -7,18 +7,54 @@ namespace LaDOSE.DesktopApp.ViewModels { public class GameViewModel : Screen { + public override string DisplayName => "Games"; + + private Game _currentGame; + private List _games; private RestService RestService { get; set; } public GameViewModel(RestService restService) { this.RestService = restService; + this.Games=new List(); } public void LoadGames() { + this.Games.Clear(); this.Games = this.RestService.GetGames(); NotifyOfPropertyChange("Games"); } - public List Games { get; set; } + public List Games + { + get => _games; + set + { + _games = value; + NotifyOfPropertyChange(()=>this.Games); + } + } + + public Game CurrentGame + { + get => _currentGame; + set + { + _currentGame = value; + NotifyOfPropertyChange(()=>CurrentGame); + } + } + + public void Update() + { + this.RestService.UpdateGame(this.CurrentGame); + this.Games = RestService.GetGames(); + } + public void AddGame() + { + var item = new Game(); + this.Games.Add(item); + this.CurrentGame = item; + } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs index 515d1a8..ae1f107 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs @@ -3,8 +3,16 @@ using LaDOSE.DesktopApp.Services; namespace LaDOSE.DesktopApp.ViewModels { - public class ShellViewModel : Conductor.Collection.OneActive + public class ShellViewModel : Conductor.Collection.AllActive { + protected override void OnInitialize() + { + var wordPressViewModel = new WordPressViewModel(IoC.Get()); + ActivateItem(wordPressViewModel); + base.OnInitialize(); + + + } public void LoadEvent() { @@ -14,5 +22,6 @@ namespace LaDOSE.DesktopApp.ViewModels { ActivateItem(new GameViewModel(IoC.Get())); } + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs index b17baf6..b75491e 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs @@ -1,24 +1,184 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Input; +using System.Windows.Threading; using Caliburn.Micro; using LaDOSE.DesktopApp.Services; +using LaDOSE.DesktopApp.Utils; using LaDOSE.DTO; +using Action = System.Action; namespace LaDOSE.DesktopApp.ViewModels { public class WordPressViewModel : Screen { + public override string DisplayName => "Events"; + private WPEvent _selectedWpEvent; + private Game _selectedGame; + private ObservableCollection _players; + private ObservableCollection _playersOptions; + private RestService RestService { get; set; } + public WordPressViewModel(RestService restService) { this.RestService = restService; + Players = new ObservableCollection(); + PlayersOptions = new ObservableCollection(); } public void LoadEvents() { - this.Events = this.RestService.GetEvents(); - NotifyOfPropertyChange("Events"); + Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait; + var tsk = Task.Factory.StartNew(Load); + tsk.ContinueWith(t => + { + MessageBox.Show(t.Exception.InnerException.Message); + Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow; + }, + CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, + TaskScheduler.FromCurrentSynchronizationContext()); } + private void Load() + { + GamesFound = new ObservableCollection(); + this.Games = this.RestService.GetGames(); + this.Events = this.RestService.GetEvents(); + + NotifyOfPropertyChange("Events"); + Application.Current.Dispatcher.Invoke(() => + System.Windows.Input.Mouse.OverrideCursor = null); + } + + + public bool CanGenerate + { + get { return SelectedWpEvent != null && SelectedGame != null; } + } + + public List Events { get; set; } + + public WPEvent SelectedWpEvent + { + get => _selectedWpEvent; + set + { + _selectedWpEvent = value; + SelectedGame = null; + ParseGame(_selectedWpEvent); + + } + } + + public Game SelectedGame + { + get => _selectedGame; + set + { + _selectedGame = value; + + Players.Clear(); + PlayersOptions.Clear(); + + Task.Factory.StartNew(LoadPlayers,TaskCreationOptions.LongRunning).ContinueWith(t => + { + + }, + CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, + TaskScheduler.FromCurrentSynchronizationContext()); + NotifyOfPropertyChange(() => SelectedGame); + NotifyOfPropertyChange(() => this.CanGenerate); + NotifyOfPropertyChange(() => Players); + NotifyOfPropertyChange(() => PlayersOptions); + } + } + + + + public ObservableCollection Players + { + get => _players; + set + { + _players = value; + NotifyOfPropertyChange(()=>Players); + } + } + + public ObservableCollection PlayersOptions + { + get => _playersOptions; + set + { + _playersOptions = value; + NotifyOfPropertyChange(() => PlayersOptions); + } + } + + + public ObservableCollection GamesFound { get; set; } + public List Games { get; set; } + + private void ParseGame(WPEvent selectedWpEvent) + { + var reservation = SelectedWpEvent.WpBookings.FirstOrDefault(); + var games = WpEventDeserialize.Parse(reservation.Meta); + GamesFound.Clear(); + + if (games != null) + { + foreach (string wpTag in games.Select(e => e.Name)) + { + var foundGame = Games.FirstOrDefault(e => + e.WordPressTag != null && e.WordPressTag.Split(';').Contains(wpTag)); + if (foundGame != null) + { + if (!GamesFound.Contains(foundGame)) + { + GamesFound.Add(foundGame); + } + } + } + } + + NotifyOfPropertyChange(() => GamesFound); + } + + private void LoadPlayers() + { + + if (SelectedWpEvent != null) + if (SelectedGame != null) + { + this.RestService.GetUsers(SelectedWpEvent.Id, SelectedGame.Id).ForEach((e) => this.Players.AddUI(e)); + this.RestService.GetUsersOptions(SelectedWpEvent.Id, SelectedGame.Id).ForEach((e) => this.PlayersOptions.AddUI(e)); + + } + + } + + public void UpdateDb() + { + if (this.RestService.RefreshDb()) + MessageBox.Show("DataBaseUpdated"); + else + MessageBox.Show("Update Failed"); + } + + public void Generate() + { + if (this.RestService.CreateChallonge(SelectedGame.Id, SelectedWpEvent.Id)) + MessageBox.Show("Challonge Created"); + else + MessageBox.Show("Didn't worl :("); + } + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Views/GameView.xaml b/LaDOSE.Src/LaDOSE.DesktopApp/Views/GameView.xaml index e84e698..535d051 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/GameView.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/GameView.xaml @@ -14,17 +14,47 @@ - + + - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Views/ShellView.xaml b/LaDOSE.Src/LaDOSE.DesktopApp/Views/ShellView.xaml index f4d9e52..47290be 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/ShellView.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/ShellView.xaml @@ -35,8 +35,18 @@ - - + + + + + + + - + + + + + + + + + - - - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Views/WpView.xaml.cs b/LaDOSE.Src/LaDOSE.DesktopApp/Views/WpView.xaml.cs index 8ceab74..31063f6 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/WpView.xaml.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/WpView.xaml.cs @@ -24,5 +24,7 @@ namespace LaDOSE.DesktopApp.Views { InitializeComponent(); } + + } } diff --git a/LaDOSE.Src/LaDOSE.Entity/Game.cs b/LaDOSE.Src/LaDOSE.Entity/Game.cs index b4b0d92..2b2ccba 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Game.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Game.cs @@ -8,7 +8,8 @@ namespace LaDOSE.Entity { public string Name { get; set; } public string ImgUrl { get; set; } - + public string WordPressTag { get; set; } + public string WordPressTagOs { get; set; } public virtual IEnumerable Seasons { get; set; } public virtual IEnumerable Events { get; set; } diff --git a/LaDOSE.Src/LaDOSE.Service/Helper/Reservation.cs b/LaDOSE.Src/LaDOSE.Service/Helper/Reservation.cs new file mode 100644 index 0000000..de28305 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Helper/Reservation.cs @@ -0,0 +1,8 @@ +namespace LaDOSE.Business.Helper +{ + public class Reservation + { + public string Name { get; set; } + public bool Valid { get; set; } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Helper/WpEventDeserialize.cs b/LaDOSE.Src/LaDOSE.Service/Helper/WpEventDeserialize.cs new file mode 100644 index 0000000..ba29581 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Helper/WpEventDeserialize.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace LaDOSE.Business.Helper +{ + public static class WpEventDeserialize + { + public static readonly List EventManagerField =new List(new []{"HR3", "HR2", "COMMENT", "BOOKING_COMMENT"}); + public static List Parse(string meta) + { + if (meta == null) return new List(); + PhpSerializer p = new PhpSerializer(); + var b = p.Deserialize(meta); + Hashtable Wpbook = b as Hashtable; + + var games = new List(); + if (Wpbook != null) + { + Hashtable reg2 = Wpbook["booking"] as Hashtable; + foreach (string reg2Key in reg2.Keys) + { + if (!EventManagerField.Contains(reg2Key.ToUpperInvariant())) + games.Add(new Reservation() + { + Name = reg2Key, + Valid = (string)reg2[reg2Key] == "1" + }); + } + + return games; + } + + return null; + } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs index fd06912..e0e2e45 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IBaseService.cs @@ -9,5 +9,7 @@ namespace LaDOSE.Business.Interface T Create(T entity); bool Update(T entity); bool Delete(int id); + + T AddOrUpdate(T entity); } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs index ee6184e..5950328 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs @@ -6,7 +6,7 @@ namespace LaDOSE.Business.Interface { public interface IEventService : IBaseService { - bool CreateChallonge(int eventId, int wpEventId); - List GetBooking(int eventId, int wpEventId, Game game); + + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IUtilService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IUtilService.cs deleted file mode 100644 index 36d1058..0000000 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IUtilService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace LaDOSE.Business.Interface -{ - public interface IUtilService - { - bool UpdateBooking(); - } -} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs index fc6ab75..e5af5eb 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using LaDOSE.Entity; using LaDOSE.Entity.Wordpress; namespace LaDOSE.Business.Interface @@ -6,5 +7,9 @@ namespace LaDOSE.Business.Interface public interface IWordPressService { List GetWpEvent(); + List GetBooking(int wpEventId, Game game); + List GetBookingOptions(int wpEventId, Game game); + bool UpdateBooking(); + bool CreateChallonge(int gameId, int wpEventId); } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs b/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs index d5f6a71..7d6a1f4 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs @@ -3,10 +3,11 @@ using System.Linq; using LaDOSE.Business.Interface; using LaDOSE.Entity.Context; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; namespace LaDOSE.Business.Service { - public class BaseService : IBaseService where T : class + public class BaseService : IBaseService where T : Entity.Context.Entity { protected LaDOSEDbContext _context; @@ -43,5 +44,21 @@ namespace LaDOSE.Business.Service _context.Remove(find); return _context.Entry(find).State == EntityState.Deleted; } + + public virtual T AddOrUpdate(T entity) + { + EntityEntry entityEntry; + if (entity.Id == 0) + { + entityEntry = this._context.Add(entity); + } + else + { + entityEntry = this._context.Update(entity); + } + + this._context.SaveChanges(); + return entityEntry.Entity; + } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs b/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs index 4d2d7f4..2e10da9 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs @@ -15,14 +15,15 @@ namespace LaDOSE.Business.Service { private IChallongeProvider _challongeProvider; - public EventService(LaDOSEDbContext context,IChallongeProvider challongeProvider) : base(context) + public EventService(LaDOSEDbContext context, IChallongeProvider challongeProvider) : base(context) { this._challongeProvider = challongeProvider; } public override Event GetById(int id) { - return _context.Event.Include(e=>e.Season).Include(e=>e.Games).ThenInclude(e=>e.Game).FirstOrDefault(e=>e.Id == id); + return _context.Event.Include(e => e.Season).Include(e => e.Games).ThenInclude(e => e.Game) + .FirstOrDefault(e => e.Id == id); } public override Event Create(Event e) @@ -37,71 +38,5 @@ namespace LaDOSE.Business.Service return eventAdded.Entity; } - public List GetBooking(int eventId, int wpEventId,Game game) - { - - var currentEvent = _context.Event.Include(e => e.Games).ThenInclude(e => e.Game).FirstOrDefault(e => e.Id == eventId); - var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser).Where(e => e.Id == wpEventId).ToList(); - List bookings = currentWpEvent.SelectMany(e => e.WPBookings).ToList(); - List users = new List(); - foreach (var booking in bookings) - { - PhpSerializer p = new PhpSerializer(); - var b = p.Deserialize(booking.Meta); - Hashtable Wpbook = b as Hashtable; - Hashtable reg = Wpbook["registration"] as Hashtable; - Hashtable reg2 = Wpbook["booking"] as Hashtable; - if (reg2.ContainsKey(game.Name) && ((string)reg2[game.Name]) == "1") - { - booking.WPUser.WPBookings = null; - users.Add(booking.WPUser); - } - - } - - return users; - } - public bool CreateChallonge(int eventId,int wpEventId) - { - var currentEvent = _context.Event.Include(e=>e.Games).ThenInclude(e=>e.Game).FirstOrDefault(e=>e.Id == eventId); - var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser).Where(e=>e.Id == wpEventId); - var users = currentWpEvent.SelectMany(e => e.WPBookings.Select(u => u.WPUser)); - - var userNames = users.Select(e => e.Name).Distinct().ToList(); - if (currentEvent != null) - { - var games = currentEvent.Games.Select(e => e.Game); - var s = currentEvent.Date.ToString("MM/dd/yy"); - foreach (var game in games) - { - var url = $"TestDev{game.Id}{game.Name}"; - var name = $"[{s}]Ranking {currentEvent.Name}{game.Name}"; - var tournament = _challongeProvider.CreateTournament(name,url).Result; - var eventGame = currentEvent.Games.FirstOrDefault(e => e.GameId == game.Id); - eventGame.ChallongeId = tournament.id; - eventGame.ChallongeUrl = tournament.url; - foreach (var userName in userNames) - { - try - { - _challongeProvider.AddPlayer(tournament.id, userName); - } - catch - { - Console.WriteLine($"Erreur d ajout sur {userName}" ); - continue; - - } - } - _context.Entry(eventGame).State = EntityState.Modified; - - } - - _context.SaveChanges(); - return true; - } - - return false; - } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/UtilService.cs b/LaDOSE.Src/LaDOSE.Service/Service/UtilService.cs deleted file mode 100644 index 180a73a..0000000 --- a/LaDOSE.Src/LaDOSE.Service/Service/UtilService.cs +++ /dev/null @@ -1,24 +0,0 @@ -using LaDOSE.Business.Interface; -using LaDOSE.Entity.Context; -using Microsoft.EntityFrameworkCore; - -namespace LaDOSE.Business.Service -{ - public class UtilService : IUtilService - { - private LaDOSEDbContext _context; - - public UtilService(LaDOSEDbContext context) - { - _context = context; - } - - public bool UpdateBooking() - { - _context.Database.SetCommandTimeout(60); - _context.Database.ExecuteSqlCommand("call ladoseapi.ImportEvent();"); - _context.Database.SetCommandTimeout(30); - return true; - } - } -} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs b/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs index 37b6c12..8537326 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs @@ -1,22 +1,136 @@ -using System.Collections.Generic; +using System; +using System.Collections; +using System.Collections.Generic; using System.Linq; +using LaDOSE.Business.Helper; using LaDOSE.Business.Interface; +using LaDOSE.Entity; using LaDOSE.Entity.Context; using LaDOSE.Entity.Wordpress; +using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; namespace LaDOSE.Business.Service { public class WordPressService : IWordPressService { private LaDOSEDbContext _context; - public WordPressService(LaDOSEDbContext context) + private IChallongeProvider _challongeProvider; + + public WordPressService(LaDOSEDbContext context, IChallongeProvider challongeProvider) { this._context = context; + this._challongeProvider = challongeProvider; } public List GetWpEvent() { - return _context.Set().ToList(); + var wpEvents = _context.Set().OrderByDescending(e => e.Id).Include(e => e.WPBookings) + .ThenInclude(e => e.WPUser).Where(e => e.WPBookings.Count() != 0).Take(10).ToList(); + return wpEvents; + } + + public bool UpdateBooking() + { + _context.Database.SetCommandTimeout(60); + _context.Database.ExecuteSqlCommand("call ladoseapi.ImportEvent();"); + _context.Database.SetCommandTimeout(30); + return true; + } + public List GetBooking(int wpEventId, Game game) + { + var selectedGameWpId = game.WordPressTag.Split(';'); + var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser).Where(e => e.Id == wpEventId).ToList(); + List bookings = currentWpEvent.SelectMany(e => e.WPBookings).ToList(); + List users = new List(); + foreach (var booking in bookings) + { + var reservations = WpEventDeserialize.Parse(booking.Meta); + if (reservations != null) + { + var gamesReservation = reservations.Where(e => e.Valid).Select(e => e.Name); + if (selectedGameWpId.Any(e => gamesReservation.Contains(e))) + { + users.Add(booking.WPUser); + } + } + } + return users; + } + + public List GetBookingOptions(int wpEventId, Game game) + { + var selectedGameWpId = game.WordPressTagOs.Split(';'); + var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser).Where(e => e.Id == wpEventId).ToList(); + List bookings = currentWpEvent.SelectMany(e => e.WPBookings).ToList(); + List users = new List(); + foreach (var booking in bookings) + { + var reservations = WpEventDeserialize.Parse(booking.Meta); + if (reservations != null) + { + var gamesReservation = reservations.Where(e => e.Valid).Select(e => e.Name); + if (selectedGameWpId.Any(e => gamesReservation.Contains(e))) + { + users.Add(booking.WPUser); + } + } + } + return users; + } + public bool CreateChallonge(int gameId, int wpEventId) + { + var selectedGame = _context.Game.FirstOrDefault(e => e.Id == gameId); + var selectedGameWpId = selectedGame.WordPressTag.Split(';'); + var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser) + .Where(e => e.Id == wpEventId); + var users = currentWpEvent.SelectMany(e => e.WPBookings.Select(u => u.WPUser)); + + + + if (selectedGame != null) + { + + var currentEvent = currentWpEvent.FirstOrDefault(); + var eventDate = currentEvent.Date?.ToString("MM/dd/yy"); + + var remove = currentEvent.Date?.ToString("Mdyy"); + var url = $"{remove}{selectedGame.Id}"; + var name = $"[{eventDate}]Ranking {currentEvent.Name} {selectedGame.Name}"; + var tournament = _challongeProvider.CreateTournament(name, url).Result; + + + foreach (var booking in currentEvent.WPBookings) + { + var reservations = WpEventDeserialize.Parse(booking.Meta); + if (reservations != null) + { + var gamesReservation = reservations.Where(e=>e.Valid).Select(e=>e.Name); + if(selectedGameWpId.Any(e => gamesReservation.Contains(e))) + { + try + { + _challongeProvider.AddPlayer(tournament.id, booking.WPUser.Name); + } + catch + { + Console.WriteLine($"Erreur d ajout sur {booking.WPUser.Name}"); + continue; + } + } + + } + + + + } + + + return true; + } + + + return false; } } } \ No newline at end of file