Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CB.Accessors/Contracts/ICreatorAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CB.Data.Entities;
using CB.Shared.Dtos;
using CB.Shared.Enums;
using Platform = CB.Shared.Enums.Platform;

namespace CB.Accessors.Contracts;

Expand Down
15 changes: 15 additions & 0 deletions src/CB.Accessors/Contracts/IFilterAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using CB.Data.Entities;
using CB.Shared.Dtos;

namespace CB.Accessors.Contracts;

public interface IFilterAccessor
{
Task<FilterDto> CreateAsync(Filter entity);

Task<List<FilterDto>> GetAllAsync();

Task<List<FilterDto>> GetAllAsync(string guildId);

Task<bool> DeleteAsync(FilterDto filter);
}
1 change: 1 addition & 0 deletions src/CB.Accessors/Implementations/CreatorAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CB.Shared.Dtos;
using CB.Shared.Enums;
using Microsoft.EntityFrameworkCore;
using Platform = CB.Shared.Enums.Platform;

namespace CB.Accessors.Implementations;

Expand Down
58 changes: 58 additions & 0 deletions src/CB.Accessors/Implementations/FilterAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using AutoMapper;
using AutoMapper.QueryableExtensions;
using CB.Accessors.Contracts;
using CB.Data;
using CB.Data.Entities;
using CB.Shared.Dtos;
using Microsoft.EntityFrameworkCore;

namespace CB.Accessors.Implementations;

public class FilterAccessor(CbContext context,
IMapper mapper) : IFilterAccessor
{
public async Task<FilterDto> CreateAsync(Filter entity)
{
context.Filters.Add(entity);
await context
.SaveChangesAsync()
.ConfigureAwait(false);

return mapper.Map<FilterDto>(entity);
}

public Task<List<FilterDto>> GetAllAsync() =>
context
.Filters
.AsNoTracking()
.ProjectTo<FilterDto>(mapper.ConfigurationProvider)
.ToListAsync();

public Task<List<FilterDto>> GetAllAsync(string guildId) =>
context
.Filters
.AsNoTracking()
.Where(x => x.GuildId == guildId)
.ProjectTo<FilterDto>(mapper.ConfigurationProvider)
.ToListAsync();

public async Task<bool> DeleteAsync(FilterDto filter)
{
var entity = await context
.Filters
.FirstOrDefaultAsync(x => x.Id == filter.Id)
.ConfigureAwait(false);

if (entity == null)
{
return false;
}

context.Filters.Remove(entity);
await context
.SaveChangesAsync()
.ConfigureAwait(false);

return true;
}
}
58 changes: 58 additions & 0 deletions src/CB.Bot/Commands/Application/FilterSlashCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using CB.Accessors.Contracts;
using CB.Data.Entities;
using Discord.Interactions;
using FilterType = CB.Shared.Enums.FilterType;
using Platform = CB.Shared.Enums.Platform;

// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMember.Local

namespace CB.Bot.Commands.Application;

public class FilterSlashCommands(IGuildAccessor guildAccessor,
IFilterAccessor filterAccessor)
: BaseSlashCommands
{
[SlashCommand(
"filter",
"Manage your filters.",
false,
RunMode.Async)]
private async Task ToggleFilter(string filterText,
FilterType filterType,
Platform platform)
{
await SocketInteraction.DeferAsync(true);

if (!await IsUserAdmin())
{
return;
}

filterText = filterText.TrimStart('"').TrimEnd('"');
var guild = await guildAccessor.GetByIdAsync(SocketInteraction.GuildId.ToString());
var existingFilters = await filterAccessor.GetAllAsync(guild?.Id);
var existingFilter = existingFilters.FirstOrDefault(
x => x.PlatformId == (int)platform &&
x.FilterTypeId == (int)filterType &&
x.Text.Equals(filterText, StringComparison.OrdinalIgnoreCase));

if (existingFilter == null)
{
await filterAccessor.CreateAsync(new Filter
{
Text = filterText,
PlatformId = (int)platform,
FilterTypeId = (int)filterType,
GuildId = guild?.Id.ToString()
});

await SocketInteraction.FollowupAsync($"{filterType} filter has been created.");
}
else
{
await filterAccessor.DeleteAsync(existingFilter);
await SocketInteraction.FollowupAsync($"{filterType} filter has been removed.");
}
}
}
1 change: 1 addition & 0 deletions src/CB.Bot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
builder.Services.AddScoped<ICreatorChannelAccessor, CreatorChannelAccessor>();
builder.Services.AddScoped<IDiscordLiveConfigurationAccessor, DiscordLiveConfigurationAccessor>();
builder.Services.AddScoped<IDropdownPayloadAccessor, DropdownPayloadAccessor>();
builder.Services.AddScoped<IFilterAccessor, FilterAccessor>();
builder.Services.AddScoped<IGuildAccessor, GuildAccessor>();
builder.Services.AddScoped<IGuildConfigurationAccessor, GuildConfigurationAccessor>();
builder.Services.AddScoped<IUserAccessor, UserAccessor>();
Expand Down
7 changes: 7 additions & 0 deletions src/CB.Data/CbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class CbContext(DbContextOptions<CbContext> options) : DbContext(options)
public DbSet<CreatorChannel> CreatorChannels => Set<CreatorChannel>();
public DbSet<DiscordLiveConfiguration> DiscordLiveConfigurations => Set<DiscordLiveConfiguration>();
public DbSet<DropdownPayload> DropdownPayloads => Set<DropdownPayload>();
public DbSet<Filter> Filters => Set<Filter>();
public DbSet<Guild> Guilds => Set<Guild>();
public DbSet<GuildConfiguration> GuildConfigurations => Set<GuildConfiguration>();
public DbSet<LiveEmbed> LiveEmbeds => Set<LiveEmbed>();
Expand Down Expand Up @@ -174,6 +175,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasForeignKey(x => x.OwnerId)
.OnDelete(DeleteBehavior.NoAction);

modelBuilder.Entity<Guild>()
.HasMany(x => x.Filters)
.WithOne(x => x.Guild)
.HasForeignKey(x => x.GuildId)
.OnDelete(DeleteBehavior.Cascade);

// Configure GuildConfiguration table
modelBuilder.Entity<GuildConfiguration>().ToTable("GuildConfigurations");

Expand Down
14 changes: 14 additions & 0 deletions src/CB.Data/Entities/Filter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace CB.Data.Entities;

public class Filter
{
public int Id { get; set; }
public string Text { get; set; }
public int PlatformId { get; set; }
public int FilterTypeId { get; set; }
public string GuildId { get; set; }

public Platform Platform { get; set; }
public FilterType FilterType { get; set; }
public Guild Guild { get; set; }
}
7 changes: 7 additions & 0 deletions src/CB.Data/Entities/FilterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CB.Data.Entities;

public class FilterType
{
public int Id { get; set; }
public string DisplayName { get; set; }
}
1 change: 1 addition & 0 deletions src/CB.Data/Entities/Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ public class Guild
public virtual LiveEmbed LiveEmbed { get; set; }
public virtual VodEmbed VodEmbed { get; set; }
public virtual DiscordLiveConfiguration DiscordLiveConfiguration { get; set; }
public virtual ICollection<Filter> Filters { get; set; }
}
13 changes: 13 additions & 0 deletions src/CB.Data/Entities/Platform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;

namespace CB.Data.Entities;

public class Platform
{
[Key]
public int Id { get; set; }
public string DisplayName { get; set; }
public string SiteUrl { get; set; }
public string LogoUrl { get; set; }
public bool Enabled { get; set; }
}
4 changes: 2 additions & 2 deletions src/CB.Engines/Implementations/CreatorEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using CB.Data.Entities;
using CB.Engines.Contracts;
using CB.Shared.Dtos;
using CB.Shared.Enums;
using CB.Shared.Responses;
using Discord;
using Discord.WebSocket;
using ChannelType = CB.Shared.Enums.ChannelType;
using Platform = CB.Shared.Enums.Platform;

// ReSharper disable UnusedMember.Global

Expand Down Expand Up @@ -37,7 +37,7 @@ await creatorAccessor.GetByChannelIdAndPlatformAsync(validChannel.ChannelId,

if (creator == null || creator.PlatformId != (int)platform)
{
creator = await creatorAccessor.CreateAsync(new Data.Entities.Creator
creator = await creatorAccessor.CreateAsync(new Creator
{
ChannelId = validChannel.ChannelId,
DisplayName = validChannel.DisplayName ?? "",
Expand Down
3 changes: 3 additions & 0 deletions src/CB.Shared/Dtos/CbProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ public CbProfile()
CreateMap<CreatorChannel, CreatorChannelDto>().ReverseMap();
CreateMap<DiscordLiveConfiguration, DiscordLiveConfigurationDto>().ReverseMap();
CreateMap<DropdownPayload, DropdownPayloadDto>().ReverseMap();
CreateMap<Filter, FilterDto>().ReverseMap();
CreateMap<FilterType, FilterTypeDto>().ReverseMap();
CreateMap<GuildConfiguration, GuildConfigurationDto>().ReverseMap();
CreateMap<Guild, GuildDto>().ReverseMap();
CreateMap<LiveEmbed, LiveEmbedDto>().ReverseMap();
CreateMap<MessageConfiguration, MessageConfigurationDto>().ReverseMap();
CreateMap<Platform, PlatformDto>().ReverseMap();
CreateMap<RoleConfiguration, RoleConfigurationDto>().ReverseMap();
CreateMap<User, UserDto>().ReverseMap();
CreateMap<VodEmbed, VodEmbedDto>().ReverseMap();
Expand Down
14 changes: 14 additions & 0 deletions src/CB.Shared/Dtos/FilterDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace CB.Shared.Dtos;

public class FilterDto
{
public int Id { get; set; }
public string Text { get; set; }
public int PlatformId { get; set; }
public int FilterTypeId { get; set; }
public int GuildId { get; set; }

public PlatformDto Platform { get; set; }
public FilterTypeDto FilterType { get; set; }
public GuildDto Guild { get; set; }
}
7 changes: 7 additions & 0 deletions src/CB.Shared/Dtos/FilterTypeDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CB.Shared.Dtos;

public class FilterTypeDto
{
public int Id { get; set; }
public string DisplayName { get; set; }
}
10 changes: 10 additions & 0 deletions src/CB.Shared/Dtos/PlatformDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CB.Shared.Dtos;

public class PlatformDto
{
public int Id { get; set; }
public string DisplayName { get; set; }
public string SiteUrl { get; set; }
public string LogoUrl { get; set; }
public bool Enabled { get; set; }
}
7 changes: 7 additions & 0 deletions src/CB.Shared/Enums/FilterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CB.Shared.Enums;

public enum FilterType
{
Game = 1,
Title = 2
}
Loading