namespace ASC.Webhooks.Core { [Scope] public class DbWorker { private Lazy LazyWebhooksDbContext { get; } private WebhooksDbContext webhooksDbContext { get => LazyWebhooksDbContext.Value; } private TenantManager TenantManager { get; } public DbWorker(DbContextManager webhooksDbContext, TenantManager tenantManager) { LazyWebhooksDbContext = new Lazy(() => webhooksDbContext.Value); TenantManager = tenantManager; } public int WriteToJournal(WebhooksLog webhook) { var entity = webhooksDbContext.WebhooksLogs.Add(webhook); webhooksDbContext.SaveChanges(); return entity.Entity.Id; } public WebhookEntry ReadFromJournal(int id) { return webhooksDbContext.WebhooksLogs .Where(it => it.Id == id) .Join(webhooksDbContext.WebhooksConfigs, t => t.ConfigId, t => t.ConfigId, (payload, config) => new { payload, config }) .Select(t => new WebhookEntry { Id = t.payload.Id, Payload = t.payload.RequestPayload, SecretKey = t.config.SecretKey, Uri = t.config.Uri }) .OrderBy(t => t.Id).FirstOrDefault(); } public void AddWebhookConfig(WebhooksConfig webhooksConfig) { webhooksConfig.TenantId = TenantManager.GetCurrentTenant().Id; var addObj = webhooksDbContext.WebhooksConfigs.Where(it => it.SecretKey == webhooksConfig.SecretKey && it.TenantId == webhooksConfig.TenantId && it.Uri == webhooksConfig.Uri).FirstOrDefault(); if (addObj != null) return; webhooksDbContext.WebhooksConfigs.Add(webhooksConfig); webhooksDbContext.SaveChanges(); } public void RemoveWebhookConfig(WebhooksConfig webhooksConfig) { webhooksConfig.TenantId = TenantManager.GetCurrentTenant().Id; var removeObj = webhooksDbContext.WebhooksConfigs.Where(it => it.SecretKey == webhooksConfig.SecretKey && it.TenantId == webhooksConfig.TenantId && it.Uri == webhooksConfig.Uri).FirstOrDefault(); webhooksDbContext.WebhooksConfigs.Remove(removeObj); webhooksDbContext.SaveChanges(); } public void UpdateWebhookConfig(WebhooksConfig webhooksConfig) { webhooksConfig.TenantId = TenantManager.GetCurrentTenant().Id; var updateObj = webhooksDbContext.WebhooksConfigs.Where(it => it.SecretKey == webhooksConfig.SecretKey && it.TenantId == webhooksConfig.TenantId && it.Uri == webhooksConfig.Uri).FirstOrDefault(); webhooksDbContext.WebhooksConfigs.Update(updateObj); webhooksDbContext.SaveChanges(); } public List GetWebhookConfigs(int tenant) { return webhooksDbContext.WebhooksConfigs.Where(t => t.TenantId == tenant).ToList(); } public void UpdateWebhookJournal(int id, ProcessStatus status, string responsePayload, string responseHeaders, string requestHeaders) { var webhook = webhooksDbContext.WebhooksLogs.Where(t => t.Id == id).FirstOrDefault(); webhook.Status = status; webhook.ResponsePayload = responsePayload; webhook.ResponseHeaders = responseHeaders; webhook.RequestHeaders = requestHeaders; webhooksDbContext.WebhooksLogs.Update(webhook); webhooksDbContext.SaveChanges(); } public List GetTenantWebhooks() { var tenant = TenantManager.GetCurrentTenant().Id; return webhooksDbContext.WebhooksLogs.Where(it => it.TenantId == tenant) .Select(t => new WebhooksLog { Uid = t.Uid, CreationTime = t.CreationTime, RequestPayload = t.RequestPayload, RequestHeaders = t.RequestHeaders, ResponsePayload = t.ResponsePayload, ResponseHeaders = t.ResponseHeaders, Status = t.Status }).ToList(); } public int ConfigsNumber() { return webhooksDbContext.WebhooksConfigs.Count(); } } }