Got fix working for issues editing
This commit is contained in:
@@ -17,8 +17,10 @@ public class ArtifactEntrySharedHelpers
|
||||
|
||||
IDbContextFactory<ApplicationDbContext> DbContextFactory { get; set; }
|
||||
|
||||
IArtifactGroupingProvider GroupingProvider { get; set; }
|
||||
|
||||
public ArtifactEntrySharedHelpers(IArtifactDefectProvider defectsProvider, IArtifactStorageLocationProvider storageLocationProvider, IArchiveEntryTagProvider tagsProvider, IArtifactTypeProvider typesProvider, IListedNameProvider listedNamesProvider, IDbContextFactory<ApplicationDbContext> contextFactory)
|
||||
|
||||
public ArtifactEntrySharedHelpers(IArtifactDefectProvider defectsProvider, IArtifactStorageLocationProvider storageLocationProvider, IArchiveEntryTagProvider tagsProvider, IArtifactTypeProvider typesProvider, IListedNameProvider listedNamesProvider, IDbContextFactory<ApplicationDbContext> contextFactory, IArtifactGroupingProvider groupingProvider)
|
||||
{
|
||||
DefectsProvider = defectsProvider;
|
||||
StorageLocationProvider = storageLocationProvider;
|
||||
@@ -26,6 +28,7 @@ public class ArtifactEntrySharedHelpers
|
||||
TypesProvider = typesProvider;
|
||||
ListedNameProvider = listedNamesProvider;
|
||||
DbContextFactory = contextFactory;
|
||||
GroupingProvider = groupingProvider;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> SearchDefects(string value, CancellationToken cancellationToken)
|
||||
@@ -103,214 +106,155 @@ public class ArtifactEntrySharedHelpers
|
||||
return names.Select(p => p.Value);
|
||||
}
|
||||
|
||||
/*
|
||||
public async Task OnGroupingPublished(ArtifactGroupingValidationModel model)
|
||||
{
|
||||
await using var context = await DbContextFactory.CreateDbContextAsync();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var grouping = model.ToArtifactGrouping();
|
||||
|
||||
// Caches to track entities processed within this transaction
|
||||
var processedFilePaths = new Dictionary<string, FilePathListing>();
|
||||
var processedLocations = new Dictionary<string, ArtifactStorageLocation>();
|
||||
var processedTags = new Dictionary<string, ArtifactEntryTag>();
|
||||
var processedDefects = new Dictionary<string, ArtifactDefect>();
|
||||
var processedTypes = new Dictionary<string, ArtifactType>();
|
||||
var processedNames = new Dictionary<string, ListedName>();
|
||||
// The old logic for attaching the category is still good.
|
||||
context.Attach(grouping.Category);
|
||||
|
||||
// Process File Paths for each entry first
|
||||
foreach (var entry in grouping.ChildArtifactEntries)
|
||||
// 1. Handle ArtifactType (no change, this was fine)
|
||||
if (grouping.Type is not null)
|
||||
{
|
||||
if (entry.Files is { Count: > 0 })
|
||||
var existingType = await context.ArtifactTypes
|
||||
.FirstOrDefaultAsync(t => t.Name == grouping.Type.Name);
|
||||
|
||||
if (existingType is not null)
|
||||
{
|
||||
var correctedFileList = new List<FilePathListing>();
|
||||
foreach (var fileListing in entry.Files)
|
||||
{
|
||||
var path = fileListing.Path;
|
||||
if (string.IsNullOrWhiteSpace(path)) continue;
|
||||
|
||||
if (processedFilePaths.TryGetValue(path, out var trackedFile))
|
||||
{
|
||||
correctedFileList.Add(trackedFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
var existingFile = await context.ArtifactFilePaths
|
||||
.FirstOrDefaultAsync(f => f.Path == path);
|
||||
|
||||
if (existingFile != null)
|
||||
{
|
||||
correctedFileList.Add(existingFile);
|
||||
processedFilePaths[path] = existingFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
correctedFileList.Add(fileListing);
|
||||
processedFilePaths[path] = fileListing;
|
||||
}
|
||||
}
|
||||
}
|
||||
entry.Files = correctedFileList;
|
||||
grouping.Type = existingType;
|
||||
}
|
||||
}
|
||||
|
||||
// Process all other related entities for each entry
|
||||
// 2. Process ChildArtifactEntries
|
||||
foreach (var entry in grouping.ChildArtifactEntries)
|
||||
{
|
||||
// Attach entry to its parent grouping
|
||||
// Handle ArtifactStorageLocation (no change, this was fine)
|
||||
var existingLocation = await context.ArtifactStorageLocations
|
||||
.FirstOrDefaultAsync(l => l.Location == entry.StorageLocation.Location);
|
||||
|
||||
if (existingLocation is not null)
|
||||
{
|
||||
entry.StorageLocation = existingLocation;
|
||||
}
|
||||
|
||||
// Handle Defects
|
||||
if (entry.Defects is not null && entry.Defects.Any())
|
||||
{
|
||||
var defectDescriptions = entry.Defects.Select(d => d.Description).ToList();
|
||||
var existingDefects = await context.ArtifactDefects
|
||||
.Where(d => defectDescriptions.Contains(d.Description))
|
||||
.ToListAsync();
|
||||
|
||||
// Replace in-memory defects with existing ones
|
||||
for (int i = 0; i < entry.Defects.Count; i++)
|
||||
{
|
||||
var existingDefect = existingDefects
|
||||
.FirstOrDefault(ed => ed.Description == entry.Defects[i].Description);
|
||||
|
||||
if (existingDefect is not null)
|
||||
{
|
||||
entry.Defects[i] = existingDefect;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle ListedNames
|
||||
if (entry.ListedNames is not null && entry.ListedNames.Any())
|
||||
{
|
||||
var listedNamesValues = entry.ListedNames.Select(n => n.Value).ToList();
|
||||
var existingNames = await context.ArtifactAssociatedNames
|
||||
.Where(n => listedNamesValues.Contains(n.Value))
|
||||
.ToListAsync();
|
||||
|
||||
for (int i = 0; i < entry.ListedNames.Count; i++)
|
||||
{
|
||||
var existingName = existingNames
|
||||
.FirstOrDefault(en => en.Value == entry.ListedNames[i].Value);
|
||||
|
||||
if (existingName is not null)
|
||||
{
|
||||
entry.ListedNames[i] = existingName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Tags
|
||||
if (entry.Tags is not null && entry.Tags.Any())
|
||||
{
|
||||
var tagNames = entry.Tags.Select(t => t.Name).ToList();
|
||||
var existingTags = await context.ArtifactEntryTags
|
||||
.Where(t => tagNames.Contains(t.Name))
|
||||
.ToListAsync();
|
||||
|
||||
for (int i = 0; i < entry.Tags.Count; i++)
|
||||
{
|
||||
var existingTag = existingTags
|
||||
.FirstOrDefault(et => et.Name == entry.Tags[i].Name);
|
||||
|
||||
if (existingTag is not null)
|
||||
{
|
||||
entry.Tags[i] = existingTag;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 💡 NEW: Handle pre-existing FilePathListings
|
||||
// This is the key change to resolve the exception
|
||||
if (entry.Files is not null)
|
||||
{
|
||||
foreach (var filepath in entry.Files)
|
||||
{
|
||||
// The issue is trying to add a new entity that has an existing primary key.
|
||||
// Since you stated that all files are pre-added, you must attach them.
|
||||
// Attach() tells EF Core to track the entity, assuming it already exists.
|
||||
context.Attach(filepath);
|
||||
// Also ensure the parent-child relationship is set correctly, though it's likely set by ToArtifactGrouping
|
||||
filepath.ParentArtifactEntry = entry;
|
||||
}
|
||||
}
|
||||
// Tag each entry with the parent grouping so it is linked correctly in the database
|
||||
entry.ArtifactGrouping = grouping;
|
||||
|
||||
// --- Process Storage Location ---
|
||||
var locationName = entry.StorageLocation?.Location;
|
||||
if (!string.IsNullOrWhiteSpace(locationName))
|
||||
{
|
||||
if (processedLocations.TryGetValue(locationName, out var trackedLocation))
|
||||
{
|
||||
entry.StorageLocation = trackedLocation;
|
||||
}
|
||||
else
|
||||
{
|
||||
var existingLocation = await context.ArtifactStorageLocations
|
||||
.FirstOrDefaultAsync(l => l.Location == locationName);
|
||||
|
||||
if (existingLocation != null)
|
||||
{
|
||||
entry.StorageLocation = existingLocation;
|
||||
processedLocations[locationName] = existingLocation;
|
||||
}
|
||||
else
|
||||
{
|
||||
processedLocations[locationName] = entry.StorageLocation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Process Tags ---
|
||||
if (entry.Tags is { Count: > 0 })
|
||||
{
|
||||
var correctedTagList = new List<ArtifactEntryTag>();
|
||||
foreach (var tag in entry.Tags)
|
||||
{
|
||||
var tagName = tag.Name;
|
||||
if (string.IsNullOrWhiteSpace(tagName)) continue;
|
||||
|
||||
if (processedTags.TryGetValue(tagName, out var trackedTag))
|
||||
{
|
||||
correctedTagList.Add(trackedTag);
|
||||
}
|
||||
else
|
||||
{
|
||||
var existingTag = await context.ArtifactEntryTags.FirstOrDefaultAsync(t => t.Name == tagName);
|
||||
if (existingTag != null)
|
||||
{
|
||||
correctedTagList.Add(existingTag);
|
||||
processedTags[tagName] = existingTag;
|
||||
}
|
||||
else
|
||||
{
|
||||
correctedTagList.Add(tag);
|
||||
processedTags[tagName] = tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
entry.Tags = correctedTagList;
|
||||
}
|
||||
|
||||
// --- Process Defects ---
|
||||
if (entry.Defects is { Count: > 0 })
|
||||
{
|
||||
var correctedDefectList = new List<ArtifactDefect>();
|
||||
foreach (var defect in entry.Defects)
|
||||
{
|
||||
var defectDesc = defect.Description;
|
||||
if (string.IsNullOrWhiteSpace(defectDesc)) continue;
|
||||
|
||||
if (processedDefects.TryGetValue(defectDesc, out var trackedDefect))
|
||||
{
|
||||
correctedDefectList.Add(trackedDefect);
|
||||
}
|
||||
else
|
||||
{
|
||||
var existingDefect = await context.ArtifactDefects.FirstOrDefaultAsync(d => d.Description == defectDesc);
|
||||
if (existingDefect != null)
|
||||
{
|
||||
correctedDefectList.Add(existingDefect);
|
||||
processedDefects[defectDesc] = existingDefect;
|
||||
}
|
||||
else
|
||||
{
|
||||
correctedDefectList.Add(defect);
|
||||
processedDefects[defectDesc] = defect;
|
||||
}
|
||||
}
|
||||
}
|
||||
entry.Defects = correctedDefectList;
|
||||
}
|
||||
|
||||
// --- Process Types ---
|
||||
if (entry.Type is not null)
|
||||
{
|
||||
var typeName = entry.Type.Name;
|
||||
if (!string.IsNullOrWhiteSpace(typeName))
|
||||
{
|
||||
if (processedTypes.TryGetValue(typeName, out var trackedType))
|
||||
{
|
||||
entry.Type = trackedType;
|
||||
}
|
||||
else
|
||||
{
|
||||
var existingType = await context.ArtifactTypes.FirstOrDefaultAsync(t => t.Name == typeName);
|
||||
if (existingType != null)
|
||||
{
|
||||
entry.Type = existingType;
|
||||
processedTypes[typeName] = existingType;
|
||||
}
|
||||
else
|
||||
{
|
||||
processedTypes[typeName] = entry.Type;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Process Listed Names ---
|
||||
if (entry.ListedNames is { Count: > 0 })
|
||||
{
|
||||
var correctedNameList = new List<ListedName>();
|
||||
foreach (var name in entry.ListedNames)
|
||||
{
|
||||
var nameValue = name.Value;
|
||||
if (string.IsNullOrWhiteSpace(nameValue)) continue;
|
||||
|
||||
if (processedNames.TryGetValue(nameValue, out var trackedName))
|
||||
{
|
||||
correctedNameList.Add(trackedName);
|
||||
}
|
||||
else
|
||||
{
|
||||
var existingName = await context.ArtifactAssociatedNames
|
||||
.FirstOrDefaultAsync(n => n.Value == nameValue);
|
||||
|
||||
if (existingName != null)
|
||||
{
|
||||
correctedNameList.Add(existingName);
|
||||
processedNames[nameValue] = existingName;
|
||||
}
|
||||
else
|
||||
{
|
||||
correctedNameList.Add(name);
|
||||
processedNames[nameValue] = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
entry.ListedNames = correctedNameList;
|
||||
}
|
||||
}
|
||||
|
||||
if (grouping.Category != null && grouping.Category.Id > 0)
|
||||
{
|
||||
context.Attach(grouping.Category);
|
||||
}
|
||||
// Add the entire graph. EF Core will correctly handle new vs. existing entities.
|
||||
// 3. Add the main grouping object and let EF Core handle the graph
|
||||
// The previous issues with the graph are resolved, so this line should now work.
|
||||
context.ArtifactGroupings.Add(grouping);
|
||||
|
||||
// 4. Save all changes in a single transaction
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
*/
|
||||
|
||||
public async Task OnGroupingPublished(ArtifactGroupingValidationModel model)
|
||||
{
|
||||
// The OnGroupingPublished method in this class should not contain DbContext logic.
|
||||
// It should orchestrate the data flow by calling the appropriate provider methods.
|
||||
var isNew = model.Id == 0 || model.Id is null;
|
||||
|
||||
// Convert the validation model to an entity
|
||||
var grouping = model.ToArtifactGrouping();
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
// For a new grouping, use the CreateGroupingAsync method.
|
||||
// The provider method will handle the file path logic.
|
||||
await GroupingProvider.CreateGroupingAsync(grouping);
|
||||
}
|
||||
else
|
||||
{
|
||||
// For an existing grouping, use the UpdateGroupingAsync method.
|
||||
// The provider method will handle the change tracking.
|
||||
await GroupingProvider.UpdateGroupingAsync(grouping);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user