diff --git a/EnvelopeGenerator.Web/Models/AnnotationParams.cs b/EnvelopeGenerator.Web/Models/AnnotationParams.cs index 011858cb..d52126f7 100644 --- a/EnvelopeGenerator.Web/Models/AnnotationParams.cs +++ b/EnvelopeGenerator.Web/Models/AnnotationParams.cs @@ -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 _annots = new(); + private readonly IEnumerable _annots = new List(); - public required Dictionary 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 Names => _annots.Select(annot => annot.Name); + + public required IEnumerable 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 diff --git a/EnvelopeGenerator.Web/appsettings.json b/EnvelopeGenerator.Web/appsettings.json index aec7c0d7..3574a25b 100644 --- a/EnvelopeGenerator.Web/appsettings.json +++ b/EnvelopeGenerator.Web/appsettings.json @@ -155,15 +155,18 @@ "Width": 1, "Height": 0.5 }, - "Annotations": { - "Signature": { + "Annotations": [ + { + "Name": "Signature" }, - "City": { + { + "Name": "City", "VerBoundAnnotName": "Signature" }, - "Date": { + { + "Name": "Date", "VerBoundAnnotName": "City" } - } + ] } }