Bot Event

This commit is contained in:
2022-07-30 18:22:49 +02:00
parent bfcca7132c
commit c0d469eac6
18 changed files with 2884 additions and 190 deletions

View 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);
}
}

View 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;
}
}
}