Adapt data to new MapTo feature

This commit is contained in:
CodeLiturgy 2022-08-18 16:46:20 +01:00
parent 3a2d3a1b41
commit e41fd2d815
16 changed files with 336 additions and 233 deletions

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using BlueWest.Data;
using BlueWest.WebApi.MySQL;
@ -6,6 +7,9 @@ using Microsoft.AspNetCore.Mvc;
namespace BlueWest.WebApi.Controllers
{
/// <summary>
/// Controller responsible to get country data
/// </summary>
[ApiController]
[Route("[controller]")]
public class CountriesController : ControllerBase
@ -25,7 +29,7 @@ public class CountriesController : ControllerBase
/// <summary>
/// Add Country
/// </summary>
/// <param name="country"></param>
/// <param name="countryToCreate">The country data to create</param>
/// <returns>The newly created country</returns>
/// /// <summary>
/// Creates a Country.
@ -44,11 +48,11 @@ public class CountriesController : ControllerBase
/// <response code="201">Returns the newly created country</response>
[ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost]
public ActionResult AddCountry(Country country)
public ActionResult AddCountry(CountryCreate countryToCreate)
{
_dbContext.Countries.Add(country);
_dbContext.Countries.Add(new Country(countryToCreate, new List<Currency>()));
_dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Code}, country);
return CreatedAtRoute(nameof(GetCountryById), new {countryId = countryToCreate.Code}, countryToCreate);
}
/// <summary>

View File

@ -7,6 +7,9 @@ using Microsoft.AspNetCore.Mvc;
namespace BlueWest.WebApi.Controllers
{
/// <summary>
/// The controller responsible to fetch currency data
/// </summary>
[ApiController]
[Route("[controller]")]
public class CurrenciesController : ControllerBase
@ -19,16 +22,27 @@ namespace BlueWest.WebApi.Controllers
}
/// <summary>
/// Add Currency to the table of currencies
/// </summary>
/// <param name="currencyToCreate">Currency data to create</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost]
public ActionResult AddCurrency(CurrencyCreate currency)
public ActionResult AddCurrency(CurrencyCreate currencyToCreate)
{
var newCurrency = new Currency();
var newCurrency = new Currency(currencyToCreate, new List<Country>());
_dbContext.Currencies.Add(newCurrency);
_dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = currency.Code}, currency);
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = currencyToCreate.Code}, currencyToCreate);
}
/// <summary>
/// Update a currency data from the Currency table in the database
/// </summary>
/// <param name="currencyNumber">The currency number we want to update</param>
/// <param name="currencyToUpdate">Currency Data to update</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPut("{currencyNumber}")]
@ -48,6 +62,11 @@ namespace BlueWest.WebApi.Controllers
}
/// <summary>
/// Gets a currency by the currency number (id)
/// </summary>
/// <param name="currencyId">The id of the currency to get</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]

View File

@ -4,7 +4,9 @@ using MapTo;
namespace BlueWest.Data
{
[MapFrom(typeof(CurrencyUpdate))]
[MapFrom(new []{
typeof(CurrencyUpdate),
typeof(CurrencyCreate)})]
public partial class Currency
{

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using MapTo;
namespace BlueWest.Data
@ -8,7 +9,7 @@ namespace BlueWest.Data
public partial class CurrencyUpdate
{
// ISO 4217 Code
public string Code { get; set; }
[MaxLength(3)] public string Code { get; set; }
}

View File

@ -30,8 +30,9 @@ namespace MapTo.Extensions
internal static SourceBuilder WriteModelInfo(this SourceBuilder builder, MappingModel model)
{
var targetSourceType = model.MappedSourceTypes[0];
return builder
foreach (var targetSourceType in model.MappedSourceTypes)
{
builder
.WriteLine()
.WriteComment($" IsTypeUpdatable {model.IsTypeUpdatable}")
.WriteComment($" HasMappedBaseClass {model.HasMappedBaseClass.ToString()}")
@ -42,6 +43,9 @@ namespace MapTo.Extensions
.WriteComment($" SourceNamespace {targetSourceType.SourceNamespace}")
.WriteComment($" SourceTypeFullName {targetSourceType.SourceTypeFullName}")
.WriteComment($" SourceTypeIdentifierName {targetSourceType.SourceTypeIdentifierName}");
}
return builder;
}

View File

@ -15,7 +15,6 @@ namespace MapTo.Extensions
{
const bool writeDebugInfo = true;
var targetSourceType = model.MappedSourceTypes[0];
using var builder = new SourceBuilder()
.WriteLine(GeneratedFilesHeader)
@ -27,6 +26,10 @@ namespace MapTo.Extensions
.WriteLine($"namespace {model.Namespace}")
.WriteOpeningBracket();
foreach (var targetSourceType in model.MappedSourceTypes)
{
if (writeDebugInfo)
builder
.WriteModelInfo(model)
@ -45,6 +48,9 @@ namespace MapTo.Extensions
.WriteMappedProperties(targetSourceType.SourceFields)
.WriteLine();
}
builder
// Class declaration
.WriteLine($"partial {structOrClass} {model.TypeIdentifierName}")
@ -52,10 +58,14 @@ namespace MapTo.Extensions
.WriteLine()
// Class body
.GeneratePublicConstructor(model);
if (model.IsJsonExtension) builder.WriteToJsonMethod(model);
foreach (var targetSourceType in model.MappedSourceTypes)
{
if (model.IsTypeUpdatable && targetSourceType.TypeProperties.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
if (model.IsTypeUpdatable && targetSourceType.TypeFields.GetWritableMappedProperties().Length > 0) builder.GenerateUpdateMethod(model);
}
if (model.IsJsonExtension) builder.WriteToJsonMethod(model);
builder
.WriteLine()
@ -70,14 +80,13 @@ namespace MapTo.Extensions
private static SourceBuilder GeneratePublicConstructor(this SourceBuilder builder, MappingModel model)
{
var targetSourceType = model.MappedSourceTypes[0];
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
const string mappingContextParameterName = "context";
foreach (var targetSourceType in model.MappedSourceTypes)
{
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
var baseConstructor = /*model.HasMappedBaseClass ? $" : base({mappingContextParameterName}, {sourceClassParameterName})" :*/ string.Empty;
var stringBuilder = new StringBuilder();
var otherProperties = new List<MappedMember>();
foreach (var property in targetSourceType.TypeProperties)
@ -88,8 +97,8 @@ namespace MapTo.Extensions
stringBuilder.Append($"{property.FullyQualifiedType} {property.SourcePropertyName.ToCamelCase()}");
otherProperties.Add(property);
}
}
}
foreach (var property in targetSourceType.TypeFields)
{
if (!targetSourceType.SourceFields.IsMappedProperty(property))
@ -100,7 +109,6 @@ namespace MapTo.Extensions
}
}
var readOnlyPropertiesArguments = stringBuilder.ToString();
builder
@ -108,8 +116,11 @@ namespace MapTo.Extensions
.WriteOpeningBracket()
.WriteAssignmentMethod(model, otherProperties.ToArray().ToImmutableArray(), sourceClassParameterName, mappingContextParameterName, false);
builder.WriteClosingBracket();
}
// End constructor declaration
return builder.WriteClosingBracket();
return builder;
}
private static bool IsMappedProperty(this System.Collections.Immutable.ImmutableArray<MappedMember> properties, MappedMember property)
@ -131,7 +142,8 @@ namespace MapTo.Extensions
.WriteLine("var stringBuilder = new System.Text.StringBuilder();")
.WriteLine(GetStringBuilderAppendNoInterpolation("{"));
var targetSourceType = model.MappedSourceTypes[0];
foreach (var targetSourceType in model.MappedSourceTypes)
{
foreach (var property in targetSourceType.TypeProperties)
{
if (!property.isEnumerable)
@ -154,6 +166,9 @@ namespace MapTo.Extensions
builder.WriteLine(GetStringBuilderAppendNoInterpolation("}"));
builder.WriteLine("return stringBuilder.ToString();");
builder.WriteClosingBracket();
}
return builder;
}
@ -231,21 +246,29 @@ namespace MapTo.Extensions
private static SourceBuilder WriteAssignmentMethod(this SourceBuilder builder, MappingModel model, System.Collections.Immutable.ImmutableArray<MappedMember>? otherProperties,
string? sourceClassParameterName, string mappingContextParameterName, bool fromUpdate)
{
var targetSourceType = model.MappedSourceTypes[0];
List<MappedMember> _addedMembers = new List<MappedMember>();
foreach (var targetSourceType in model.MappedSourceTypes)
{
foreach (var property in targetSourceType.SourceProperties)
{
if (property.isReadOnly && fromUpdate) continue;
if(_addedMembers.Contains(property)) continue;
builder.WriteLine($"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};");
_addedMembers.Add(property);
}
foreach (var property in targetSourceType.SourceFields)
{
if (property.isReadOnly && fromUpdate) continue;
if(_addedMembers.Contains(property)) continue;
builder.WriteLine($"{property.Name} = {sourceClassParameterName}.{property.SourcePropertyName};");
_addedMembers.Add(property);
}
@ -253,11 +276,18 @@ namespace MapTo.Extensions
foreach (var property in otherProperties)
{
if(_addedMembers.Contains(property)) continue;
builder.WriteLine(property.MappedSourcePropertyTypeName is null
? $"{property.Name} = {property.SourcePropertyName.ToCamelCase()};"
: "");
_addedMembers.Add(property);
}
}
return builder;
@ -266,7 +296,9 @@ namespace MapTo.Extensions
private static SourceBuilder GenerateUpdateMethod(this SourceBuilder builder, MappingModel model)
{
var targetSourceType = model.MappedSourceTypes[0];
foreach (var targetSourceType in model.MappedSourceTypes)
{
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
builder
@ -275,6 +307,8 @@ namespace MapTo.Extensions
.WriteOpeningBracket()
.WriteAssignmentMethod(model, null, sourceClassParameterName, "context", true)
.WriteClosingBracket();
}
return builder;
}
@ -286,28 +320,37 @@ namespace MapTo.Extensions
return builder;
}
var targetSourceType = model.MappedSourceTypes[0];
return builder
foreach (var targetSourceType in model.MappedSourceTypes)
{
builder
.WriteLine("/// <summary>")
.WriteLine($"/// Updates <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
.WriteLine($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
.WriteLine("/// </summary>")
.WriteLine($"/// <param name=\"{sourceClassParameterName}\">The instance of <see cref=\"{targetSourceType.SourceType}\"/> to use as source.</param>");
}
return builder;
}
private static SourceBuilder GenerateEnumerableJsonSourceTypeExtensionMethod(this SourceBuilder builder, MappingModel model)
{
var targetSourceType = model.MappedSourceTypes[0];
foreach (var targetSourceType in model.MappedSourceTypes)
{
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
return builder
builder
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
.WriteLine($"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static string ToJson(this IEnumerable<{targetSourceType.SourceType}{model.Options.NullableReferenceSyntax}> {sourceClassParameterName}List)")
.WriteOpeningBracket()
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
.WriteClosingBracket();
}
return builder;
}
}
}

View File

@ -22,15 +22,18 @@ namespace MapTo.Sources
.WriteLine($"partial record {model.TypeIdentifierName}")
.WriteOpeningBracket();
var targetSourceType = model.MappedSourceTypes[0];
// Class body
foreach (var targetSourceType in model.MappedSourceTypes)
{
if (targetSourceType.GenerateSecondaryConstructor)
{
builder
.GenerateSecondaryConstructor(model)
.WriteLine();
}
}
// Class body
builder
.GeneratePrivateConstructor(model)
@ -55,9 +58,10 @@ namespace MapTo.Sources
private static SourceBuilder GenerateSecondaryConstructor(this SourceBuilder builder, MappingModel model)
{
// grab first data from array
var targetSourceType = model.MappedSourceTypes[0];
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
foreach (var targetSourceType in model.MappedSourceTypes)
{
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
if (model.Options.GenerateXmlDocument)
{
builder
@ -67,43 +71,47 @@ namespace MapTo.Sources
.WriteLine("/// </summary>")
.WriteLine($"/// <exception cref=\"ArgumentNullException\">{sourceClassParameterName} is null</exception>");
}
return builder
.WriteLine($"{model.Options.ConstructorAccessModifier.ToLowercaseString()} {model.TypeIdentifierName}({targetSourceType.SourceType} {sourceClassParameterName})")
builder .WriteLine($"{model.Options.ConstructorAccessModifier.ToLowercaseString()} {model.TypeIdentifierName}({targetSourceType.SourceType} {sourceClassParameterName})")
.WriteLine($" : this(new {MappingContextSource.ClassName}(), {sourceClassParameterName}) {{ }}");
}
return builder;
}
private static SourceBuilder GeneratePrivateConstructor(this SourceBuilder builder, MappingModel model)
{
var targetSourceType = model.MappedSourceTypes[0];
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
const string mappingContextParameterName = "context";
foreach (var targetSourceType in model.MappedSourceTypes)
{
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
builder
.WriteLine($"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {targetSourceType.SourceType} {sourceClassParameterName})")
.WriteLine(
$"private protected {model.TypeIdentifierName}({MappingContextSource.ClassName} {mappingContextParameterName}, {targetSourceType.SourceType} {sourceClassParameterName})")
.Indent()
.Write(": this(").
WriteProperties(model, sourceClassParameterName, mappingContextParameterName)
.Write(": this(").WriteProperties(model, sourceClassParameterName, mappingContextParameterName)
.WriteLine(")")
.Unindent()
.WriteOpeningBracket()
.WriteLine($"if ({mappingContextParameterName} == null) throw new ArgumentNullException(nameof({mappingContextParameterName}));")
.WriteLine($"if ({sourceClassParameterName} == null) throw new ArgumentNullException(nameof({sourceClassParameterName}));")
.WriteLine()
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);");
.WriteLine($"{mappingContextParameterName}.{MappingContextSource.RegisterMethodName}({sourceClassParameterName}, this);")
.WriteClosingBracket();
}
// End constructor declaration
return builder.WriteClosingBracket();
return builder;
}
private static SourceBuilder WriteProperties(this SourceBuilder builder, MappingModel model, string sourceClassParameterName,
string mappingContextParameterName)
{
var targetSourceType = model.MappedSourceTypes[0];
foreach (var targetSourceType in model.MappedSourceTypes)
{
for (var i = 0; i < targetSourceType.SourceProperties.Length; i++)
{
var property = targetSourceType.SourceProperties[i];
@ -136,16 +144,20 @@ namespace MapTo.Sources
builder.Write(", ");
}
}
}
return builder;
}
private static SourceBuilder GenerateFactoryMethod(this SourceBuilder builder, MappingModel model)
{
var targetSourceType = model.MappedSourceTypes[0];
foreach (var targetSourceType in model.MappedSourceTypes)
{
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
return builder
builder
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
.WriteLine(
@ -156,15 +168,20 @@ namespace MapTo.Sources
.WriteClosingBracket();
}
return builder;
}
private static SourceBuilder GenerateConvertorMethodsXmlDocs(this SourceBuilder builder, MappingModel model, string sourceClassParameterName)
{
if (!model.Options.GenerateXmlDocument)
{
return builder;
}
var targetSourceType = model.MappedSourceTypes[0];
return builder
foreach (var targetSourceType in model.MappedSourceTypes)
{
builder
.WriteLine("/// <summary>")
.WriteLine($"/// Creates a new instance of <see cref=\"{model.TypeIdentifierName}\"/> and sets its participating properties")
.WriteLine($"/// using the property values from <paramref name=\"{sourceClassParameterName}\"/>.")
@ -174,11 +191,15 @@ namespace MapTo.Sources
$"/// <returns>A new instance of <see cred=\"{model.TypeIdentifierName}\"/> -or- <c>null</c> if <paramref name=\"{sourceClassParameterName}\"/> is <c>null</c>.</returns>");
}
return builder;
}
private static SourceBuilder GenerateSourceTypeExtensionClass(this SourceBuilder builder, MappingModel model)
{
var targetSourceType = model.MappedSourceTypes[0];
return builder
foreach (var targetSourceType in model.MappedSourceTypes)
{
builder
.WriteLine(
$"{model.Options.GeneratedMethodsAccessModifier.ToLowercaseString()} static partial class {targetSourceType.SourceTypeIdentifierName}To{model.TypeIdentifierName}Extensions")
.WriteOpeningBracket()
@ -186,13 +207,17 @@ namespace MapTo.Sources
.WriteClosingBracket();
}
return builder;
}
private static SourceBuilder GenerateSourceTypeExtensionMethod(this SourceBuilder builder, MappingModel model)
{
var targetSourceType = model.MappedSourceTypes[0];
foreach (var targetSourceType in model.MappedSourceTypes)
{
var sourceClassParameterName = targetSourceType.SourceTypeIdentifierName.ToCamelCase();
return builder
builder
.GenerateConvertorMethodsXmlDocs(model, sourceClassParameterName)
.WriteLineIf(model.Options.SupportNullableStaticAnalysis, $"[return: NotNullIfNotNull(\"{sourceClassParameterName}\")]")
.WriteLine(
@ -200,6 +225,11 @@ namespace MapTo.Sources
.WriteOpeningBracket()
.WriteLine($"return {sourceClassParameterName} == null ? null : new {model.TypeIdentifierName}({sourceClassParameterName});")
.WriteClosingBracket();
}
return builder;
}
}
}