2026-11-18 21:58:33 +01:00
|
|
|
|
using System.Data;
|
2026-10-28 21:43:17 +01:00
|
|
|
|
using Helpdesk.Application.Abstractions;
|
2026-11-19 13:07:48 +01:00
|
|
|
|
using Helpdesk.Application.Common;
|
2026-11-19 12:58:03 +01:00
|
|
|
|
using Helpdesk.Application.Statistics;
|
2026-10-28 21:43:17 +01:00
|
|
|
|
using Helpdesk.Domain.Auth;
|
|
|
|
|
|
using Helpdesk.Domain.Catalogs;
|
2026-11-18 18:32:59 +01:00
|
|
|
|
using Helpdesk.Domain.Issues;
|
2026-11-18 21:58:33 +01:00
|
|
|
|
using Helpdesk.Domain.Notifications;
|
|
|
|
|
|
using Helpdesk.Domain.Proposals;
|
2026-10-28 21:43:17 +01:00
|
|
|
|
using Helpdesk.Domain.Users;
|
|
|
|
|
|
using Helpdesk.Infrastructure.Security;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-11-18 21:58:33 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
2026-10-28 21:43:17 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
2026-11-19 13:07:48 +01:00
|
|
|
|
using MySqlConnector;
|
2026-10-28 21:43:17 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.Infrastructure.Persistence;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// EF Core slouží jen k mapování – schéma vytvářejí SQL migrace v <c>db/migrations</c> (kap. 12.5).
|
|
|
|
|
|
/// Názvy sloupců jsou snake_case (EFCore.NamingConventions), tabulky jsou pojmenované explicitně.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public sealed class HelpdeskDbContext(DbContextOptions<HelpdeskDbContext> options, FieldEncryptor encryptor)
|
|
|
|
|
|
: DbContext(options), IAppDbContext
|
|
|
|
|
|
{
|
|
|
|
|
|
public DbSet<User> Users => Set<User>();
|
|
|
|
|
|
public DbSet<AuthToken> AuthTokens => Set<AuthToken>();
|
|
|
|
|
|
public DbSet<Building> Buildings => Set<Building>();
|
|
|
|
|
|
public DbSet<IssueCategory> IssueCategories => Set<IssueCategory>();
|
|
|
|
|
|
public DbSet<ProposalCategory> ProposalCategories => Set<ProposalCategory>();
|
2026-11-18 18:32:59 +01:00
|
|
|
|
public DbSet<Issue> Issues => Set<Issue>();
|
|
|
|
|
|
public DbSet<IssueEvent> IssueEvents => Set<IssueEvent>();
|
2026-11-19 12:58:03 +01:00
|
|
|
|
public IQueryable<IssueTiming> IssueTimings => Set<IssueTiming>();
|
2026-11-18 18:32:59 +01:00
|
|
|
|
public DbSet<Attachment> Attachments => Set<Attachment>();
|
2026-11-18 21:58:33 +01:00
|
|
|
|
public DbSet<Proposal> Proposals => Set<Proposal>();
|
|
|
|
|
|
public DbSet<ProposalEvent> ProposalEvents => Set<ProposalEvent>();
|
|
|
|
|
|
public DbSet<ProposalVote> ProposalVotes => Set<ProposalVote>();
|
|
|
|
|
|
public DbSet<ProposalScore> ProposalScores => Set<ProposalScore>();
|
|
|
|
|
|
public DbSet<Notification> Notifications => Set<Notification>();
|
2026-11-18 18:32:59 +01:00
|
|
|
|
public DbSet<NotificationDelivery> NotificationDeliveries => Set<NotificationDelivery>();
|
|
|
|
|
|
public DbSet<PushDevice> PushDevices => Set<PushDevice>();
|
2026-10-28 21:43:17 +01:00
|
|
|
|
|
2026-11-19 13:07:48 +01:00
|
|
|
|
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return await base.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (DbUpdateException exception) when (
|
|
|
|
|
|
exception.InnerException is MySqlException { ErrorCode: MySqlErrorCode.DuplicateKeyEntry } &&
|
|
|
|
|
|
exception.Entries is [var entry] && entry.Entity is (IssueCategory or ProposalCategory))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ConflictException("Kategorie se stejným názvem již existuje.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-18 21:58:33 +01:00
|
|
|
|
public Task<IDbContextTransaction> BeginProposalWriteAsync(CancellationToken ct) =>
|
|
|
|
|
|
Database.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct);
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Proposal?> LockProposalAsync(int id, CancellationToken ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Všechny zápisy nad návrhem zamykají stejný řádek před čtením stavu a hlasů.
|
|
|
|
|
|
var rows = await Proposals.FromSql($"SELECT * FROM proposal WHERE id = {id} FOR UPDATE").ToListAsync(ct);
|
|
|
|
|
|
return rows.SingleOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 21:43:17 +01:00
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
|
{
|
|
|
|
|
|
modelBuilder.ApplyConfiguration(new UserConfiguration(encryptor));
|
|
|
|
|
|
modelBuilder.ApplyConfiguration(new AuthTokenConfiguration());
|
|
|
|
|
|
modelBuilder.ApplyConfiguration(new BuildingConfiguration());
|
|
|
|
|
|
modelBuilder.ApplyConfiguration(new IssueCategoryConfiguration());
|
|
|
|
|
|
modelBuilder.ApplyConfiguration(new ProposalCategoryConfiguration());
|
2026-11-18 18:32:59 +01:00
|
|
|
|
modelBuilder.ApplyConfiguration(new IssueConfiguration());
|
|
|
|
|
|
modelBuilder.ApplyConfiguration(new IssueEventConfiguration());
|
2026-11-19 12:58:03 +01:00
|
|
|
|
modelBuilder.Entity<IssueTiming>().HasNoKey().ToSqlQuery("""
|
|
|
|
|
|
SELECT issue_id,
|
|
|
|
|
|
TIMESTAMPDIFF(MICROSECOND, created_at, first_assigned_at) AS microseconds_to_assignment,
|
|
|
|
|
|
TIMESTAMPDIFF(MICROSECOND, created_at, closed_at) AS microseconds_to_closure
|
|
|
|
|
|
FROM v_issue_lifecycle
|
|
|
|
|
|
""");
|
2026-11-18 18:32:59 +01:00
|
|
|
|
modelBuilder.ApplyConfiguration(new AttachmentConfiguration());
|
2026-11-18 21:58:33 +01:00
|
|
|
|
modelBuilder.ApplyConfiguration(new ProposalConfiguration());
|
|
|
|
|
|
modelBuilder.ApplyConfiguration(new ProposalEventConfiguration());
|
|
|
|
|
|
modelBuilder.ApplyConfiguration(new ProposalVoteConfiguration());
|
|
|
|
|
|
modelBuilder.Entity<ProposalScore>().HasNoKey().ToView("v_proposal_score");
|
|
|
|
|
|
modelBuilder.ApplyConfiguration(new NotificationConfiguration());
|
2026-11-18 18:32:59 +01:00
|
|
|
|
modelBuilder.ApplyConfiguration(new NotificationDeliveryConfiguration());
|
|
|
|
|
|
modelBuilder.Entity<PushDevice>().ToTable("push_device");
|
2026-10-28 21:43:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Všechny časy jsou v DB v UTC; při čtení jim nastavíme Kind = Utc, aby se v JSON serializovaly se "Z".
|
|
|
|
|
|
configurationBuilder.Properties<DateTime>().HaveConversion<UtcDateTimeConverter>();
|
|
|
|
|
|
configurationBuilder.Properties<DateTime?>().HaveConversion<UtcDateTimeConverter>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private sealed class UtcDateTimeConverter() : ValueConverter<DateTime, DateTime>(
|
|
|
|
|
|
v => v.Kind == DateTimeKind.Utc ? v : v.ToUniversalTime(),
|
|
|
|
|
|
v => DateTime.SpecifyKind(v, DateTimeKind.Utc));
|
|
|
|
|
|
}
|