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
{
[JsonIgnore]
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;
init
{
_annots = value;
foreach (var name in _annots.Keys)
foreach (var name in Names)
{
#region set default values
if(DefaultAnnotation is not null)
{
// 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)
_annots[name]._left = DefaultAnnotation.Left;
if (this[name]._left == default && DefaultAnnotation.Left != default)
this[name]._left = DefaultAnnotation.Left;
if (_annots[name]._top == default && DefaultAnnotation.Top != default)
_annots[name]._top = DefaultAnnotation.Top;
if (this[name]._top == default && DefaultAnnotation.Top != default)
this[name]._top = DefaultAnnotation.Top;
if (_annots[name]._width == default && DefaultAnnotation.Width != default)
_annots[name]._width = DefaultAnnotation.Width;
if (this[name]._width == default && DefaultAnnotation.Width != default)
this[name]._width = DefaultAnnotation.Width;
if (_annots[name]._height == default && DefaultAnnotation.Height != default)
_annots[name]._height = DefaultAnnotation.Height;
if (this[name]._height == default && DefaultAnnotation.Height != default)
this[name]._height = DefaultAnnotation.Height;
}
#endregion
#region set bound annotations
// horizontal
if (_annots[name].HorBoundAnnotName is string horBoundAnnotName)
if (_annots.TryGetValue(horBoundAnnotName, out var horBoundAnnot))
_annots[name].HorBoundAnnot = horBoundAnnot;
if (this[name].HorBoundAnnotName is string horBoundAnnotName)
if (TryGet(horBoundAnnotName, out var horBoundAnnot))
this[name].HorBoundAnnot = horBoundAnnot;
else
throw new InvalidOperationException($"{horBoundAnnotName} added as bound anotation. However, it is not defined.");
// vertical
if (_annots[name].VerBoundAnnotName is string verBoundAnnotName)
if (_annots.TryGetValue(verBoundAnnotName, out var verBoundAnnot))
_annots[name].VerBoundAnnot = verBoundAnnot;
if (this[name].VerBoundAnnotName is string verBoundAnnotName)
if (TryGet(verBoundAnnotName, out var verBoundAnnot))
this[name].VerBoundAnnot = verBoundAnnot;
else
throw new InvalidOperationException($"{verBoundAnnotName} added as bound anotation. However, it is not defined.");
#endregion

View File

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