Skip to content

Commit

Permalink
Rework ef core entity configs and add UserManagerService
Browse files Browse the repository at this point in the history
  • Loading branch information
Plerx2493 committed Apr 22, 2024
1 parent 0794eb6 commit 9657c11
Show file tree
Hide file tree
Showing 11 changed files with 876 additions and 65 deletions.
12 changes: 8 additions & 4 deletions ModularAssistentForDiscordServer/Commands/Slash/Reminder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ namespace MADS.Commands.Slash;
public sealed class Reminder : MadsBaseApplicationCommand
{
private readonly ReminderService _reminderService;
private readonly UserManagerService _userManagerService;

public Reminder(ReminderService reminderService)
public Reminder(ReminderService reminderService, UserManagerService userManagerService)
{
_reminderService = reminderService;
_userManagerService = userManagerService;
}

[SlashCommand("add", "add new reminder")]
Expand All @@ -52,6 +54,8 @@ public async Task AddReminder
await EditResponse_Error("Invalid timespan (5s, 3m, 7h, 2d)");
return;
}

await _userManagerService.GetOrCreateUserAsync(ctx.User);

ReminderDbEntity newReminder = new()
{
Expand All @@ -65,7 +69,7 @@ public async Task AddReminder

ReminderDbEntity reminder = await _reminderService.AddReminder(newReminder);

await EditResponse_Success($"Reminder created with id {reminder.Id}. I will remind you in {Formatter.Timestamp(timeSpan.Value)}");
await EditResponse_Success($"Reminder created with id `{reminder.Id}`. I will remind you in {Formatter.Timestamp(timeSpan.Value)}");
}

[SlashCommand("list", "list your Reminder")]
Expand Down Expand Up @@ -108,11 +112,11 @@ long id
return;
}

bool success = await _reminderService.TryDeleteById((ulong) id);
bool success = await _reminderService.TryDeleteById((ulong) id, ctx.User.Id);

if (!success)
{
await EditResponse_Error("Something went wrong. Please try again");
await EditResponse_Error("Something went wrong. Are you sure you own this reminder?");
return;
}

Expand Down
27 changes: 2 additions & 25 deletions ModularAssistentForDiscordServer/Entities/GuildConfigDbEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,6 @@ namespace MADS.Entities;

public class GuildConfigDbEntity
{
public GuildDbEntity Guild;

public GuildConfigDbEntity()
{
}

public GuildConfigDbEntity(ulong guildId, string prefix)
{
DiscordGuildId = guildId;
Prefix = prefix;
}

public GuildConfigDbEntity(GuildConfigDbEntity old)
{
DiscordGuildId = old.DiscordGuildId;
Prefix = old.Prefix;
}

public GuildConfigDbEntity(ulong guildId)
{
DiscordGuildId = guildId;
Prefix = "!";
StarboardActive = false;
}

[Key, Column("id"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public ulong Id { get; init; }

Expand All @@ -70,4 +45,6 @@ public GuildConfigDbEntity(ulong guildId)

[Column("starboardEmojiName")]
public string? StarboardEmojiName { get; set; }

public GuildDbEntity Guild;
}
27 changes: 27 additions & 0 deletions ModularAssistentForDiscordServer/Entities/GuildDbEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace MADS.Entities;

Expand Down Expand Up @@ -47,4 +49,29 @@ public GuildDbEntity(GuildDbEntity old)
public List<IncidentDbEntity> Incidents { get; set; }

public List<QuoteDbEntity> Quotes { get; set; }
}

public class GuildDbEntityConfig : IEntityTypeConfiguration<GuildDbEntity>
{
public void Configure(EntityTypeBuilder<GuildDbEntity> builder)
{
builder.ToTable("guilds");

builder.HasKey(x => x.Id);

builder.HasOne<GuildConfigDbEntity>(x => x.Settings)
.WithOne(x => x.Guild)
.HasForeignKey<GuildConfigDbEntity>(x => x.DiscordGuildId)
.OnDelete(DeleteBehavior.Cascade);

builder.HasMany<IncidentDbEntity>(x => x.Incidents)
.WithOne(x => x.Guild)
.HasForeignKey(x => x.GuildId)
.OnDelete(DeleteBehavior.Cascade);

builder.HasMany<QuoteDbEntity>(x => x.Quotes)
.WithOne(x => x.Guild)
.HasForeignKey(x => x.DiscordGuildId)
.OnDelete(DeleteBehavior.Cascade);
}
}
31 changes: 26 additions & 5 deletions ModularAssistentForDiscordServer/Entities/UserDbEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace MADS.Entities;

Expand All @@ -25,16 +26,36 @@ public class UserDbEntity

[Column("username")]
public string Username { get; set; }

Check warning on line 28 in ModularAssistentForDiscordServer/Entities/UserDbEntity.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Username' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Column("discriminator")]
public int Discriminator { get; set; }

[Column("prefered_language")]
public string PreferedLanguage { get; set; }

Check warning on line 31 in ModularAssistentForDiscordServer/Entities/UserDbEntity.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'PreferedLanguage' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public List<IncidentDbEntity> Incidents { get; set; }

Check warning on line 33 in ModularAssistentForDiscordServer/Entities/UserDbEntity.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Incidents' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public List<ReminderDbEntity> Reminders { get; set; }

Check warning on line 35 in ModularAssistentForDiscordServer/Entities/UserDbEntity.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Reminders' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public List<VoiceAlert> VoiceAlerts { get; set; }

Check warning on line 36 in ModularAssistentForDiscordServer/Entities/UserDbEntity.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'VoiceAlerts' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public class UserDbEntityConfig : IEntityTypeConfiguration<UserDbEntity>
{
public void Configure(EntityTypeBuilder<UserDbEntity> builder)
{
builder.ToTable("users");

builder.HasKey(x => x.Id);

builder.HasMany<IncidentDbEntity>(x => x.Incidents)
.WithOne(x => x.TargetUser)
.HasForeignKey(x => x.TargetId);

builder.HasMany<ReminderDbEntity>(x => x.Reminders)
.WithOne(x => x.User)
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Cascade);

builder.HasMany<VoiceAlert>(x => x.VoiceAlerts)
.WithOne(x => x.User)
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Cascade);
}
}
Loading

0 comments on commit 9657c11

Please sign in to comment.