Bot Event
This commit is contained in:
12
LaDOSE.Src/LaDOSE.Service/Interface/IBotEventService.cs
Normal file
12
LaDOSE.Src/LaDOSE.Service/Interface/IBotEventService.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using LaDOSE.Entity.BotEvent;
|
||||
|
||||
namespace LaDOSE.Business.Interface
|
||||
{
|
||||
public interface IBotEventService : IBaseService<BotEvent>
|
||||
{
|
||||
BotEvent GetLastEvent();
|
||||
|
||||
bool CreateEvent(string EventName);
|
||||
bool SetResult(string discordId, string name, bool present);
|
||||
}
|
||||
}
|
||||
55
LaDOSE.Src/LaDOSE.Service/Service/BotEventService.cs
Normal file
55
LaDOSE.Src/LaDOSE.Service/Service/BotEventService.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using LaDOSE.Business.Interface;
|
||||
using LaDOSE.Entity;
|
||||
using LaDOSE.Entity.BotEvent;
|
||||
using LaDOSE.Entity.Context;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LaDOSE.Business.Service
|
||||
{
|
||||
public class BotEventService : BaseService<BotEvent>, IBotEventService
|
||||
{
|
||||
public BotEventService(LaDOSEDbContext context) : base(context)
|
||||
{
|
||||
this._context = context;
|
||||
|
||||
}
|
||||
|
||||
public bool CreateEvent(string EventName)
|
||||
{
|
||||
this._context.BotEvent.Add(new BotEvent { Name = EventName, Date = DateTime.Now });
|
||||
return this._context.SaveChanges()!=0;
|
||||
}
|
||||
|
||||
public BotEvent GetLastEvent()
|
||||
{
|
||||
return this._context.BotEvent.Include(e=>e.Results).FirstOrDefault(e => e.Date == this._context.BotEvent.Max(e => e.Date));
|
||||
}
|
||||
|
||||
public bool SetResult(string discordId, string name, bool present)
|
||||
{
|
||||
if (string.IsNullOrEmpty(discordId))
|
||||
throw new Exception("DiscordId invalid.");
|
||||
|
||||
BotEvent currentEvent = this.GetLastEvent();
|
||||
|
||||
if(currentEvent == null)
|
||||
{
|
||||
throw new Exception("Oups");
|
||||
}
|
||||
BotEventResult res = currentEvent.Results.FirstOrDefault(e => e.DiscordId == discordId);
|
||||
if (res != null)
|
||||
{
|
||||
res.Result = present;
|
||||
_context.BotEventResult.Update(res);
|
||||
_context.SaveChanges();
|
||||
return true;
|
||||
}
|
||||
|
||||
_context.BotEventResult.Add(new BotEventResult() { BotEventId = currentEvent.Id, DiscordId = discordId, Result = present, Name = name });
|
||||
return _context.SaveChanges()!=0;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user