XML Comments in Templates

This commit is contained in:
CodeLiturgy 2022-09-01 06:54:42 +01:00
parent 3e9e047b51
commit 23a7d6ebc0
9 changed files with 99 additions and 35 deletions

View File

@ -18,8 +18,7 @@ namespace BlueWest.WebApi.EF
[EfUpdateMethods(
updateType: typeof(CompanyUpdate),
returnType: typeof(CompanyUnique),
keyPropertyMemberName: nameof(Company.Id),
keyPropertyMemberType: typeof(int))
keyPropertyMemberName: nameof(Company.Id))
]
/*[EFUpdateMethods(typeof(int),
@ -36,8 +35,8 @@ namespace BlueWest.WebApi.EF
[EfUpdateMethods(
updateType: typeof(IndustryUpdate),
returnType: typeof(IndustryUnique),
keyPropertyMemberName: nameof(Industry.Id),
keyPropertyMemberType: typeof(int))
keyPropertyMemberName: nameof(Industry.Id)
)
]
public DbSet<Industry> Industries { get; set; }

View File

@ -16,6 +16,19 @@ namespace BlueWest.WebApi.EF
/// <summary>
/// Countries Database Table
/// </summary>
[EfGetOne(
returnType: typeof(CountryUnique),
keyMembernameof: nameof(Country.Id))
]
[EfAddEntityToList(
listEntityType: typeof(Currency),
listEntityCreateType: typeof(CurrencyCreate),
listEntityReturnType: typeof(CurrencyUnique),
keyMembernameof: nameof(Country.Id))
]
[EfAddMethods(
createType: typeof(CountryCreate),
returnType: typeof(CountryUnique))
@ -24,9 +37,9 @@ namespace BlueWest.WebApi.EF
[EfUpdateMethods(
updateType: typeof(CountryUpdate),
returnType: typeof(CountryUnique),
keyPropertyMemberName: nameof(Country.Id),
keyPropertyMemberType: typeof(int))
keyPropertyMemberName: nameof(Country.Id))
]
public DbSet<Country> Countries { get; set; }
/// <summary>
@ -38,8 +51,7 @@ namespace BlueWest.WebApi.EF
[EfUpdateMethods(
updateType: typeof(CurrencyUpdate),
returnType: typeof(CurrencyUnique),
keyPropertyMemberName: nameof(Currency.Id),
keyPropertyMemberType: typeof(int))
keyPropertyMemberName: nameof(Currency.Id))
]
public DbSet<Currency> Currencies { get; set; }

View File

@ -0,0 +1,9 @@
using System;
namespace BlueWest.WebApi.EF;
[AttributeUsage(AttributeTargets.Property)]
public class EfAddEntityToListAttribute : Attribute
{
public EfAddEntityToListAttribute(Type listEntityType, Type listEntityCreateType, Type listEntityReturnType, string keyMembernameof) { }
}

View File

@ -1,6 +1,12 @@
using System;
namespace BlueWest.WebApi.EF;
public class EfGetManyAttribute
[AttributeUsage(AttributeTargets.Property)]
public class EfGetManyAttribute : Attribute
{
public EfGetManyAttribute(Type returnType, string keyMemberName)
{
}
}

View File

@ -1,6 +1,12 @@
using System;
namespace BlueWest.WebApi.EF;
public class EfGetOneAttribute
[AttributeUsage(AttributeTargets.Property)]
public sealed class EfGetOneAttribute : Attribute
{
public EfGetOneAttribute(Type returnType, string keyMembernameof)
{
}
}

View File

@ -1,8 +1,14 @@
/// <summary>
/// Adds a new {entityTypeName} to <see cref="{contextFullName}"></see> DbSet.
/// </summary>
/// <param name="dbContext">The database context.</param>
/// <param name="{toCreateVarName}">The data type with the add data to create {entityTypeName}</param>
/// <returns>The added data.</returns>
public static (bool, {returnTypeFullName}) Add{entityTypeName}(
this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName}) {
var {newEntityVarName} = new {entityTypeFullName}({toCreateVarName});
dbContext.{propertyName}.Add({newEntityVarName});
var success = dbContext.SaveChanges() >= 0;
return (success, new {returnTypeFullName}({newEntityVarName}));
public static (bool, {returnTypeFullName}) Add{entityTypeName}(this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName})
{
var {newEntityVarName} = new {entityTypeFullName}({toCreateVarName});
dbContext.{propertyName}.Add({newEntityVarName});
var success = dbContext.SaveChanges() >= 0;
return (success, new {returnTypeFullName}({newEntityVarName}));
}

View File

@ -0,0 +1,26 @@
public static (bool, string, {returnTypeFullName}) Add{listEntityTypeName}To{entityTypeName}( this {contextFullName} dbContext,
{keyTypeFullName} {keyVarName}, {listEntityCreateFullName} {listItemCreateVarName}, Expression<Func<{entityTypeFullName},bool>>[] duplicationValidations)
{
var entityQuery = from aEntity in dbContext.{propertyName}
where aEntity.{keyVarName} == {keyVarName}
let itemsInList = aEntity.{entityListMemberName}
select aEntity;
var entity = entityQuery.FirstOrDefault();
if (entity == null) return (false, $"{nameof(country)} Not found.", null);
foreach (var duplicationValidation in duplicationValidations)
{
var entityToGet = dbContext.{entityListMemberName}.FirstOrDefault(duplicationValidation);
if (entityToGet != null)
{
return (false, $"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}", null);
}
}
var newListItem = new {listEntityFullName}({listItemCreateVarName});
entity.{entityListMemberName}.Add({listItemCreateVarName});
var success = dbContext.SaveChanges() >= 0;
return !success ? (false, "Error saving changes.", null) : (true, string.Empty, new {returnTypeFullName}(newListItem));
}

View File

@ -1,13 +1,15 @@
public static (bool, {returnTypeFullName}) Update{entityTypeName}(
this {contextFullName} dbContext,
{updateTypeFullName} {updateVarName},
{keyTypeFullName} {keyVarName})
{
var {existingEntityVarName} = dbContext.{propertyName}.FirstOrDefault(x => x.{keyPropertyName} == {keyVarName});
if ({existingEntityVarName} == null) return (false, null);
{existingEntityVarName}.Update({updateVarName});
dbContext.{propertyName}.Update({existingEntityVarName});
var result = dbContext.SaveChanges() >= 0;
return (result, new {returnTypeFullName}({existingEntityVarName}));
}
/// <summary>
/// Updates {entityTypeName} in the DbSet of <see cref="{contextFullName}"></see>.
/// </summary>
/// <param name="dbContext">Database context</param>
/// <param name="{updateVarName}">The data type with the add data to update {entityTypeName}</param>
/// <returns>Returns the current data.</returns>
public static (bool, {returnTypeFullName}) Update{entityTypeName}( this {contextFullName} dbContext, {updateTypeFullName} {updateVarName}, {keyTypeFullName} {keyVarName})
{
var {existingEntityVarName} = dbContext.{propertyName}.FirstOrDefault(x => x.{keyPropertyName} == {keyVarName});
if ({existingEntityVarName} == null) return (false, null);
{existingEntityVarName}.Update({updateVarName});
dbContext.{propertyName}.Update({existingEntityVarName});
var result = dbContext.SaveChanges() >= 0;
return (result, new {returnTypeFullName}({existingEntityVarName}));
}

View File

@ -77,17 +77,15 @@ namespace BlueWest.WebApi.Controllers
[HttpPost]
public ActionResult AddCurrency(CurrencyCreate currencyToCreate)
{
/*var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate);
var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate);
if (!success)
{
return new NotFoundResult();
}
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code},
new CurrencyUnique(newCurrency));*/
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, newCurrency);
return null;
}
/// <summary>