From b8d49cea697a400d1c9bf17c72061dcae38b7749 Mon Sep 17 00:00:00 2001 From: Darkstack <1835601+darkstack@users.noreply.github.com> Date: Thu, 28 Mar 2019 00:23:44 +0100 Subject: [PATCH] Todo : Fix format RestService : Fix connection if first connection fail BL : Fix Update/Delete return value --- LaDOSE.Src/LaDOSE.DiscordBot/Command/Todo.cs | 20 ++++++++--- .../LaDOSE.DiscordBot/Service/WebService.cs | 10 +++++- LaDOSE.Src/LaDOSE.REST/RestService.cs | 2 +- .../LaDOSE.Service/Service/BaseService.cs | 36 ++++++++++++++----- .../LaDOSE.Service/Service/TodoService.cs | 21 ++++++++--- 5 files changed, 70 insertions(+), 19 deletions(-) diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Command/Todo.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Command/Todo.cs index d73f284..99c8cd1 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Command/Todo.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Command/Todo.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using System.Threading.Tasks; using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; @@ -38,12 +39,21 @@ namespace LaDOSE.DiscordBot.Command break; case "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}"); + StringBuilder sb = new StringBuilder(); + sb.AppendLine("Todos: "); + if (todoDtos!=null && todoDtos.Count>0) + { + foreach (var task in todoDtos) + { + string taskStatus = task.Done ? ":white_check_mark:" : ":negative_squared_cross_mark:"; + sb.AppendLine($"{task.Id} | {taskStatus} | {task.User} | {task.Task}"); + } } - + else + { + sb.AppendLine("None."); + } + await ctx.RespondAsync(sb.ToString()); break; case "DEL": try diff --git a/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs b/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs index dfbc301..bb087c8 100644 --- a/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs +++ b/LaDOSE.Src/LaDOSE.DiscordBot/Service/WebService.cs @@ -23,7 +23,15 @@ namespace LaDOSE.DiscordBot.Service public WebService(Uri uri,string user,string password) { restService = new RestService(); - restService.Connect(uri,user,password); + try + { + restService.Connect(uri, user, password); + } + catch (Exception) + { + Console.WriteLine("Unable to contact services"); + } + } private void CheckToken() diff --git a/LaDOSE.Src/LaDOSE.REST/RestService.cs b/LaDOSE.Src/LaDOSE.REST/RestService.cs index aed06c0..12e4fc7 100644 --- a/LaDOSE.Src/LaDOSE.REST/RestService.cs +++ b/LaDOSE.Src/LaDOSE.REST/RestService.cs @@ -65,7 +65,7 @@ namespace LaDOSE.REST private void CheckToken() { - if (this.Auth.Expire <= DateTime.Now) + if (this.Auth == null || this.Auth.Expire <= DateTime.Now) { GetToken(this.username,this.password); } diff --git a/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs b/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs index febe803..8ac9606 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/BaseService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using LaDOSE.Business.Interface; using LaDOSE.Entity.Context; @@ -34,17 +35,36 @@ namespace LaDOSE.Business.Service public virtual bool Update(T entity) { - var entityEntry = _context.Update(entity); - this._context.SaveChanges(); - return _context.Entry(entityEntry).State == EntityState.Unchanged; + try + { + + var entityEntry = _context.Update(entity); + this._context.SaveChanges(); + return true; + } + catch (DbUpdateException e) + { + Console.WriteLine(e.Message); + return false; + } } public virtual bool Delete(int id) { - var find = _context.Find(id); - _context.Remove(find); - this._context.SaveChanges(); - return _context.Entry(find).State == EntityState.Deleted; + try + { + var find = _context.Find(id); + if (find == null) return false; + _context.Remove((object) find); + this._context.SaveChanges(); + return true; + } + catch (DbUpdateException e) + { + Console.WriteLine(e.Message); + return false; + } + } public virtual T AddOrUpdate(T entity) diff --git a/LaDOSE.Src/LaDOSE.Service/Service/TodoService.cs b/LaDOSE.Src/LaDOSE.Service/Service/TodoService.cs index f1b858e..9dc81aa 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/TodoService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/TodoService.cs @@ -16,10 +16,23 @@ namespace LaDOSE.Business.Service 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; + try + { + var find = _context.Find(id); + if (find != null) + { + if (find.Deleted.HasValue) + return false; + find.Deleted = DateTime.Now; + } + + this._context.SaveChanges(); + return true; + } + catch (DbUpdateException) + { + return false; + } } public override IEnumerable GetAll()