private FileShareRecord ToFileShareRecord(DbFilesSecurity r) => new FileShareRecord { Tenant = r.TenantId, EntryId = MappingID(r.EntryId), EntryType = r.EntryType, Subject = r.Subject, Owner = r.Owner, Share = r.Security };
public void SetShare(FileShareRecord r) { if (r.Share == FileShare.None) { var entryId = (MappingID(r.EntryId) ?? "").ToString(); if (string.IsNullOrEmpty(entryId)) { return; } using (var tx = FilesDbContext.Database.BeginTransaction()) { var files = new List <string>(); if (r.EntryType == FileEntryType.Folder) { var folders = new List <string>(); if (int.TryParse(entryId, out var intEntryId)) { var foldersInt = FilesDbContext.Tree .Where(r => r.ParentId.ToString() == entryId) .Select(r => r.FolderId) .ToList(); folders.AddRange(foldersInt.Select(folderInt => folderInt.ToString())); files.AddRange(Query(FilesDbContext.Files).Where(r => foldersInt.Any(a => a == r.FolderId)).Select(r => r.Id.ToString())); } else { folders.Add(entryId); } var toDelete = FilesDbContext.Security .Where(a => a.TenantId == r.Tenant) .Where(a => folders.Any(b => b == a.EntryId)) .Where(a => a.EntryType == FileEntryType.Folder) .Where(a => a.Subject == r.Subject); FilesDbContext.Security.RemoveRange(toDelete); FilesDbContext.SaveChanges(); } else { files.Add(entryId); } if (0 < files.Count) { var toDelete = FilesDbContext.Security .Where(a => a.TenantId == r.Tenant) .Where(a => files.Any(b => b == a.EntryId)) .Where(a => a.EntryType == FileEntryType.File) .Where(a => a.Subject == r.Subject); FilesDbContext.Security.RemoveRange(toDelete); FilesDbContext.SaveChanges(); } tx.Commit(); } } else { var toInsert = new DbFilesSecurity { TenantId = r.Tenant, EntryId = MappingID(r.EntryId, true).ToString(), EntryType = r.EntryType, Subject = r.Subject, Owner = r.Owner, Security = r.Share, TimeStamp = DateTime.UtcNow }; FilesDbContext.AddOrUpdate(r => r.Security, toInsert); FilesDbContext.SaveChanges(); } }