diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/TodoController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/TodoController.cs new file mode 100644 index 0000000..2c7843b --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/TodoController.cs @@ -0,0 +1,22 @@ +using LaDOSE.Business.Interface; +using LaDOSE.DTO; +using LaDOSE.Entity; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace LaDOSE.Api.Controllers +{ + [Authorize] + [Route("api/[controller]")] + [Produces("application/json")] + public class TodoController : GenericControllerDTO + { + public TodoController(ITodoService service) : base(service) + { + + } + + + + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/UsersController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/UsersController.cs index f0efb58..a356ff4 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/UsersController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/UsersController.cs @@ -6,6 +6,7 @@ using System.Security.Claims; using System.Text; using System.Threading.Tasks; using LaDOSE.Business.Interface; +using LaDOSE.DTO; using LaDOSE.Entity; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -50,20 +51,21 @@ namespace LaDOSE.Api.Controllers { new Claim(ClaimTypes.Name, user.Id.ToString()) }), - Expires = DateTime.UtcNow.AddDays(7), + Expires = DateTime.UtcNow.AddMinutes(16), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); var tokenString = tokenHandler.WriteToken(token); // return basic user info (without password) and token to store client side - return Ok(new + return Ok(new ApplicationUserDTO { Id = user.Id, Username = user.Username, FirstName = user.FirstName, LastName = user.LastName, - Token = tokenString + Token = tokenString, + Expire = token.ValidTo }); } diff --git a/LaDOSE.Src/LaDOSE.Api/Startup.cs b/LaDOSE.Src/LaDOSE.Api/Startup.cs index 669e865..c94e7cf 100644 --- a/LaDOSE.Src/LaDOSE.Api/Startup.cs +++ b/LaDOSE.Src/LaDOSE.Api/Startup.cs @@ -96,6 +96,7 @@ namespace LaDOSE.Api // return unauthorized if user no longer exists context.Fail("Unauthorized"); } + return Task.CompletedTask; } }; @@ -117,9 +118,10 @@ namespace LaDOSE.Api cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); - cfg.CreateMap(); + cfg.CreateMap(); cfg.CreateMap().ForMember(e=>e.Meta,opt=>opt.MapFrom(s=>s.Meta.CleanWpMeta())); cfg.CreateMapTwoWay(); + cfg.CreateMapTwoWay(); }); } @@ -133,6 +135,7 @@ namespace LaDOSE.Api services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } diff --git a/LaDOSE.Src/LaDOSE.DTO/ApplicationUser.cs b/LaDOSE.Src/LaDOSE.DTO/ApplicationUserDTO.cs similarity index 75% rename from LaDOSE.Src/LaDOSE.DTO/ApplicationUser.cs rename to LaDOSE.Src/LaDOSE.DTO/ApplicationUserDTO.cs index 077e440..30ba9a0 100644 --- a/LaDOSE.Src/LaDOSE.DTO/ApplicationUser.cs +++ b/LaDOSE.Src/LaDOSE.DTO/ApplicationUserDTO.cs @@ -1,7 +1,9 @@  +using System; + namespace LaDOSE.DTO { - public class ApplicationUser + public class ApplicationUserDTO { public int Id { get; set; } public string FirstName { get; set; } @@ -10,6 +12,7 @@ namespace LaDOSE.DTO public string Password { get; set; } public string Token { get; set; } + public DateTime Expire { get; set; } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DTO/TodoDTO.cs b/LaDOSE.Src/LaDOSE.DTO/TodoDTO.cs new file mode 100644 index 0000000..71f7dc9 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.DTO/TodoDTO.cs @@ -0,0 +1,15 @@ +using System; + +namespace LaDOSE.DTO +{ + public class TodoDTO + { + public int Id { get; set; } + public string User { get; set; } + public string Task { get; set; } + public bool Done { get; set; } + public DateTime Created { get; set; } + public DateTime? Deleted { get; set; } + + } +} \ 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 a967143..19b8148 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs +++ b/LaDOSE.Src/LaDOSE.DesktopApp/ViewModels/ShellViewModel.cs @@ -4,28 +4,40 @@ using System.Windows; using System.Windows.Media.Imaging; using Caliburn.Micro; using LaDOSE.REST; +using LaDOSE.REST.Event; namespace LaDOSE.DesktopApp.ViewModels { public class ShellViewModel : Conductor.Collection.AllActive { - - + private string _user; + + public string User + { + get => _user; + set + { + _user = value; + NotifyOfPropertyChange(User); + } + } + protected override void OnInitialize() { this.DisplayName = "LaDOSE"; + this.User = "Test"; 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.UpdatedJwtEvent += TokenUpdate; restService.Connect(uri, user, password); - var wordPressViewModel = new WordPressViewModel(IoC.Get()); ActivateItem(wordPressViewModel); base.OnInitialize(); @@ -33,6 +45,12 @@ namespace LaDOSE.DesktopApp.ViewModels } + private void TokenUpdate(object sender, UpdatedJwtEventHandler e) + { + this.User = e.Message.FirstName; + } + + public BitmapFrame AppIcon { get; set; } public void LoadEvent() diff --git a/LaDOSE.Src/LaDOSE.DesktopApp/Views/ShellView.xaml b/LaDOSE.Src/LaDOSE.DesktopApp/Views/ShellView.xaml index 72bd338..52533b3 100644 --- a/LaDOSE.Src/LaDOSE.DesktopApp/Views/ShellView.xaml +++ b/LaDOSE.Src/LaDOSE.DesktopApp/Views/ShellView.xaml @@ -72,8 +72,10 @@ - - + + User : + + diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Command/Result.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Command/Result.cs index f78c0c5..e031b32 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Command/Result.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Command/Result.cs @@ -14,18 +14,18 @@ namespace LaDOSE.DiscordBot.Command } - [RequireRolesAttribute("Staff")] - [Command("update")] - public async Task UpdateAsync(CommandContext ctx) - { - var tournament = await dep.ChallongeService.GetLastTournament(); - await ctx.RespondAsync($"Mise à jour effectuée"); - } + //[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(); + var lastTournamentMessage = dep.WebService.GetLastChallonge(); await ctx.RespondAsync(lastTournamentMessage); } diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Command/Todo.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Command/Todo.cs index 2e0c720..d73f284 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Command/Todo.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Command/Todo.cs @@ -1,6 +1,9 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; +using DSharpPlus.Interactivity; +using LaDOSE.DTO; namespace LaDOSE.DiscordBot.Command { @@ -17,22 +20,98 @@ namespace LaDOSE.DiscordBot.Command public async Task TodoAsync(CommandContext ctx, string command,params string[] todo) { await ctx.TriggerTypingAsync(); + string args = string.Join(" ",todo); switch (command.ToUpperInvariant()) { case "ADD": - dep.TodoService.Add(todo[0]); + + var todoDto = new TodoDTO + { + Created = DateTime.Now, + Done = false, + Deleted = null, + Task = args, + User = ctx.User.Username, + }; + dep.WebService.RestService.UpdateTodo(todoDto); + //dep.WebService.RestService.UpdateGame(); break; case "LIST": - await ctx.RespondAsync($"{dep.TodoService.List()}"); + var todoDtos = dep.WebService.RestService.GetTodos(); + foreach (var task in todoDtos) + { + string taskStatus = task.Done ? ":white_check_mark:" : ":negative_squared_cross_mark:"; + await ctx.RespondAsync($"{task?.Id} - {task?.Task} Par : {task?.User} Etat : {taskStatus}"); + } + break; case "DEL": - int id; - if (int.TryParse(todo[0], out id)) + try { - await ctx.RespondAsync($"{dep.TodoService.Delete(id)}"); - break; - }; - await ctx.RespondAsync($"invalid id"); + int id = int.Parse(todo[0]); + await ctx.RespondAsync(dep.WebService.RestService.DeleteTodo(id) ? $"Deleted" : $"Error"); + } + catch (Exception e) + { + await ctx.RespondAsync($"Error {e.Message}"); + return; + } + break; + case "V": + try + { + int id = int.Parse(todo[0]); + var todoById = dep.WebService.RestService.GetTodoById(id); + todoById.Done = true; + dep.WebService.RestService.UpdateTodo(todoById); + await ctx.RespondAsync($"Done : {todoById.Id} - {todoById.Task}"); + } + catch (Exception e) + { + await ctx.RespondAsync($"Error {e.Message}"); + return; + } + break; + case "X": + try + { + int id = int.Parse(todo[0]); + var todoById = dep.WebService.RestService.GetTodoById(id); + todoById.Done = false; + dep.WebService.RestService.UpdateTodo(todoById); + await ctx.RespondAsync($"Undone : {todoById.Id} - {todoById.Task}"); + } + catch (Exception e) + { + await ctx.RespondAsync($"Erreur {e.Message}"); + return; + } + break; + case "TRUNC": + try + { + + var todos = dep.WebService.RestService.GetTodos(); + await ctx.RespondAsync($"Sure ? (Y/N)"); + var interactivity = ctx.Client.GetInteractivityModule(); + var waitForMessageAsync = await interactivity.WaitForMessageAsync(xm => xm.Content.Contains("Y")||xm.Content.Contains("N"), TimeSpan.FromSeconds(10)); + if (waitForMessageAsync!= null) + { + if (waitForMessageAsync.Message.Content == "Y") + { + foreach (var task in todos) + { + dep.WebService.RestService.DeleteTodo(task.Id); + await ctx.RespondAsync($"Deleted - {task.Id}"); + } + } + } + } + catch (Exception e) + { + await ctx.RespondAsync($"Erreur {e.Message}"); + return; + } break; } diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Dependencies.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Dependencies.cs index f9cbf58..c331360 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Dependencies.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Dependencies.cs @@ -8,8 +8,6 @@ namespace LaDOSE.DiscordBot { internal InteractivityModule Interactivity { get; set; } 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/Program.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Program.cs index 0b7f7df..032293e 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Program.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Program.cs @@ -16,7 +16,7 @@ namespace LaDOSE.DiscordBot class Program { static DiscordClient discord; - + static InteractivityModule Interactivity { get; set; } static void Main(string[] args) { MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult(); @@ -44,9 +44,20 @@ namespace LaDOSE.DiscordBot TokenType = TokenType.Bot }); + discord.UseInteractivity(new InteractivityConfiguration + { + // default pagination behaviour to just ignore the reactions + PaginationBehaviour = TimeoutBehaviour.Ignore, + + // default pagination timeout to 5 minutes + PaginationTimeout = TimeSpan.FromMinutes(5), + + // default timeout for other actions to 2 minutes + Timeout = TimeSpan.FromMinutes(2) + }); var webService = new WebService(new Uri(restUrl),restUser,restPassword); - var challongeService = new ChallongeService(challongeToken); - var todoService = new TodoService(); + //var challongeService = new ChallongeService(challongeToken); + var cts = new CancellationTokenSource(); DependencyCollection dep = null; @@ -54,10 +65,8 @@ namespace LaDOSE.DiscordBot { d.AddInstance(new Dependencies() { - Cts = cts, - ChallongeService = challongeService, - TodoService = todoService, + //ChallongeService = challongeService, WebService = webService }); dep = d.Build(); diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Service/ChallongeService.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Service/ChallongeService.cs deleted file mode 100644 index 22198c9..0000000 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Service/ChallongeService.cs +++ /dev/null @@ -1,72 +0,0 @@ -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; - -namespace LaDOSE.DiscordBot.Service -{ - public class ChallongeService - { - private ChallongeConfig Config; - public string ApiKey { get; set; } - - public ChallongeHTTPClientAPICaller ApiCaller { get; set; } - - public string DernierTournois { get; set; } - - - public ChallongeService(string apiKey) - { - this.ApiKey = apiKey; - this.Config = new ChallongeConfig(this.ApiKey); - this.ApiCaller = new ChallongeHTTPClientAPICaller(Config); - DernierTournois = "Aucun tournois."; - } - - - public async Task GetLastTournament() - { - try - { - - - List tournamentResultList = await new TournamentsQuery() - { - state = TournamentState.Ended - } - .call(this.ApiCaller); - - - var lastDate = tournamentResultList.Max(e => e.completed_at); - if (lastDate.HasValue) - { - var lastRankingDate = new DateTime(lastDate.Value.Year, lastDate.Value.Month, lastDate.Value.Day); - - var lastTournament = tournamentResultList.Where(e => e.completed_at > lastRankingDate).ToList(); - string returnValue = "Les derniers tournois : \n"; - foreach (var tournamentResult in lastTournament) - { - returnValue += $"{tournamentResult.name} : \n"; - } - - DernierTournois = returnValue; - } - return true; - } - catch - { - return false; - } - } - public string GetLastTournamentMessage() - { - return DernierTournois; - } - } -} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Service/TodoService.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Service/TodoService.cs deleted file mode 100644 index 3299155..0000000 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Service/TodoService.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.IO; - -namespace LaDOSE.DiscordBot.Service -{ - public class TodoService - { - private const string db = "todo.txt"; - - public TodoService() - { - } - - public bool Add(string text) - { - if (!string.IsNullOrEmpty(text)) { - using (var textWriter =File.AppendText(db)) - { - textWriter.WriteLine(text); - } - return true; - } - - return false; - - } - public bool Delete(int id) - { - string returnText = ""; - var text = File.ReadAllText(db); - var i = 0; - foreach (var line in text.Split('\n')) - { - ++i; - if (i != id) - { - returnText += $"{line}\n"; - } - - } - - File.WriteAllText(db,returnText); - return true; - - } - - public string List() - { - string returnText = ""; - var text = File.ReadAllText(db); - var i = 0; - foreach (var line in text.Split()) - { - if(!string.IsNullOrEmpty(line)) - returnText += $"{++i}. {line}"; - } - - return returnText; - } - } -} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs index 258f1e8..dfbc301 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs @@ -10,6 +10,7 @@ using ChallongeCSharpDriver.Main; using ChallongeCSharpDriver.Main.Objects; using LaDOSE.DTO; using LaDOSE.REST; +using RestSharp.Authenticators; namespace LaDOSE.DiscordBot.Service { @@ -17,12 +18,20 @@ namespace LaDOSE.DiscordBot.Service { private RestService restService; + public RestService RestService => restService; + public WebService(Uri uri,string user,string password) { restService = new RestService(); restService.Connect(uri,user,password); } + private void CheckToken() + { + + + } + public String GetInscrits() @@ -37,5 +46,10 @@ namespace LaDOSE.DiscordBot.Service { return restService.RefreshDb(); } + + public string GetLastChallonge() + { + return restService.GetLastChallonge(); + } } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs b/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs index 8c3e385..a97575e 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs @@ -9,6 +9,7 @@ namespace LaDOSE.Entity.Context public DbSet ApplicationUser { get; set; } public DbSet Season { get; set; } public DbSet Event { get; set; } + public DbSet Todo { get; set; } #region WordPress public DbSet WPUser { get; set; } diff --git a/LaDOSE.Src/LaDOSE.Entity/Game.cs b/LaDOSE.Src/LaDOSE.Entity/Game.cs index 5f8f6f1..20afa2d 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Game.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Game.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace LaDOSE.Entity diff --git a/LaDOSE.Src/LaDOSE.Entity/Todo.cs b/LaDOSE.Src/LaDOSE.Entity/Todo.cs new file mode 100644 index 0000000..89c4ccc --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Entity/Todo.cs @@ -0,0 +1,14 @@ +using System; + +namespace LaDOSE.Entity +{ + public class Todo : Context.Entity + { + public string User { get; set; } + public string Task { get; set; } + public bool Done { get; set; } + public DateTime Created { get; set; } + public DateTime? Deleted { get; set; } + + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.REST/Event/UpdatedEvent.cs b/LaDOSE.Src/LaDOSE.REST/Event/UpdatedEvent.cs new file mode 100644 index 0000000..2f28667 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.REST/Event/UpdatedEvent.cs @@ -0,0 +1,16 @@ +using System; +using LaDOSE.DTO; + +namespace LaDOSE.REST.Event +{ + public class UpdatedJwtEventHandler : EventArgs + { + private ApplicationUserDTO msg; + public UpdatedJwtEventHandler(ApplicationUserDTO applicationUser) + { + this.msg = applicationUser; + } + + public ApplicationUserDTO Message => msg; + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.REST/RestService.cs b/LaDOSE.Src/LaDOSE.REST/RestService.cs index 8b742bb..aed06c0 100644 --- a/LaDOSE.Src/LaDOSE.REST/RestService.cs +++ b/LaDOSE.Src/LaDOSE.REST/RestService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using LaDOSE.DTO; +using LaDOSE.REST.Event; using RestSharp; using RestSharp.Authenticators; using RestSharp.Serialization.Json; @@ -9,23 +10,43 @@ namespace LaDOSE.REST { public class RestService { + private string username { get; set; } + private string password { get; set; } + private ApplicationUserDTO Auth { get; set; } + + public string UserName => Auth?.FirstName; + + public DateTime ValidUntil => Auth.Expire; + //private ApplicationUserDTO token = null; public RestClient Client { get; set; } + public event EventHandler UpdatedJwtEvent; + public RestService() { } public void Connect(Uri url, string user, string password) { Client = new RestClient(url); + this.username = user; + this.password = password; + GetToken(user, password); + } + + private void GetToken(string user, string password) + { var restRequest = new RestRequest("users/auth", Method.POST); restRequest.AddJsonBody(new {username = user, password = password}); + var response = Client.Post(restRequest); if (response.IsSuccessful) { JsonDeserializer d = new JsonDeserializer(); - var applicationUser = d.Deserialize(response); + var applicationUser = d.Deserialize(response); + this.Auth = applicationUser; Client.Authenticator = new JwtAuthenticator($"{applicationUser.Token}"); + RaiseUpdatedJwtEvent(new UpdatedJwtEventHandler(this.Auth)); } else { @@ -33,6 +54,23 @@ namespace LaDOSE.REST } } + private void RaiseUpdatedJwtEvent(UpdatedJwtEventHandler auth) + { + EventHandler handler = UpdatedJwtEvent; + + handler?.Invoke(this, auth); + + } + + + private void CheckToken() + { + if (this.Auth.Expire <= DateTime.Now) + { + GetToken(this.username,this.password); + } + } + #region PostFix private T Post(string resource,T entity) @@ -91,32 +129,43 @@ namespace LaDOSE.REST #region WordPress public List GetEvents() { + CheckToken(); var restRequest = new RestRequest("/api/wordpress/WPEvent", Method.GET); var restResponse = Client.Get>(restRequest); return restResponse.Data; } public WPEventDTO GetNextEvent() { + CheckToken(); var restRequest = new RestRequest("/api/wordpress/NextEvent", Method.GET); var restResponse = Client.Get(restRequest); return restResponse.Data; } + public string GetLastChallonge() + { + CheckToken(); + var restRequest = new RestRequest($"/api/wordpress/GetLastChallonge/", Method.GET); + var restResponse = Client.Get(restRequest); + return restResponse.Content; + } public string CreateChallonge(int gameId, int eventId) { + CheckToken(); var restRequest = new RestRequest($"/api/wordpress/CreateChallonge/{gameId}/{eventId}", Method.GET); var restResponse = Client.Get(restRequest); return restResponse.Content; } public string CreateChallonge2(int gameId, int eventId, List optionalPlayers) { - + CheckToken(); var restResponse = Post,string>($"/api/wordpress/CreateChallonge/{gameId}/{eventId}",optionalPlayers); return restResponse; } public bool RefreshDb() { + CheckToken(); var restRequest = new RestRequest("/api/Wordpress/UpdateDb", Method.GET); var restResponse = Client.Get(restRequest); return restResponse.Data; @@ -124,6 +173,7 @@ namespace LaDOSE.REST public List GetUsers(int wpEventId, int gameId) { + CheckToken(); var restRequest = new RestRequest($"/api/Wordpress/GetUsers/{wpEventId}/{gameId}", Method.GET); var restResponse = Client.Get>(restRequest); return restResponse.Data; @@ -131,6 +181,7 @@ namespace LaDOSE.REST public List GetUsersOptions(int wpEventId, int gameId) { + CheckToken(); var restRequest = new RestRequest($"/api/Wordpress/GetUsersOptions/{wpEventId}/{gameId}", Method.GET); var restResponse = Client.Get>(restRequest); return restResponse.Data; @@ -142,6 +193,7 @@ namespace LaDOSE.REST #region Games public List GetGames() { + CheckToken(); var restRequest = new RestRequest("/api/Game", Method.GET); var restResponse = Client.Get>(restRequest); return restResponse.Data; @@ -149,10 +201,12 @@ namespace LaDOSE.REST public GameDTO UpdateGame(GameDTO game) { + CheckToken(); return Post("Api/Game", game); } public bool DeleteGame(int gameId) { + CheckToken(); var restRequest = new RestRequest($"/api/Game/{gameId}", Method.DELETE); var restResponse = Client.Execute(restRequest); return restResponse.IsSuccessful; @@ -164,7 +218,37 @@ namespace LaDOSE.REST #endregion + #region Todo + public List GetTodos() + { + CheckToken(); + var restRequest = new RestRequest("/api/Todo", Method.GET); + var restResponse = Client.Get>(restRequest); + return restResponse.Data; + } + public TodoDTO GetTodoById(int id) + { + CheckToken(); + var restRequest = new RestRequest($"/api/Todo/{id}", Method.GET); + var restResponse = Client.Get(restRequest); + return restResponse.Data; + } + public TodoDTO UpdateTodo(TodoDTO Todo) + { + CheckToken(); + return Post("Api/Todo", Todo); + } + public bool DeleteTodo(int todoId) + { + CheckToken(); + var restRequest = new RestRequest($"/api/Todo/{todoId}", Method.DELETE); + var restResponse = Client.Execute(restRequest); + return restResponse.IsSuccessful; + } + + + #endregion diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IChallongeProvider.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IChallongeProvider.cs index d6803f4..ca436ad 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IChallongeProvider.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IChallongeProvider.cs @@ -6,7 +6,7 @@ namespace LaDOSE.Business.Interface { public interface IChallongeProvider { - Task GetLastTournament(); + Task GetLastTournament(); string GetLastTournamentMessage(); Task CreateTournament(string name, string url); Task AddPlayer(int tournamentId, string userName); diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/ITodoService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/ITodoService.cs new file mode 100644 index 0000000..6c87bb7 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Interface/ITodoService.cs @@ -0,0 +1,9 @@ +using LaDOSE.Entity; + +namespace LaDOSE.Business.Interface +{ + public interface ITodoService : IBaseService + { + + } +} \ 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 4ec4879..034d726 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 System.Threading.Tasks; using LaDOSE.Entity; using LaDOSE.Entity.Wordpress; @@ -12,5 +13,7 @@ namespace LaDOSE.Business.Interface List GetBookingOptions(int wpEventId, Game game); bool UpdateBooking(); string CreateChallonge(int gameId, int wpEventId, IList additionPlayers); + + Task GetLastChallonge(); } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Provider/ChallongeProvider.cs b/LaDOSE.Src/LaDOSE.Service/Provider/ChallongeProvider.cs index ca5e6b0..30588d7 100644 --- a/LaDOSE.Src/LaDOSE.Service/Provider/ChallongeProvider.cs +++ b/LaDOSE.Src/LaDOSE.Service/Provider/ChallongeProvider.cs @@ -45,8 +45,9 @@ namespace LaDOSE.Business.Provider } - public async Task GetLastTournament() + public async Task GetLastTournament() { + string dernierTournois = null; try { @@ -59,6 +60,7 @@ namespace LaDOSE.Business.Provider var lastDate = tournamentResultList.Max(e => e.completed_at); + if (lastDate.HasValue) { var lastRankingDate = new DateTime(lastDate.Value.Year, lastDate.Value.Month, lastDate.Value.Day); @@ -70,13 +72,13 @@ namespace LaDOSE.Business.Provider returnValue += $"{tournamentResult.name} : \n"; } - DernierTournois = returnValue; + dernierTournois = returnValue; } - return true; + return dernierTournois; } catch { - return false; + return dernierTournois; } } public string GetLastTournamentMessage() diff --git a/LaDOSE.Src/LaDOSE.Service/Service/TodoService.cs b/LaDOSE.Src/LaDOSE.Service/Service/TodoService.cs new file mode 100644 index 0000000..f1b858e --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Service/TodoService.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using LaDOSE.Business.Interface; +using LaDOSE.Entity; +using LaDOSE.Entity.Context; +using Microsoft.EntityFrameworkCore; + +namespace LaDOSE.Business.Service +{ + public class TodoService : BaseService, ITodoService + { + public TodoService(LaDOSEDbContext context) : base(context) + { + } + + public override bool Delete(int id) + { + var find = _context.Find(id); + find.Deleted = DateTime.Now; + this._context.SaveChanges(); + return _context.Entry(find).State == EntityState.Modified; + } + + public override IEnumerable GetAll() + { + return _context.Set().Where(e=>e.Deleted == null).ToList(); + } + } +} \ 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 5a0130e..aa68316 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/WordPressService.cs @@ -1,7 +1,12 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; +using System.Threading.Tasks; +using ChallongeCSharpDriver; +using ChallongeCSharpDriver.Core.Queries; +using ChallongeCSharpDriver.Core.Results; using LaDOSE.Business.Helper; using LaDOSE.Business.Interface; using LaDOSE.Entity; @@ -154,6 +159,12 @@ namespace LaDOSE.Business.Service return "error while creating challonge"; } + public async Task GetLastChallonge() + { + var lastTournament = await _challongeProvider.GetLastTournament(); + return lastTournament; + } + private string FormatCurrentEventName(string currentEventName) {