public async Task BeforeSave( EntityState operation, LactalisDBContext dbContext, IServiceProvider serviceProvider, CancellationToken cancellationToken = default) { if (operation == EntityState.Deleted) { if (ImageId.HasValue) { var existingFile = dbContext.Files.FirstOrDefault(f => f.Id == ImageId.Value); if (existingFile != null) { dbContext.Files.Remove(existingFile); await existingFile.BeforeSave(EntityState.Deleted, dbContext, serviceProvider); } } if (FileId.HasValue) { var existingFile = dbContext.Files.FirstOrDefault(f => f.Id == FileId.Value); if (existingFile != null) { dbContext.Files.Remove(existingFile); await existingFile.BeforeSave(EntityState.Deleted, dbContext, serviceProvider); } } } }
public async Task BeforeSave( EntityState operation, LactalisDBContext dbContext, IServiceProvider serviceProvider, CancellationToken cancellationToken = default) { }
public async Task <int> CleanReference <T>( string reference, IEnumerable <T> models, LactalisDBContext dbContext, CancellationToken cancellation = default) where T : IOwnerAbstractModel { var modelList = models.Cast <TradingPostCategoryEntity>().ToList(); var ids = modelList.Select(t => t.Id).ToList(); switch (reference) { case "TradingPostListingss": var tradingPostListingsEntities = modelList .SelectMany(m => m.TradingPostListingss) .Select(m => m.Id); var oldTradingPostListings = await dbContext.TradingPostListingsTradingPostCategories .Where(m => ids.Contains(m.TradingPostCategoriesId) && !tradingPostListingsEntities.Contains(m.Id)) .ToListAsync(cancellation); dbContext.TradingPostListingsTradingPostCategories.RemoveRange(oldTradingPostListings); return(oldTradingPostListings.Count); default: return(0); } }
public async Task <int> CleanReference <T>( string reference, IEnumerable <T> models, LactalisDBContext dbContext, CancellationToken cancellation = default) where T : IOwnerAbstractModel { var modelList = models.Cast <ImportantDocumentCategoryEntity>().ToList(); var ids = modelList.Select(t => t.Id).ToList(); switch (reference) { case "ImportantDocumentss": var importantDocumentsIds = modelList.SelectMany(x => x.ImportantDocumentss.Select(m => m.Id)).ToList(); var oldimportantDocuments = await dbContext.ImportantDocumentEntity .Where(m => m.DocumentCategoryId.HasValue && ids.Contains(m.DocumentCategoryId.Value)) .Where(m => !importantDocumentsIds.Contains(m.Id)) .ToListAsync(cancellation); foreach (var importantDocuments in oldimportantDocuments) { importantDocuments.DocumentCategoryId = null; } dbContext.ImportantDocumentEntity.UpdateRange(oldimportantDocuments); return(oldimportantDocuments.Count); default: return(0); } }
public async Task <int> CleanReference <T>( string reference, IEnumerable <T> models, LactalisDBContext dbContext, CancellationToken cancellation = default) where T : IOwnerAbstractModel { var modelList = models.Cast <PromotedArticlesEntity>().ToList(); var ids = modelList.Select(t => t.Id).ToList(); switch (reference) { case "NewsArticless": var newsArticlesIds = modelList.SelectMany(x => x.NewsArticless.Select(m => m.Id)).ToList(); var oldnewsArticles = await dbContext.NewsArticleEntity .Where(m => m.PromotedArticlesId.HasValue && ids.Contains(m.PromotedArticlesId.Value)) .Where(m => !newsArticlesIds.Contains(m.Id)) .ToListAsync(cancellation); foreach (var newsArticles in oldnewsArticles) { newsArticles.PromotedArticlesId = null; } dbContext.NewsArticleEntity.UpdateRange(oldnewsArticles); return(oldnewsArticles.Count); default: return(0); } }
public async Task AfterSave( EntityState operation, LactalisDBContext dbContext, IServiceProvider serviceProvider, ICollection <ChangeState> changes, CancellationToken cancellationToken = default) { }
public async virtual Task <int> CleanReference <T>( string reference, IEnumerable <T> models, LactalisDBContext dbContext, CancellationToken cancellation = default) where T : IOwnerAbstractModel { return(0); }
public async override Task <int> CleanReference <T>( string reference, IEnumerable <T> models, LactalisDBContext dbContext, CancellationToken cancellation = default) { var modelList = models.Cast <AdminEntity>().ToList(); var ids = modelList.Select(t => t.Id).ToList(); switch (reference) { default: return(0); } }
public async Task <int> CleanReference <T>( string reference, IEnumerable <T> models, LactalisDBContext dbContext, CancellationToken cancellation = default) where T : IOwnerAbstractModel { var modelList = models.Cast <QualityDocumentEntity>().ToList(); var ids = modelList.Select(t => t.Id).ToList(); switch (reference) { default: return(0); } }
public async Task <int> CleanReference <T>( string reference, IEnumerable <T> models, LactalisDBContext dbContext, CancellationToken cancellation = default) where T : IOwnerAbstractModel { var modelList = models.Cast <FarmEntity>().ToList(); var ids = modelList.Select(t => t.Id).ToList(); switch (reference) { case "Pickupss": var pickupsIds = modelList.SelectMany(x => x.Pickupss.Select(m => m.Id)).ToList(); var oldpickups = await dbContext.MilkTestEntity .Where(m => m.FarmId.HasValue && ids.Contains(m.FarmId.Value)) .Where(m => !pickupsIds.Contains(m.Id)) .ToListAsync(cancellation); foreach (var pickups in oldpickups) { pickups.FarmId = null; } dbContext.MilkTestEntity.UpdateRange(oldpickups); return(oldpickups.Count); case "Farmerss": var farmersEntities = modelList .SelectMany(m => m.Farmerss) .Select(m => m.Id); var oldFarmers = await dbContext.FarmersFarms .Where(m => ids.Contains(m.FarmsId) && !farmersEntities.Contains(m.Id)) .ToListAsync(cancellation); dbContext.FarmersFarms.RemoveRange(oldFarmers); return(oldFarmers.Count); default: return(0); } }
public async override Task <int> CleanReference <T>( string reference, IEnumerable <T> models, LactalisDBContext dbContext, CancellationToken cancellation = default) { var modelList = models.Cast <FarmerEntity>().ToList(); var ids = modelList.Select(t => t.Id).ToList(); switch (reference) { case "TradingPostListingss": var tradingPostListingsIds = modelList.SelectMany(x => x.TradingPostListingss.Select(m => m.Id)).ToList(); var oldtradingPostListings = await dbContext.TradingPostListingEntity .Where(m => m.FarmerId.HasValue && ids.Contains(m.FarmerId.Value)) .Where(m => !tradingPostListingsIds.Contains(m.Id)) .ToListAsync(cancellation); foreach (var tradingPostListings in oldtradingPostListings) { tradingPostListings.FarmerId = null; } dbContext.TradingPostListingEntity.UpdateRange(oldtradingPostListings); return(oldtradingPostListings.Count); case "Farmss": var farmsEntities = modelList .SelectMany(m => m.Farmss) .Select(m => m.Id); var oldFarms = await dbContext.FarmersFarms .Where(m => ids.Contains(m.FarmersId) && !farmsEntities.Contains(m.Id)) .ToListAsync(cancellation); dbContext.FarmersFarms.RemoveRange(oldFarms); return(oldFarms.Count); default: return(0); } }
public async Task AfterSave(EntityState operation, LactalisDBContext dbContext, IServiceProvider serviceProvider, ICollection <ChangeState> changes, CancellationToken cancellationToken = default) { if (operation == EntityState.Deleted) { try { var storageProvider = serviceProvider.GetRequiredService <IUploadStorageProvider>(); await storageProvider.DeleteAsync(new StorageDeleteOptions { Container = Container, FileName = FileId, }); } catch (Exception exception) { // Ignore errors to not destroy the operation since it is non critical for this to succeed. Log.Error( "Failed to delete file from storage provider. Error: {exception}", exception, this); } } return; }