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

View File

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

View File

@ -1,6 +1,12 @@
/// <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}( public static (bool, {returnTypeFullName}) Add{entityTypeName}(this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName})
this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName}) { {
var {newEntityVarName} = new {entityTypeFullName}({toCreateVarName}); var {newEntityVarName} = new {entityTypeFullName}({toCreateVarName});
dbContext.{propertyName}.Add({newEntityVarName}); dbContext.{propertyName}.Add({newEntityVarName});
var success = dbContext.SaveChanges() >= 0; var success = dbContext.SaveChanges() >= 0;

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,7 +1,10 @@
public static (bool, {returnTypeFullName}) Update{entityTypeName}( /// <summary>
this {contextFullName} dbContext, /// Updates {entityTypeName} in the DbSet of <see cref="{contextFullName}"></see>.
{updateTypeFullName} {updateVarName}, /// </summary>
{keyTypeFullName} {keyVarName}) /// <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}); var {existingEntityVarName} = dbContext.{propertyName}.FirstOrDefault(x => x.{keyPropertyName} == {keyVarName});
if ({existingEntityVarName} == null) return (false, null); if ({existingEntityVarName} == null) return (false, null);
@ -9,5 +12,4 @@ public static (bool, {returnTypeFullName}) Update{entityTypeName}(
dbContext.{propertyName}.Update({existingEntityVarName}); dbContext.{propertyName}.Update({existingEntityVarName});
var result = dbContext.SaveChanges() >= 0; var result = dbContext.SaveChanges() >= 0;
return (result, new {returnTypeFullName}({existingEntityVarName})); return (result, new {returnTypeFullName}({existingEntityVarName}));
} }

View File

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