Todo : Fix format

RestService : Fix connection if first connection fail
BL : Fix Update/Delete return value
This commit is contained in:
2019-03-28 00:23:44 +01:00
parent 8b0820ece4
commit b8d49cea69
5 changed files with 70 additions and 19 deletions

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes; using DSharpPlus.CommandsNext.Attributes;
@@ -38,12 +39,21 @@ namespace LaDOSE.DiscordBot.Command
break; break;
case "LIST": case "LIST":
var todoDtos = dep.WebService.RestService.GetTodos(); var todoDtos = dep.WebService.RestService.GetTodos();
StringBuilder sb = new StringBuilder();
sb.AppendLine("Todos: ");
if (todoDtos!=null && todoDtos.Count>0)
{
foreach (var task in todoDtos) foreach (var task in todoDtos)
{ {
string taskStatus = task.Done ? ":white_check_mark:" : ":negative_squared_cross_mark:"; string taskStatus = task.Done ? ":white_check_mark:" : ":negative_squared_cross_mark:";
await ctx.RespondAsync($"{task?.Id} - {task?.Task} Par : {task?.User} Etat : {taskStatus}"); sb.AppendLine($"{task.Id} | {taskStatus} | {task.User} | {task.Task}");
} }
}
else
{
sb.AppendLine("None.");
}
await ctx.RespondAsync(sb.ToString());
break; break;
case "DEL": case "DEL":
try try

View File

@@ -23,7 +23,15 @@ namespace LaDOSE.DiscordBot.Service
public WebService(Uri uri,string user,string password) public WebService(Uri uri,string user,string password)
{ {
restService = new RestService(); 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() private void CheckToken()

View File

@@ -65,7 +65,7 @@ namespace LaDOSE.REST
private void CheckToken() private void CheckToken()
{ {
if (this.Auth.Expire <= DateTime.Now) if (this.Auth == null || this.Auth.Expire <= DateTime.Now)
{ {
GetToken(this.username,this.password); GetToken(this.username,this.password);
} }

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using LaDOSE.Business.Interface; using LaDOSE.Business.Interface;
using LaDOSE.Entity.Context; using LaDOSE.Entity.Context;
@@ -34,17 +35,36 @@ namespace LaDOSE.Business.Service
public virtual bool Update(T entity) public virtual bool Update(T entity)
{ {
try
{
var entityEntry = _context.Update(entity); var entityEntry = _context.Update(entity);
this._context.SaveChanges(); this._context.SaveChanges();
return _context.Entry(entityEntry).State == EntityState.Unchanged; return true;
}
catch (DbUpdateException e)
{
Console.WriteLine(e.Message);
return false;
}
} }
public virtual bool Delete(int id) public virtual bool Delete(int id)
{
try
{ {
var find = _context.Find<T>(id); var find = _context.Find<T>(id);
_context.Remove(find); if (find == null) return false;
_context.Remove((object) find);
this._context.SaveChanges(); this._context.SaveChanges();
return _context.Entry(find).State == EntityState.Deleted; return true;
}
catch (DbUpdateException e)
{
Console.WriteLine(e.Message);
return false;
}
} }
public virtual T AddOrUpdate(T entity) public virtual T AddOrUpdate(T entity)

View File

@@ -15,11 +15,24 @@ namespace LaDOSE.Business.Service
} }
public override bool Delete(int id) public override bool Delete(int id)
{
try
{ {
var find = _context.Find<Todo>(id); var find = _context.Find<Todo>(id);
if (find != null)
{
if (find.Deleted.HasValue)
return false;
find.Deleted = DateTime.Now; find.Deleted = DateTime.Now;
}
this._context.SaveChanges(); this._context.SaveChanges();
return _context.Entry(find).State == EntityState.Modified; return true;
}
catch (DbUpdateException)
{
return false;
}
} }
public override IEnumerable<Todo> GetAll() public override IEnumerable<Todo> GetAll()