Polls persist restarts now.

This commit is contained in:
Master Kwoth
2017-10-27 18:39:56 +02:00
parent 2fbb80a2a2
commit 29f97f3732
16 changed files with 2461 additions and 168 deletions

View File

@ -24,6 +24,7 @@ namespace NadekoBot.Core.Services.Database
IWarningsRepository Warnings { get; }
IXpRepository Xp { get; }
IClubRepository Clubs { get; }
IPollsRepository Polls { get; }
int Complete();
Task<int> CompleteAsync();

View File

@ -3,15 +3,16 @@ using System.Collections.Generic;
namespace NadekoBot.Core.Services.Database.Models
{
public class Poll
public class Poll : DbEntity
{
public ulong GuildId { get; set; }
public ulong ChannelId { get; set; }
public string Question { get; set; }
public IndexedCollection<PollAnswer> Answers { get; set; }
public HashSet<PollVote> Votes { get; set; }
public HashSet<PollVote> Votes { get; set; } = new HashSet<PollVote>();
}
public class PollAnswer : IIndexed
public class PollAnswer : DbEntity, IIndexed
{
public int Index { get; set; }
public string Text { get; set; }

View File

@ -1,13 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Core.Services.Database.Models
namespace NadekoBot.Core.Services.Database.Models
{
public class PollVote
public class PollVote : DbEntity
{
public ulong UserId { get; set; }
public int VoteIndex { get; set; }
public override int GetHashCode()
{
return UserId.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is PollVote p
? p.UserId == UserId
: false;
}
}
}

View File

@ -341,6 +341,12 @@ namespace NadekoBot.Core.Services.Database
.WithMany(x => x.Bans);
#endregion
#region Polls
modelBuilder.Entity<Poll>()
.HasIndex(x => x.GuildId)
.IsUnique();
#endregion
}
}
}

View File

@ -0,0 +1,15 @@
using NadekoBot.Core.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Core.Services.Database.Repositories
{
public interface IPollsRepository : IRepository<Poll>
{
IEnumerable<Poll> GetAllPolls();
void RemovePoll(int id);
}
}

View File

@ -0,0 +1,35 @@
using NadekoBot.Core.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Core.Services.Database.Repositories.Impl
{
public class PollsRepository : Repository<Poll>, IPollsRepository
{
public PollsRepository(DbContext context) : base(context)
{
}
public IEnumerable<Poll> GetAllPolls()
{
return _set.Include(x => x.Answers)
.Include(x => x.Votes)
.ToArray();
}
public void RemovePoll(int id)
{
var p = _set
.Include(x => x.Answers)
.Include(x => x.Votes)
.FirstOrDefault(x => x.Id == id);
p.Votes.Clear();
p.Answers.Clear();
_set.Remove(p);
}
}
}

View File

@ -57,6 +57,9 @@ namespace NadekoBot.Core.Services.Database
private IClubRepository _clubs;
public IClubRepository Clubs => _clubs ?? (_clubs = new ClubRepository(_context));
private IPollsRepository _polls;
public IPollsRepository Polls => _polls ?? (_polls = new PollsRepository(_context));
public UnitOfWork(NadekoContext context)
{
_context = context;