Removed poll repository

This commit is contained in:
Kwoth
2021-06-18 08:34:03 +02:00
parent dbf51a8a1b
commit ae59bac118
7 changed files with 17 additions and 32 deletions

View File

@@ -3,6 +3,7 @@ using Discord;
using NadekoBot.Core.Services.Database.Models; using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Core.Services; using NadekoBot.Core.Services;
using System; using System;
using System.Linq;
using System.Threading; using System.Threading;
namespace NadekoBot.Modules.Games.Common namespace NadekoBot.Modules.Games.Common
@@ -57,7 +58,7 @@ namespace NadekoBot.Modules.Games.Common
finally { _locker.Release(); } finally { _locker.Release(); }
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
var trackedPoll = uow.Polls.GetById(Poll.Id); var trackedPoll = uow._context.Poll.FirstOrDefault(x => x.Id == Poll.Id);
trackedPoll.Votes.Add(voteObj); trackedPoll.Votes.Add(voteObj);
uow.SaveChanges(); uow.SaveChanges();
} }

View File

@@ -31,7 +31,7 @@ namespace NadekoBot.Modules.Games.Services
using (var uow = db.GetDbContext()) using (var uow = db.GetDbContext())
{ {
ActivePolls = uow.Polls.GetAllPolls() ActivePolls = uow._context.Poll.GetAllPolls()
.ToDictionary(x => x.GuildId, x => .ToDictionary(x => x.GuildId, x =>
{ {
var pr = new PollRunner(db, x); var pr = new PollRunner(db, x);
@@ -70,7 +70,7 @@ namespace NadekoBot.Modules.Games.Services
{ {
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
uow.Polls.Add(p); uow._context.Poll.Add(p);
uow.SaveChanges(); uow.SaveChanges();
} }
@@ -87,7 +87,7 @@ namespace NadekoBot.Modules.Games.Services
pr.OnVoted -= Pr_OnVoted; pr.OnVoted -= Pr_OnVoted;
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
uow.Polls.RemovePoll(pr.Poll.Id); uow._context.RemovePoll(pr.Poll.Id);
uow.SaveChanges(); uow.SaveChanges();
return pr.Poll; return pr.Poll;

View File

@@ -18,7 +18,6 @@ namespace NadekoBot.Core.Services.Database
IDiscordUserRepository DiscordUsers { get; } IDiscordUserRepository DiscordUsers { get; }
IWarningsRepository Warnings { get; } IWarningsRepository Warnings { get; }
IXpRepository Xp { get; } IXpRepository Xp { get; }
IPollsRepository Polls { get; }
IPlantedCurrencyRepository PlantedCurrency { get; } IPlantedCurrencyRepository PlantedCurrency { get; }
int SaveChanges(); int SaveChanges();

View File

@@ -57,6 +57,7 @@ namespace NadekoBot.Core.Services.Database
public DbSet<DiscordUser> DiscordUser { get; set; } public DbSet<DiscordUser> DiscordUser { get; set; }
public DbSet<MusicPlayerSettings> MusicPlayerSettings { get; set; } public DbSet<MusicPlayerSettings> MusicPlayerSettings { get; set; }
public DbSet<Repeater> Repeaters { get; set; } public DbSet<Repeater> Repeaters { get; set; }
public DbSet<Poll> Poll { get; set; }
public NadekoContext(DbContextOptions<NadekoContext> options) : base(options) public NadekoContext(DbContextOptions<NadekoContext> options) : base(options)
{ {

View File

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

View File

@@ -2,25 +2,23 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services.Database;
namespace NadekoBot.Core.Services.Database.Repositories.Impl namespace NadekoBot.Modules.Games.Common
{ {
public class PollsRepository : Repository<Poll>, IPollsRepository public static class PollExtensions
{ {
public PollsRepository(DbContext context) : base(context) public static IEnumerable<Poll> GetAllPolls(this DbSet<Poll> polls)
{ {
} return polls.Include(x => x.Answers)
public IEnumerable<Poll> GetAllPolls()
{
return _set.Include(x => x.Answers)
.Include(x => x.Votes) .Include(x => x.Votes)
.ToArray(); .ToArray();
} }
public void RemovePoll(int id) public static void RemovePoll(this NadekoContext ctx, int id)
{ {
var p = _set var p = ctx
.Poll
.Include(x => x.Answers) .Include(x => x.Answers)
.Include(x => x.Votes) .Include(x => x.Votes)
.FirstOrDefault(x => x.Id == id); .FirstOrDefault(x => x.Id == id);
@@ -30,17 +28,17 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
if (p.Votes != null) if (p.Votes != null)
{ {
_context.Set<PollVote>().RemoveRange(p.Votes); ctx.RemoveRange(p.Votes);
p.Votes.Clear(); p.Votes.Clear();
} }
if (p.Answers != null) if (p.Answers != null)
{ {
_context.Set<PollAnswer>().RemoveRange(p.Answers); ctx.RemoveRange(p.Answers);
p.Answers.Clear(); p.Answers.Clear();
} }
_set.Remove(p); ctx.Poll.Remove(p);
} }
} }
} }

View File

@@ -39,9 +39,6 @@ namespace NadekoBot.Core.Services.Database
private IXpRepository _xp; private IXpRepository _xp;
public IXpRepository Xp => _xp ?? (_xp = new XpRepository(_context)); public IXpRepository Xp => _xp ?? (_xp = new XpRepository(_context));
private IPollsRepository _polls;
public IPollsRepository Polls => _polls ?? (_polls = new PollsRepository(_context));
private IPlantedCurrencyRepository _planted; private IPlantedCurrencyRepository _planted;
public IPlantedCurrencyRepository PlantedCurrency => _planted ?? (_planted = new PlantedCurrencyRepository(_context)); public IPlantedCurrencyRepository PlantedCurrency => _planted ?? (_planted = new PlantedCurrencyRepository(_context));