mirror of
https://github.com/Cleanuparr/Cleanuparr.git
synced 2026-01-16 01:38:02 -05:00
50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using System.Globalization;
|
|
using Common.Helpers;
|
|
using Data.Models.Events;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
|
|
namespace Data;
|
|
|
|
/// <summary>
|
|
/// Database context for events
|
|
/// </summary>
|
|
public class DataContext : DbContext
|
|
{
|
|
public DbSet<AppEvent> Events { get; set; }
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
var dbPath = Path.Combine(ConfigurationPathProvider.GetConfigPath(), "cleanuparr.db");
|
|
optionsBuilder.UseSqlite($"Data Source={dbPath}");
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<AppEvent>(entity =>
|
|
{
|
|
entity.Property(e => e.Timestamp)
|
|
.HasConversion(new UtcDateTimeConverter());
|
|
});
|
|
}
|
|
|
|
public class UtcDateTimeConverter : ValueConverter<DateTime, DateTime>
|
|
{
|
|
public UtcDateTimeConverter() : base(
|
|
v => v,
|
|
v => DateTime.SpecifyKind(v, DateTimeKind.Utc)
|
|
) {}
|
|
}
|
|
|
|
public static string GetLikePattern(string input)
|
|
{
|
|
input = input.Replace("[", "[[]")
|
|
.Replace("%", "[%]")
|
|
.Replace("_", "[_]");
|
|
|
|
return $"%{input}%";
|
|
}
|
|
} |