Emberend/Database/AppDbContext.cs
2026-01-20 18:55:57 +01:00

62 lines
1.7 KiB
C#

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<AppDbContext> options) : base(options) { }
public DbSet<User> Users => Set<User>();
protected override void OnModelCreating(ModelBuilder model)
{
model.Entity<User>()
.Property(u => u.Interests)
.HasConversion(
v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
v => JsonSerializer.Deserialize<string[]>(v, (JsonSerializerOptions?)null) ?? Array.Empty<string>()
);
model.Entity<User>().HasKey(u => u.Id);
model.Entity<User>().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<string>();
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);
}