using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Reflection; using System.Reflection.Emit; using System.Text.Json; namespace Emberend.Database; public sealed class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Users => Set(); protected override void OnModelCreating(ModelBuilder model) { model.Entity() .Property(u => u.Interests) .HasConversion( v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null), v => JsonSerializer.Deserialize(v, (JsonSerializerOptions?)null) ?? Array.Empty() ); model.Entity().HasKey(u => u.Id); model.Entity().HasIndex(u => u.Email).IsUnique(); } } public enum Gender : byte { Unspecified = 0, Male = 1, Female = 2, Other = 3 } public sealed class User { public string Id { get; set; } = null!; public string Email { get; set; } = null!; public string Name { get; set; } = null!; public string Description { get; set; } = string.Empty; public string ProfilePicture { get; set; } = string.Empty; public DateOnly DateOfBirth { get; set; } public Gender Gender { get; set; } = Gender.Unspecified; public string[] Interests { get; set; } = Array.Empty(); public string PasswordHash { get; set; } = null!; public string Salt { get; set; } = null!; [NotMapped] public int Age => DateTime.Today.Year - DateOfBirth.Year - (DateOfBirth.ToDateTime(TimeOnly.MinValue) > DateTime.Today ? 1 : 0); }