elliebot/src/EllieBot/Db/Extensions/PollExtensions.cs

28 lines
No EOL
725 B
C#

#nullable disable
using Microsoft.EntityFrameworkCore;
using EllieBot.Services.Database;
using EllieBot.Services.Database.Models;
namespace EllieBot.Db;
public static class PollExtensions
{
public static IEnumerable<Poll> GetAllPolls(this DbSet<Poll> polls)
=> polls.Include(x => x.Answers).Include(x => x.Votes).ToArray();
public static void RemovePoll(this EllieContext ctx, int id)
{
var p = ctx.Poll.Include(x => x.Answers).Include(x => x.Votes).FirstOrDefault(x => x.Id == id);
if (p is null)
return;
if (p.Votes is not null)
{
ctx.RemoveRange(p.Votes);
p.Answers.Clear();
}
ctx.Poll.Remove(p);
}
}