feat(Annotations): Konvertiere _annots von Dictionary zu IEnumerable.

- Zugehörige Index- und TryGet-Methoden hinzugefügt.
This commit is contained in:
Developer 02 2025-03-20 14:44:57 +01:00
parent 093e64de81
commit 78b5e3f5cc
2 changed files with 42 additions and 23 deletions

View File

@ -1,50 +1,66 @@
namespace EnvelopeGenerator.Web.Models; using System.Text.Json.Serialization;
namespace EnvelopeGenerator.Web.Models;
public class AnnotationParams public class AnnotationParams
{ {
[JsonIgnore]
public Annotation? DefaultAnnotation { get; init; } public Annotation? DefaultAnnotation { get; init; }
private readonly Dictionary<string, Annotation> _annots = new(); private readonly IEnumerable<Annotation> _annots = new List<Annotation>();
public required Dictionary<string, Annotation> Annotations public Annotation this[string name] => _annots.First(a => a.Name == name);
public bool TryGet(string name, out Annotation annotation)
{
#pragma warning disable CS8601 // Possible null reference assignment.
annotation = _annots.FirstOrDefault(a => a.Name == name);
#pragma warning restore CS8601 // Possible null reference assignment.
return annotation is not null;
}
[JsonIgnore]
public IEnumerable<string> Names => _annots.Select(annot => annot.Name);
public required IEnumerable<Annotation> Annotations
{ {
get => _annots; get => _annots;
init init
{ {
_annots = value; _annots = value;
foreach (var name in _annots.Keys) foreach (var name in Names)
{ {
#region set default values #region set default values
if(DefaultAnnotation is not null) if(DefaultAnnotation is not null)
{ {
// To set default value, annotation must have default (0) value but default must has non-default value // To set default value, annotation must have default (0) value but default must has non-default value
if (_annots[name]._left == default && DefaultAnnotation.Left != default) if (this[name]._left == default && DefaultAnnotation.Left != default)
_annots[name]._left = DefaultAnnotation.Left; this[name]._left = DefaultAnnotation.Left;
if (_annots[name]._top == default && DefaultAnnotation.Top != default) if (this[name]._top == default && DefaultAnnotation.Top != default)
_annots[name]._top = DefaultAnnotation.Top; this[name]._top = DefaultAnnotation.Top;
if (_annots[name]._width == default && DefaultAnnotation.Width != default) if (this[name]._width == default && DefaultAnnotation.Width != default)
_annots[name]._width = DefaultAnnotation.Width; this[name]._width = DefaultAnnotation.Width;
if (_annots[name]._height == default && DefaultAnnotation.Height != default) if (this[name]._height == default && DefaultAnnotation.Height != default)
_annots[name]._height = DefaultAnnotation.Height; this[name]._height = DefaultAnnotation.Height;
} }
#endregion #endregion
#region set bound annotations #region set bound annotations
// horizontal // horizontal
if (_annots[name].HorBoundAnnotName is string horBoundAnnotName) if (this[name].HorBoundAnnotName is string horBoundAnnotName)
if (_annots.TryGetValue(horBoundAnnotName, out var horBoundAnnot)) if (TryGet(horBoundAnnotName, out var horBoundAnnot))
_annots[name].HorBoundAnnot = horBoundAnnot; this[name].HorBoundAnnot = horBoundAnnot;
else else
throw new InvalidOperationException($"{horBoundAnnotName} added as bound anotation. However, it is not defined."); throw new InvalidOperationException($"{horBoundAnnotName} added as bound anotation. However, it is not defined.");
// vertical // vertical
if (_annots[name].VerBoundAnnotName is string verBoundAnnotName) if (this[name].VerBoundAnnotName is string verBoundAnnotName)
if (_annots.TryGetValue(verBoundAnnotName, out var verBoundAnnot)) if (TryGet(verBoundAnnotName, out var verBoundAnnot))
_annots[name].VerBoundAnnot = verBoundAnnot; this[name].VerBoundAnnot = verBoundAnnot;
else else
throw new InvalidOperationException($"{verBoundAnnotName} added as bound anotation. However, it is not defined."); throw new InvalidOperationException($"{verBoundAnnotName} added as bound anotation. However, it is not defined.");
#endregion #endregion

View File

@ -155,15 +155,18 @@
"Width": 1, "Width": 1,
"Height": 0.5 "Height": 0.5
}, },
"Annotations": { "Annotations": [
"Signature": { {
"Name": "Signature"
}, },
"City": { {
"Name": "City",
"VerBoundAnnotName": "Signature" "VerBoundAnnotName": "Signature"
}, },
"Date": { {
"Name": "Date",
"VerBoundAnnotName": "City" "VerBoundAnnotName": "City"
} }
} ]
} }
} }