From 2fe08f938ab3b91124a6a7f74067dd12ab0f2cb3 Mon Sep 17 00:00:00 2001 From: Darkstack <1835601+darkstack@users.noreply.github.com> Date: Sat, 16 Mar 2019 12:22:52 +0100 Subject: [PATCH] REST to a .Net Standard Library Test REST in discord bot and WPF Small improvements --- .../Controllers/WordPressController.cs | 29 ++- LaDOSE.Src/LaDOSE.DTO/GameDTO.cs | 1 + .../Behaviors/TextBoxInputRegExBehaviour.cs | 173 ++++++++++++++++++ LaDOSE.Src/LaDOSE.DesktopApp/Bootstrapper.cs | 14 +- .../LaDOSE.DesktopApp.csproj | 13 +- .../ViewModels/GameViewModel.cs | 13 +- .../ViewModels/ShellViewModel.cs | 13 +- .../ViewModels/WordPressViewModel.cs | 19 +- .../LaDOSE.DesktopApp/Views/GameView.xaml | 24 ++- LaDOSE.Src/LaDOSE.DesktopApp/packages.config | 2 +- .../LaDOSE.DiscordBot/Command/Result.cs | 68 ++++--- LaDOSE.Src/LaDOSE.DiscordBot/Dependencies.cs | 2 +- .../LaDOSE.DiscordBot.csproj | 4 + LaDOSE.Src/LaDOSE.DiscordBot/Program.cs | 7 +- .../LaDOSE.DiscordBot/Service/WebService.cs | 41 +++++ LaDOSE.Src/LaDOSE.Entity/Game.cs | 1 + LaDOSE.Src/LaDOSE.REST/LaDOSE.REST.csproj | 15 ++ .../Services => LaDOSE.REST}/RestService.cs | 30 ++- .../Interface/IWordPressService.cs | 1 + .../LaDOSE.Service/Service/GameService.cs | 16 +- .../Service/WordPressService.cs | 6 + LaDOSE.Src/LaDOSE.sln | 9 + 22 files changed, 423 insertions(+), 78 deletions(-) create mode 100644 LaDOSE.Src/LaDOSE.DesktopApp/Behaviors/TextBoxInputRegExBehaviour.cs create mode 100644 LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs create mode 100644 LaDOSE.Src/LaDOSE.REST/LaDOSE.REST.csproj rename LaDOSE.Src/{LaDOSE.DesktopApp/Services => LaDOSE.REST}/RestService.cs (87%) diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/WordPressController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/WordPressController.cs index 4733ad7..335fc21 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/WordPressController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/WordPressController.cs @@ -14,16 +14,16 @@ namespace LaDOSE.Api.Controllers public class WordPressController : Controller { public IGameService GameService { get; } + private IWordPressService _service; // GET - public WordPressController(IWordPressService service, IGameService gameService ) + public WordPressController(IWordPressService service, IGameService gameService) { GameService = gameService; _service = service; } - [HttpGet("WPEvent")] public List Event() @@ -31,30 +31,44 @@ namespace LaDOSE.Api.Controllers var wpEvents = _service.GetWpEvent(); foreach (var wpEvent in wpEvents) { - foreach (var wpEventWpBooking in wpEvent.WPBookings) { wpEventWpBooking.WPEvent = null; wpEventWpBooking.WPUser.WPBookings = null; } } + return Mapper.Map>(wpEvents); } + + [HttpGet("NextEvent")] + public WPEventDTO NextEvent() + { + var wpEvents = _service.GetNextWpEvent(); + + + foreach (var wpEventWpBooking in wpEvents.WPBookings) + { + wpEventWpBooking.WPEvent = null; + wpEventWpBooking.WPUser.WPBookings = null; + } + + return Mapper.Map(wpEvents); + } [HttpGet("GetUsers/{wpEventId}/{gameId}")] public List GetUsers(int wpEventId, int gameId) { var game = GameService.GetById(gameId); return Mapper.Map>(_service.GetBooking(wpEventId, game)); - } + [HttpGet("GetUsersOptions/{wpEventId}/{gameId}")] public List GetUsersOptions(int wpEventId, int gameId) { var game = GameService.GetById(gameId); return Mapper.Map>(_service.GetBookingOptions(wpEventId, game)); - } @@ -62,17 +76,16 @@ namespace LaDOSE.Api.Controllers public bool UpdateDb() { return _service.UpdateBooking(); - } [HttpGet("CreateChallonge/{gameId:int}/{wpEventId:int}")] public string CreateChallonge(int gameId, int wpEventId) { - return _service.CreateChallonge(gameId, wpEventId,null); + return _service.CreateChallonge(gameId, wpEventId, null); } [HttpPost("CreateChallonge/{gameId:int}/{wpEventId:int}")] - public string CreateChallonge(int gameId, int wpEventId, [FromBody]List additionalPlayer) + public string CreateChallonge(int gameId, int wpEventId, [FromBody] List additionalPlayer) { return _service.CreateChallonge(gameId, wpEventId, additionalPlayer); } diff --git a/LaDOSE.Src/LaDOSE.DTO/GameDTO.cs b/LaDOSE.Src/LaDOSE.DTO/GameDTO.cs index 309c233..68c9757 100644 --- a/LaDOSE.Src/LaDOSE.DTO/GameDTO.cs +++ b/LaDOSE.Src/LaDOSE.DTO/GameDTO.cs @@ -4,6 +4,7 @@ { public int Id { get; set; } public string Name { get; set; } + public int Order { get; set; } public string ImgUrl { get; set; } public string WordPressTag { get; set; } public string WordPressTagOs { get; set; } diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Behaviors/TextBoxInputRegExBehaviour.cs b/LaDOSE.Src/LaDOSE.DesktopApp/Behaviors/TextBoxInputRegExBehaviour.cs new file mode 100644 index 0000000..2e38ab6 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Behaviors/TextBoxInputRegExBehaviour.cs @@ -0,0 +1,173 @@ +using System; +using System.Text.RegularExpressions; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using System.Windows.Interactivity; + +namespace LaDOSE.DesktopApp.Behaviors +{ + public class TextBoxInputRegExBehaviour : Behavior + { + #region DependencyProperties + public static readonly DependencyProperty RegularExpressionProperty = + DependencyProperty.Register("RegularExpression", typeof(string), typeof(TextBoxInputRegExBehaviour), new FrameworkPropertyMetadata(".*")); + + public string RegularExpression + { + get { return (string)GetValue(RegularExpressionProperty); } + set { SetValue(RegularExpressionProperty, value); } + } + + public static readonly DependencyProperty MaxLengthProperty = + DependencyProperty.Register("MaxLength", typeof(int), typeof(TextBoxInputRegExBehaviour), + new FrameworkPropertyMetadata(int.MinValue)); + + public int MaxLength + { + get { return (int)GetValue(MaxLengthProperty); } + set { SetValue(MaxLengthProperty, value); } + } + + public static readonly DependencyProperty EmptyValueProperty = + DependencyProperty.Register("EmptyValue", typeof(string), typeof(TextBoxInputRegExBehaviour), null); + + public string EmptyValue + { + get { return (string)GetValue(EmptyValueProperty); } + set { SetValue(EmptyValueProperty, value); } + } + #endregion + + /// + /// Attach our behaviour. Add event handlers + /// + protected override void OnAttached() + { + base.OnAttached(); + + AssociatedObject.PreviewTextInput += PreviewTextInputHandler; + AssociatedObject.PreviewKeyDown += PreviewKeyDownHandler; + DataObject.AddPastingHandler(AssociatedObject, PastingHandler); + } + + /// + /// Deattach our behaviour. remove event handlers + /// + protected override void OnDetaching() + { + base.OnDetaching(); + + AssociatedObject.PreviewTextInput -= PreviewTextInputHandler; + AssociatedObject.PreviewKeyDown -= PreviewKeyDownHandler; + DataObject.RemovePastingHandler(AssociatedObject, PastingHandler); + } + + #region Event handlers [PRIVATE] -------------------------------------- + + void PreviewTextInputHandler(object sender, TextCompositionEventArgs e) + { + string text; + if (this.AssociatedObject.Text.Length < this.AssociatedObject.CaretIndex) + text = this.AssociatedObject.Text; + else + { + // Remaining text after removing selected text. + string remainingTextAfterRemoveSelection; + + text = TreatSelectedText(out remainingTextAfterRemoveSelection) + ? remainingTextAfterRemoveSelection.Insert(AssociatedObject.SelectionStart, e.Text) + : AssociatedObject.Text.Insert(this.AssociatedObject.CaretIndex, e.Text); + } + + e.Handled = !ValidateText(text); + } + + /// + /// PreviewKeyDown event handler + /// + void PreviewKeyDownHandler(object sender, KeyEventArgs e) + { + if (string.IsNullOrEmpty(this.EmptyValue)) + return; + + string text = null; + + // Handle the Backspace key + if (e.Key == Key.Back) + { + if (!this.TreatSelectedText(out text)) + { + if (AssociatedObject.SelectionStart > 0) + text = this.AssociatedObject.Text.Remove(AssociatedObject.SelectionStart - 1, 1); + } + } + // Handle the Delete key + else if (e.Key == Key.Delete) + { + // If text was selected, delete it + if (!this.TreatSelectedText(out text) && this.AssociatedObject.Text.Length > AssociatedObject.SelectionStart) + { + // Otherwise delete next symbol + text = this.AssociatedObject.Text.Remove(AssociatedObject.SelectionStart, 1); + } + } + + if (text == string.Empty) + { + this.AssociatedObject.Text = this.EmptyValue; + if (e.Key == Key.Back) + AssociatedObject.SelectionStart++; + e.Handled = true; + } + } + + private void PastingHandler(object sender, DataObjectPastingEventArgs e) + { + if (e.DataObject.GetDataPresent(DataFormats.Text)) + { + string text = Convert.ToString(e.DataObject.GetData(DataFormats.Text)); + + if (!ValidateText(text)) + e.CancelCommand(); + } + else + e.CancelCommand(); + } + #endregion Event handlers [PRIVATE] ----------------------------------- + + #region Auxiliary methods [PRIVATE] ----------------------------------- + + /// + /// Validate certain text by our regular expression and text length conditions + /// + /// Text for validation + /// True - valid, False - invalid + private bool ValidateText(string text) + { + return (new Regex(this.RegularExpression, RegexOptions.IgnoreCase)).IsMatch(text) && (MaxLength == int.MinValue || text.Length <= MaxLength); + } + + /// + /// Handle text selection + /// + /// true if the character was successfully removed; otherwise, false. + private bool TreatSelectedText(out string text) + { + text = null; + if (AssociatedObject.SelectionLength <= 0) + return false; + + var length = this.AssociatedObject.Text.Length; + if (AssociatedObject.SelectionStart >= length) + return true; + + if (AssociatedObject.SelectionStart + AssociatedObject.SelectionLength >= length) + AssociatedObject.SelectionLength = length - AssociatedObject.SelectionStart; + + text = this.AssociatedObject.Text.Remove(AssociatedObject.SelectionStart, AssociatedObject.SelectionLength); + return true; + } + #endregion Auxiliary methods [PRIVATE] -------------------------------- + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Bootstrapper.cs b/LaDOSE.Src/LaDOSE.DesktopApp/Bootstrapper.cs index 0e92fc0..2dd5391 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Bootstrapper.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Bootstrapper.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.Configuration; using System.Windows; using Caliburn.Micro; -using LaDOSE.DesktopApp.Services; + using LaDOSE.DesktopApp.ViewModels; +using LaDOSE.REST; namespace LaDOSE.DesktopApp { @@ -18,14 +20,19 @@ namespace LaDOSE.DesktopApp protected override void Configure() { + container = new SimpleContainer(); container.Singleton(); - - container.PerRequest(); container.Singleton(); + + container.PerRequest(); + + + } + protected override void OnStartup(object sender, StartupEventArgs e) { DisplayRootViewFor(); @@ -44,6 +51,7 @@ namespace LaDOSE.DesktopApp protected override void BuildUp(object instance) { container.BuildUp(instance); + } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj b/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj index 4f21d37..b6a044e 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj +++ b/LaDOSE.Src/LaDOSE.DesktopApp/LaDOSE.DesktopApp.csproj @@ -66,8 +66,8 @@ ..\packages\Caliburn.Micro.3.2.0\lib\net45\Caliburn.Micro.Platform.Core.dll - - ..\packages\RestSharp.106.6.5\lib\net452\RestSharp.dll + + ..\packages\RestSharp.106.6.9\lib\net452\RestSharp.dll @@ -99,12 +99,12 @@ MSBuild:Compile Designer + - BookingUserControl.xaml @@ -189,6 +189,10 @@ {61201da6-1bc9-4ba1-ac45-70104d391ecd} LaDOSE.DTO + + {692c2a72-ab7e-4502-bed8-aa2afa1761cb} + LaDOSE.REST + @@ -208,5 +212,8 @@ false + + + \ 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 e2ceb47..7bb3513 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/GameViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/GameViewModel.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using System.Linq; using Caliburn.Micro; -using LaDOSE.DesktopApp.Services; using LaDOSE.DTO; +using LaDOSE.REST; namespace LaDOSE.DesktopApp.ViewModels { @@ -16,19 +17,20 @@ namespace LaDOSE.DesktopApp.ViewModels { this.RestService = restService; this.Games=new List(); - + } protected override void OnInitialize() { LoadGames(); + this.CurrentGame = Games.First(); base.OnInitialize(); } public void LoadGames() { - this.Games.Clear(); - this.Games = this.RestService.GetGames(); + var gameDtos = this.RestService.GetGames().OrderBy(e=>e.Order).ToList(); + this.Games = gameDtos; NotifyOfPropertyChange("Games"); } @@ -56,7 +58,8 @@ namespace LaDOSE.DesktopApp.ViewModels public void Update() { this.RestService.UpdateGame(this.CurrentGame); - this.Games = RestService.GetGames(); + LoadGames(); + } public void AddGame() { diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs index 4b020cc..a967143 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs @@ -1,8 +1,9 @@ using System; +using System.Configuration; using System.Windows; using System.Windows.Media.Imaging; using Caliburn.Micro; -using LaDOSE.DesktopApp.Services; +using LaDOSE.REST; namespace LaDOSE.DesktopApp.ViewModels { @@ -15,6 +16,16 @@ namespace LaDOSE.DesktopApp.ViewModels this.DisplayName = "LaDOSE"; this.AppIcon = BitmapFrame.Create(Application.GetResourceStream(new Uri("/LaDOSE.DesktopApp;component/Resources/64x64.png", UriKind.RelativeOrAbsolute)).Stream); + + var appSettings = ConfigurationManager.AppSettings; + string url = (string)appSettings["ApiUri"]; + string user = (string)appSettings["ApiUser"]; + string password = (string)appSettings["ApiPassword"]; + Uri uri = new Uri(url); + var restService = IoC.Get(); + + restService.Connect(uri, user, password); + var wordPressViewModel = new WordPressViewModel(IoC.Get()); ActivateItem(wordPressViewModel); base.OnInitialize(); diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs index ada6330..575b9e3 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/WordPressViewModel.cs @@ -8,9 +8,9 @@ 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 LaDOSE.REST; using Action = System.Action; namespace LaDOSE.DesktopApp.ViewModels @@ -169,7 +169,7 @@ namespace LaDOSE.DesktopApp.ViewModels var reservation = SelectedWpEvent.WpBookings.FirstOrDefault(); var games = WpEventDeserialize.Parse(reservation.Meta); GamesFound.Clear(); - + var foundGames = new List(); if (games != null) { foreach (string wpTag in games.Select(e => e.Name)) @@ -178,14 +178,20 @@ namespace LaDOSE.DesktopApp.ViewModels e.WordPressTag != null && e.WordPressTag.Split(';').Contains(wpTag)); if (foundGame != null) { - if (!GamesFound.Contains(foundGame)) + if (!foundGames.Contains(foundGame)) { - GamesFound.Add(foundGame); + foundGames.Add(foundGame); } } } } + var orderedEnumerable = foundGames.OrderBy(e => e.Order); + foreach (var gameDto in orderedEnumerable) + { + GamesFound.Add(gameDto); + } + NotifyOfPropertyChange(() => GamesFound); } @@ -209,8 +215,9 @@ namespace LaDOSE.DesktopApp.ViewModels System.Windows.Input.Mouse.OverrideCursor = Cursors.Wait); GamesFound = new ObservableCollection(); this.Games = this.RestService.GetGames(); - this.Events = this.RestService.GetEvents(); - + var events = this.RestService.GetEvents(); + events.ForEach(e => e.WpBookings = e.WpBookings.OrderBy(x => x.WpUser.Name).ToList()); + this.Events = events; NotifyOfPropertyChange("Events"); Application.Current.Dispatcher.Invoke(() => System.Windows.Input.Mouse.OverrideCursor = null); diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Views/GameView.xaml b/LaDOSE.Src/LaDOSE.DesktopApp/Views/GameView.xaml index d843241..b96944b 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/GameView.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/GameView.xaml @@ -4,6 +4,8 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:LaDOSE.DesktopApp.Views" + 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"> @@ -19,7 +21,7 @@ - + @@ -37,6 +39,7 @@ + @@ -51,11 +54,20 @@ - - - - - + + + + + + + + + + + + + + diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/packages.config b/LaDOSE.Src/LaDOSE.DesktopApp/packages.config index d5fb30e..013ed69 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/packages.config +++ b/LaDOSE.Src/LaDOSE.DesktopApp/packages.config @@ -2,6 +2,6 @@ - + \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Command/Result.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Command/Result.cs index c1985d4..f78c0c5 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Command/Result.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Command/Result.cs @@ -4,32 +4,50 @@ using DSharpPlus.CommandsNext.Attributes; namespace LaDOSE.DiscordBot.Command { + internal class Result + { + Dependencies dep; - internal class Result + public Result(Dependencies d) { - Dependencies dep; - public Result(Dependencies d) - { - this.dep = d; - } - - - [RequireRolesAttribute("Staff")] - [Command("update")] - public async Task UpdateAsync(CommandContext ctx) - { - var tournament = await dep.ChallongeService.GetLastTournament(); - await ctx.RespondAsync($"Mise à jour effectuée"); - - } - - [Command("last")] - public async Task LastAsync(CommandContext ctx) - { - var lastTournamentMessage = dep.ChallongeService.GetLastTournamentMessage(); - await ctx.RespondAsync(lastTournamentMessage); - - } + this.dep = d; } - + + + [RequireRolesAttribute("Staff")] + [Command("update")] + public async Task UpdateAsync(CommandContext ctx) + { + var tournament = await dep.ChallongeService.GetLastTournament(); + await ctx.RespondAsync($"Mise à jour effectuée"); + } + + [Command("last")] + public async Task LastAsync(CommandContext ctx) + { + var lastTournamentMessage = dep.ChallongeService.GetLastTournamentMessage(); + await ctx.RespondAsync(lastTournamentMessage); + } + + [RequireRolesAttribute("Staff")] + [Command("inscriptions")] + public async Task InscriptionsAsync(CommandContext ctx) + { + await ctx.TriggerTypingAsync(); + var inscrits = dep.WebService.GetInscrits(); + await ctx.RespondAsync(inscrits); + } + + [RequireRolesAttribute("Staff")] + [Command("UpdateDb")] + public async Task UpdateDbAsync(CommandContext ctx) + { + await ctx.RespondAsync("Mise à jour des inscriptions en cours..."); + await ctx.TriggerTypingAsync(); + + var status = dep.WebService.RefreshDb() ? "Ok" : "erreur"; + + await ctx.RespondAsync($"Status: {status}"); + } + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Dependencies.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Dependencies.cs index 7877c2e..f9cbf58 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Dependencies.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Dependencies.cs @@ -10,6 +10,6 @@ namespace LaDOSE.DiscordBot internal CancellationTokenSource Cts { get; set; } public ChallongeService ChallongeService { get; set; } public TodoService TodoService { get; set; } - + public WebService WebService { get; set; } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/LaDOSE.DiscordBot.csproj b/LaDOSE.Src/LaDOSE.DiscordBot/LaDOSE.DiscordBot.csproj index 2c13af2..9861ab2 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/LaDOSE.DiscordBot.csproj +++ b/LaDOSE.Src/LaDOSE.DiscordBot/LaDOSE.DiscordBot.csproj @@ -13,6 +13,10 @@ + + + + ..\..\Library\ChallongeCSharpDriver.dll diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Program.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Program.cs index 84a978b..0b7f7df 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Program.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Program.cs @@ -31,7 +31,9 @@ namespace LaDOSE.DiscordBot var discordToken = builder["Discord:Token"].ToString(); var challongeToken = builder["Challonge:Token"].ToString(); - + var restUrl = builder["REST:Url"].ToString(); + var restUser = builder["REST:User"].ToString(); + var restPassword = builder["REST:Password"].ToString(); Console.WriteLine($"LaDOSE.Net Discord Bot"); @@ -42,7 +44,7 @@ namespace LaDOSE.DiscordBot TokenType = TokenType.Bot }); - + var webService = new WebService(new Uri(restUrl),restUser,restPassword); var challongeService = new ChallongeService(challongeToken); var todoService = new TodoService(); var cts = new CancellationTokenSource(); @@ -56,6 +58,7 @@ namespace LaDOSE.DiscordBot Cts = cts, ChallongeService = challongeService, TodoService = todoService, + WebService = webService }); dep = d.Build(); } diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs new file mode 100644 index 0000000..258f1e8 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using ChallongeCSharpDriver; +using ChallongeCSharpDriver.Caller; +using ChallongeCSharpDriver.Core.Queries; +using ChallongeCSharpDriver.Core.Results; +using ChallongeCSharpDriver.Main; +using ChallongeCSharpDriver.Main.Objects; +using LaDOSE.DTO; +using LaDOSE.REST; + +namespace LaDOSE.DiscordBot.Service +{ + public class WebService + { + private RestService restService; + + public WebService(Uri uri,string user,string password) + { + restService = new RestService(); + restService.Connect(uri,user,password); + } + + + + public String GetInscrits() + { + var wpEventDto = restService.GetNextEvent(); + var wpBookingDtos = wpEventDto.WpBookings; + List player= new List(); + wpBookingDtos.OrderBy(e=>e.WpUser.Name).ToList().ForEach(e=> player.Add(e.WpUser.Name)); + return $"Les Joueurs inscrits pour {wpEventDto.Name} {string.Join(", ", player)}"; + } + public bool RefreshDb() + { + return restService.RefreshDb(); + } + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Entity/Game.cs b/LaDOSE.Src/LaDOSE.Entity/Game.cs index 2b2ccba..5f8f6f1 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Game.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Game.cs @@ -8,6 +8,7 @@ namespace LaDOSE.Entity { public string Name { get; set; } public string ImgUrl { get; set; } + public int Order { get; set; } public string WordPressTag { get; set; } public string WordPressTagOs { get; set; } public virtual IEnumerable Seasons { get; set; } diff --git a/LaDOSE.Src/LaDOSE.REST/LaDOSE.REST.csproj b/LaDOSE.Src/LaDOSE.REST/LaDOSE.REST.csproj new file mode 100644 index 0000000..3f17b47 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.REST/LaDOSE.REST.csproj @@ -0,0 +1,15 @@ + + + + netstandard2.0 + + + + + + + + + + + diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Services/RestService.cs b/LaDOSE.Src/LaDOSE.REST/RestService.cs similarity index 87% rename from LaDOSE.Src/LaDOSE.DesktopApp/Services/RestService.cs rename to LaDOSE.Src/LaDOSE.REST/RestService.cs index e1e02dc..8b742bb 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Services/RestService.cs +++ b/LaDOSE.Src/LaDOSE.REST/RestService.cs @@ -1,32 +1,25 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; -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 +namespace LaDOSE.REST { public class RestService { + public RestClient Client { get; set; } - public RestService() + public RestService() { } + + + public void Connect(Uri url, string user, string password) { -#if DEBUG - MessageBox.Show("WAIT"); -#endif - 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 = user, password = password }); + restRequest.AddJsonBody(new {username = user, password = password}); var response = Client.Post(restRequest); if (response.IsSuccessful) { @@ -36,8 +29,7 @@ namespace LaDOSE.DesktopApp.Services } else { - MessageBox.Show("Unable to contact services, i m useless, BYEKTHX","Error",MessageBoxButton.OK,MessageBoxImage.Error); - Application.Current.Shutdown(-1); + throw new Exception("unable to contact services"); } } @@ -103,6 +95,12 @@ namespace LaDOSE.DesktopApp.Services var restResponse = Client.Get>(restRequest); return restResponse.Data; } + public WPEventDTO GetNextEvent() + { + var restRequest = new RestRequest("/api/wordpress/NextEvent", Method.GET); + var restResponse = Client.Get(restRequest); + return restResponse.Data; + } public string CreateChallonge(int gameId, int eventId) diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs index 252fee3..4ec4879 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IWordPressService.cs @@ -6,6 +6,7 @@ namespace LaDOSE.Business.Interface { public interface IWordPressService { + WPEvent GetNextWpEvent(); List GetWpEvent(); List GetBooking(int wpEventId, Game game); List GetBookingOptions(int wpEventId, Game game); diff --git a/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs b/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs index cbe5ed2..9c2300b 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/GameService.cs @@ -15,11 +15,25 @@ namespace LaDOSE.Business.Service { } + public override Game AddOrUpdate(Game entity) + { + if (entity.Order == 0) + { + entity.Order = GetNextFreeOrder(); + } + + return base.AddOrUpdate(entity); + } + public override IEnumerable GetAll() { return _context.Game.Include(e => e.Seasons).ThenInclude(e=>e.Season).ToList(); } - + public int GetNextFreeOrder() + { + int nextFreeOrder = _context.Game.Max(e => e.Order); + return ++nextFreeOrder; + } } } \ 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 4cf17b3..5a0130e 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs @@ -30,6 +30,12 @@ namespace LaDOSE.Business.Service .ThenInclude(e => e.WPUser).Where(e => e.WPBookings.Count() != 0).Take(10).ToList(); return wpEvents; } + public WPEvent GetNextWpEvent() + { + var wpEvents = _context.Set().OrderByDescending(e=>e.Date).ThenByDescending(e => e.Id) + .Include(e => e.WPBookings).ThenInclude(e => e.WPUser).FirstOrDefault(e => Enumerable.Count(e.WPBookings) != 0); + return wpEvents; + } public bool UpdateBooking() { diff --git a/LaDOSE.Src/LaDOSE.sln b/LaDOSE.Src/LaDOSE.sln index 5055821..d03425b 100644 --- a/LaDOSE.Src/LaDOSE.sln +++ b/LaDOSE.Src/LaDOSE.sln @@ -23,6 +23,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Bot", "Bot", "{8B9C38FB-2A8 EndProject Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "Setup", "Setup\Setup.vdproj", "{6825F5F4-EBB5-469F-B935-5B916EA37ACC}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Utils", "Utils", "{2A0E1491-8E15-4062-ABE7-C04AE9655515}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LaDOSE.REST", "LaDOSE.REST\LaDOSE.REST.csproj", "{692C2A72-AB7E-4502-BED8-AA2AFA1761CB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -55,6 +59,10 @@ Global {61201DA6-1BC9-4BA1-AC45-70104D391ECD}.Release|Any CPU.Build.0 = Release|Any CPU {6825F5F4-EBB5-469F-B935-5B916EA37ACC}.Debug|Any CPU.ActiveCfg = Debug {6825F5F4-EBB5-469F-B935-5B916EA37ACC}.Release|Any CPU.ActiveCfg = Release + {692C2A72-AB7E-4502-BED8-AA2AFA1761CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {692C2A72-AB7E-4502-BED8-AA2AFA1761CB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {692C2A72-AB7E-4502-BED8-AA2AFA1761CB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {692C2A72-AB7E-4502-BED8-AA2AFA1761CB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -67,6 +75,7 @@ Global {A76AC851-4D43-4BF9-9034-F496888ADAFD} = {DD52FE00-B822-4DE3-B369-2BFB5F808130} {61201DA6-1BC9-4BA1-AC45-70104D391ECD} = {6FC9438E-D93E-4E63-9342-F8A966EE2D06} {6825F5F4-EBB5-469F-B935-5B916EA37ACC} = {DD52FE00-B822-4DE3-B369-2BFB5F808130} + {692C2A72-AB7E-4502-BED8-AA2AFA1761CB} = {2A0E1491-8E15-4062-ABE7-C04AE9655515} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {D47DEDD0-C906-439D-81E4-D86BBE723B8C}