Skip to content

Commit

Permalink
Fixes #39 part label templates not saving correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Brown committed Feb 23, 2022
1 parent 2f7d0b4 commit 8a2a089
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using AutoMapper;
using Binner.Common.IO.Printing;
using Binner.Common.Models;
using Binner.Common.Models.Configuration;
using Binner.Common.Models.Requests;
using Binner.Common.Models.Responses;
using System.Collections.Generic;
using System.Linq;

namespace Binner.Web.Configuration.MappingProfiles
{
Expand All @@ -9,9 +14,11 @@ public class SettingsRequestProfile : Profile
public SettingsRequestProfile()
{
CreateMap<SettingsRequest, WebHostServiceConfiguration>(MemberList.None)
.ForMember(x => x.Integrations, options => options.MapFrom(x => new IntegrationConfiguration {
.ForMember(x => x.Integrations, options => options.MapFrom(x => new IntegrationConfiguration
{
Digikey = x.Digikey,
Mouser = new MouserConfiguration {
Mouser = new MouserConfiguration
{
ApiKeys = new MouserApiKeys
{
CartApiKey = x.Mouser.CartApiKey,
Expand All @@ -21,7 +28,35 @@ public SettingsRequestProfile()
},
Octopart = x.Octopart
}))
.ForMember(x => x.PrinterConfiguration, options => options.MapFrom(x => x.Printer));
.ForMember(x => x.PrinterConfiguration, options => options.MapFrom(x => x.Printer))
.ForMember(x => x.MaxCacheItems, options => options.Ignore())
.ForMember(x => x.CacheAbsoluteExpirationMinutes, options => options.Ignore())
.ForMember(x => x.CacheSlidingExpirationMinutes, options => options.Ignore())
.ForMember(x => x.Integrations, options => options.Ignore())
.ForMember(x => x.CorsAllowOrigin, options => options.Ignore())
.ForMember(x => x.Environment, options => options.Ignore())
.ForMember(x => x.PublicUrl, options => options.Ignore())
.ForMember(x => x.Port, options => options.Ignore())
.ForMember(x => x.Name, options => options.Ignore())
.ForMember(x => x.IP, options => options.Ignore())
;

CreateMap<SettingsRequest, PrinterConfiguration>(MemberList.None)
.ForMember(x => x.PartLabelName, options => options.MapFrom(x => x.Printer.PartLabelName))
.ForMember(x => x.PartLabelSource, options => options.MapFrom(x => x.Printer.PartLabelSource))
.ForMember(x => x.PrinterName, options => options.MapFrom(x => x.Printer.PrinterName))
// complex mapping situation
.ForMember(x => x.PartLabelTemplate, options =>
options.MapFrom(x => new PartLabelTemplate
{
Line1 = x.Printer.Lines.Skip(0).Select(z => new LineConfiguration(z, LabelLines.Line1)).First(),
Line2 = x.Printer.Lines.Skip(1).Select(z => new LineConfiguration(z, LabelLines.Line2)).First(),
Line3 = x.Printer.Lines.Skip(2).Select(z => new LineConfiguration(z, LabelLines.Line3)).First(),
Line4 = x.Printer.Lines.Skip(3).Select(z => new LineConfiguration(z, LabelLines.Line4)).First(),
Identifier = x.Printer.Lines.Skip(4).Select(z => new LineConfiguration(z, LabelLines.Identifier1)).First(),
Identifier2 = x.Printer.Lines.Skip(5).Select(z => new LineConfiguration(z, LabelLines.Identifier2)).First(),
})
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Binner.Common.Models.Configuration;
using Binner.Common.Models.Responses;
using System.Collections.Generic;
using System.Linq;

namespace Binner.Web.Configuration.MappingProfiles
{
Expand All @@ -24,13 +25,41 @@ public SettingsResponseProfile()
.ForMember(x => x.CartApiKey, options => options.MapFrom(x => x.ApiKeys.CartApiKey))
.ReverseMap();

CreateMap<PrinterConfiguration, PrinterSettingsResponse>()
CreateMap<PrinterConfiguration, PrinterSettingsResponse>(MemberList.None)
.ForMember(x => x.PartLabelName, options => options.MapFrom(x => x.PartLabelName))
.ForMember(x => x.PartLabelSource, options => options.MapFrom(x => x.PartLabelSource))
.ForMember(x => x.PrinterName, options => options.MapFrom(x => x.PrinterName))
.ForMember(x => x.Lines, options => options.MapFrom(x => new List<LineConfiguration> { x.PartLabelTemplate.Line1, x.PartLabelTemplate.Line2, x.PartLabelTemplate.Line3, x.PartLabelTemplate.Line4 }))
.ForMember(x => x.Identifiers, options => options.MapFrom(x => new List<LineConfiguration> { x.PartLabelTemplate.Identifier, x.PartLabelTemplate.Identifier2 }))
.ReverseMap(); // this probably won't work here
// complex mapping situation
.ForMember(x => x.Lines, options => options.MapFrom(x => new List<LineConfiguration> {
x.PartLabelTemplate.Line1,
x.PartLabelTemplate.Line2,
x.PartLabelTemplate.Line3,
x.PartLabelTemplate.Line4
})

)
.ForMember(x => x.Identifiers, options => options.MapFrom(x => new List<LineConfiguration> {
x.PartLabelTemplate.Identifier,
x.PartLabelTemplate.Identifier2
})
);

CreateMap<PrinterSettingsResponse, PrinterConfiguration>(MemberList.None)
.ForMember(x => x.PartLabelName, options => options.MapFrom(x => x.PartLabelName))
.ForMember(x => x.PartLabelSource, options => options.MapFrom(x => x.PartLabelSource))
.ForMember(x => x.PrinterName, options => options.MapFrom(x => x.PrinterName))
.ForMember(x => x.LabelDefinitions, options => options.Ignore())
// complex mapping situation
.ForMember(x => x.PartLabelTemplate, options => options.MapFrom(x => new PartLabelTemplate
{
Line1 = x.Lines.Skip(0).FirstOrDefault(),
Line2 = x.Lines.Skip(1).FirstOrDefault(),
Line3 = x.Lines.Skip(2).FirstOrDefault(),
Line4 = x.Lines.Skip(3).FirstOrDefault(),
Identifier = x.Identifiers.Skip(0).FirstOrDefault(),
Identifier2 = x.Identifiers.Skip(1).FirstOrDefault()
})
);
}
}
}
21 changes: 20 additions & 1 deletion Binner/Library/Binner.Common/IO/Printing/LineConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using Binner.Common.Models;
using System.ComponentModel.DataAnnotations;

namespace Binner.Common.IO.Printing
{
Expand Down Expand Up @@ -66,5 +67,23 @@ public class LineConfiguration
/// Will show on previews and for printers that use color
/// </summary>
public string Color { get; set; }

public LabelLines Line { get; set; }

public LineConfiguration() { }

public LineConfiguration(LineConfiguration lineConfiguration, LabelLines line)
{
Label = lineConfiguration.Label;
Content = lineConfiguration.Content;
FontName = lineConfiguration.FontName;
AutoSize = lineConfiguration.AutoSize;
UpperCase = lineConfiguration.UpperCase;
LowerCase = lineConfiguration.LowerCase;
FontSize = lineConfiguration.FontSize;
Margin = lineConfiguration.Margin;
Color = lineConfiguration.Color;
Line = line;
}
}
}
15 changes: 15 additions & 0 deletions Binner/Library/Binner.Common/Models/LabelLines.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Binner.Common.Models
{
/// <summary>
/// Indicates a label line
/// </summary>
public enum LabelLines
{
Line1 = 1,
Line2,
Line3,
Line4,
Identifier1,
Identifier2
}
}

0 comments on commit 8a2a089

Please sign in to comment.