Fixed bug where deletes of artifact groupings would not cascade

This commit is contained in:
Vincent Allen
2025-11-12 19:10:35 -05:00
parent b34449808f
commit 9298829db6
325 changed files with 5233 additions and 20996 deletions

View File

@@ -0,0 +1,97 @@
namespace OpenArchival.Blazor.AdminPages.Shared;
using OpenArchival.DataAccess;
using System.ComponentModel.DataAnnotations;
public class ArtifactEntryValidationModel
{
/// <summary>
/// Used when translating between the validation model and the database model
/// </summary>
public int? Id { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "An artifact numbering must be supplied")]
public string? ArtifactNumber { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "A title must be provided")]
public string? Title { get; set; }
public string? Description { get; set; }
public string? Type { get; set; }
public string? StorageLocation { get; set; }
public List<string>? Tags { get; set; } = [];
public List<string>? ListedNames { get; set; } = [];
public List<DateTime>? AssociatedDates { get; set; } = [];
public List<string>? Defects { get; set; } = [];
public List<string>? Links { get; set; } = [];
public List<FilePathListing>? Files { get; set; } = [];
public string? FileTextContent { get; set; }
public List<ArtifactEntry> RelatedArtifacts { get; set; } = [];
public bool IsPublicallyVisible { get; set; } = true;
public int Quantity { get; set; }
public ArtifactEntry ToArtifactEntry(ArtifactGrouping? parent = null)
{
List<ArtifactEntryTag> tags = new();
if (Tags is not null)
{
foreach (var tag in Tags)
{
tags.Add(new ArtifactEntryTag() { Name = tag });
}
}
List<ArtifactDefect> defects = new();
foreach (var defect in Defects)
{
defects.Add(new ArtifactDefect() { Description=defect});
}
var entry = new ArtifactEntry()
{
Id = Id ?? 0,
Files = Files,
Type = new DataAccess.ArtifactType() { Name = Type },
ArtifactNumber = ArtifactNumber,
AssociatedDates = AssociatedDates,
Defects = defects,
Links = Links,
StorageLocation = null,
Description = Description,
FileTextContent = FileTextContent,
IsPubliclyVisible = IsPublicallyVisible,
Tags = tags,
Title = Title,
ArtifactGrouping = parent,
RelatedTo = RelatedArtifacts,
Quantity = Quantity
};
List<ListedName> listedNames = new();
foreach (var name in ListedNames)
{
listedNames.Add(new ListedName() { Value=name });
}
entry.ListedNames = listedNames;
if (!string.IsNullOrEmpty(StorageLocation))
{
entry.StorageLocation = new ArtifactStorageLocation() { Location = StorageLocation };
}
return entry;
}
}

View File

@@ -0,0 +1,140 @@
using OpenArchival.DataAccess;
using System.ComponentModel.DataAnnotations;
namespace OpenArchival.Blazor.AdminPages.Shared;
public class ArtifactGroupingValidationModel : IValidatableObject
{
/// <summary>
/// Used by update code to track the database record that corresponds to the data within this DTO
/// </summary>
public int? Id { get; set; }
[Required(ErrorMessage = "A grouping title is required.")]
public string? Title { get; set; }
[Required(ErrorMessage = "A grouping description is required.")]
public string? Description { get; set; }
[Required(ErrorMessage = "A type is required.")]
public string? Type { get; set; }
public ArchiveCategory? Category { get; set; }
public List<IdentifierFieldValidationModel> IdentifierFieldValues { get; set; } = new();
public List<ArtifactEntryValidationModel> ArtifactEntries { get; set; } = new();
public bool IsPublicallyVisible { get; set; } = true;
public ArtifactGrouping ToArtifactGrouping()
{
IdentifierFields identifierFields = new();
identifierFields.Values = IdentifierFieldValues.Select(p => p.Value).ToList();
List<ArtifactEntry> entries = [];
foreach (var entry in ArtifactEntries)
{
entries.Add(entry.ToArtifactEntry());
}
var grouping = new ArtifactGrouping()
{
Id = Id ?? default,
Title = Title,
Description = Description,
Category = Category,
IdentifierFields = identifierFields,
IsPublicallyVisible = true,
ChildArtifactEntries = entries,
Type = new ArtifactType() { Name = Type }
};
// Create the parent link
foreach (var entry in grouping.ChildArtifactEntries)
{
entry.ArtifactGrouping = grouping;
}
return grouping;
}
public static ArtifactGroupingValidationModel ToValidationModel(ArtifactGrouping grouping)
{
var entries = new List<ArtifactEntryValidationModel>();
foreach (var entry in grouping.ChildArtifactEntries)
{
var defects = new List<string>();
if (entry.Defects is not null)
{
defects.AddRange(entry.Defects.Select(defect => defect.Description));
}
var validationModel = new ArtifactEntryValidationModel()
{
Id = entry.Id,
Title = entry.Title,
StorageLocation = entry.StorageLocation.Location,
ArtifactNumber = entry.ArtifactNumber,
AssociatedDates = entry.AssociatedDates,
Defects = entry?.Defects?.Select(defect => defect.Description).ToList(),
Description = entry?.Description,
Files = entry?.Files,
FileTextContent = entry?.FileTextContent,
IsPublicallyVisible = entry.IsPubliclyVisible,
Links = entry.Links,
ListedNames = entry?.ListedNames?.Select(name => name.Value).ToList(),
RelatedArtifacts = entry.RelatedTo,
Tags = entry?.Tags?.Select(tag => tag.Name).ToList(),
Type = entry?.Type.Name,
Quantity = entry.Quantity
};
entries.Add(validationModel);
}
var identifierFieldsStrings = grouping.IdentifierFields.Values;
List<IdentifierFieldValidationModel> identifierFields = new();
for (int index = 0; index < identifierFieldsStrings.Count; ++index)
{
identifierFields.Add(new IdentifierFieldValidationModel()
{
Value = identifierFieldsStrings[index],
Name = grouping.Category.FieldNames[index]
});
}
return new ArtifactGroupingValidationModel()
{
Id = grouping.Id,
Title = grouping.Title,
ArtifactEntries = entries,
Category = grouping.Category,
Description = grouping.Description,
IdentifierFieldValues = identifierFields,
IsPublicallyVisible = grouping.IsPublicallyVisible,
Type = grouping.Type.Name
};
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
foreach (var entry in ArtifactEntries)
{
var context = new ValidationContext(entry);
var validationResult = new List<ValidationResult>();
bool valid = Validator.TryValidateObject(entry, context, validationResult);
foreach (var result in validationResult)
{
yield return result;
}
}
if (ArtifactEntries.Count == 0)
{
yield return new ValidationResult("Must upload one or more files");
}
}
}

View File

@@ -0,0 +1,7 @@
namespace OpenArchival.Blazor.AdminPages.Shared;
public class IdentifierFieldValidationModel
{
public string Name { get; set; } = "";
public string Value { get; set; } = "";
}