From 23a7d6ebc06c693556626707c4360e3095f979e4 Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Thu, 1 Sep 2022 06:54:42 +0100 Subject: [PATCH] XML Comments in Templates --- BlueWest.Api/Context/CompanyDbContext.cs | 7 ++--- BlueWest.Api/Context/CountryDbContext.cs | 20 ++++++++++--- .../Extensions/EfAddEntityToListAttribute.cs | 9 ++++++ .../Context/Extensions/EfGetManyAttribute.cs | 10 +++++-- .../Context/Extensions/EfGetOneAttribute.cs | 10 +++++-- .../Context/Templates/AddToEntityTemplate.csx | 18 ++++++++---- .../Context/Templates/AddToListInEntity.csx | 26 +++++++++++++++++ .../Templates/UpdateEntityTemplate.csx | 28 ++++++++++--------- .../Controllers/CurrencyController.cs | 6 ++-- 9 files changed, 99 insertions(+), 35 deletions(-) create mode 100644 BlueWest.Api/Context/Extensions/EfAddEntityToListAttribute.cs diff --git a/BlueWest.Api/Context/CompanyDbContext.cs b/BlueWest.Api/Context/CompanyDbContext.cs index d608411..d001a5c 100644 --- a/BlueWest.Api/Context/CompanyDbContext.cs +++ b/BlueWest.Api/Context/CompanyDbContext.cs @@ -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 Industries { get; set; } diff --git a/BlueWest.Api/Context/CountryDbContext.cs b/BlueWest.Api/Context/CountryDbContext.cs index cff8a29..f652ef7 100644 --- a/BlueWest.Api/Context/CountryDbContext.cs +++ b/BlueWest.Api/Context/CountryDbContext.cs @@ -16,6 +16,19 @@ namespace BlueWest.WebApi.EF /// /// Countries Database Table /// + + [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 Countries { get; set; } /// @@ -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 Currencies { get; set; } diff --git a/BlueWest.Api/Context/Extensions/EfAddEntityToListAttribute.cs b/BlueWest.Api/Context/Extensions/EfAddEntityToListAttribute.cs new file mode 100644 index 0000000..bbded21 --- /dev/null +++ b/BlueWest.Api/Context/Extensions/EfAddEntityToListAttribute.cs @@ -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) { } +} \ No newline at end of file diff --git a/BlueWest.Api/Context/Extensions/EfGetManyAttribute.cs b/BlueWest.Api/Context/Extensions/EfGetManyAttribute.cs index f3d878f..711fa26 100644 --- a/BlueWest.Api/Context/Extensions/EfGetManyAttribute.cs +++ b/BlueWest.Api/Context/Extensions/EfGetManyAttribute.cs @@ -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) + { + + } } \ No newline at end of file diff --git a/BlueWest.Api/Context/Extensions/EfGetOneAttribute.cs b/BlueWest.Api/Context/Extensions/EfGetOneAttribute.cs index 6873f90..86a81cf 100644 --- a/BlueWest.Api/Context/Extensions/EfGetOneAttribute.cs +++ b/BlueWest.Api/Context/Extensions/EfGetOneAttribute.cs @@ -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) + { + + } } \ No newline at end of file diff --git a/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx b/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx index 493a3fb..b3f3d82 100644 --- a/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx +++ b/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx @@ -1,8 +1,14 @@ +/// +/// Adds a new {entityTypeName} to DbSet. +/// +/// The database context. +/// The data type with the add data to create {entityTypeName} +/// The added data. -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})); } diff --git a/BlueWest.Api/Context/Templates/AddToListInEntity.csx b/BlueWest.Api/Context/Templates/AddToListInEntity.csx index e69de29..6d294f0 100644 --- a/BlueWest.Api/Context/Templates/AddToListInEntity.csx +++ b/BlueWest.Api/Context/Templates/AddToListInEntity.csx @@ -0,0 +1,26 @@ +public static (bool, string, {returnTypeFullName}) Add{listEntityTypeName}To{entityTypeName}( this {contextFullName} dbContext, + {keyTypeFullName} {keyVarName}, {listEntityCreateFullName} {listItemCreateVarName}, Expression>[] 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)); +} \ No newline at end of file diff --git a/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx b/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx index 33147a4..9433679 100644 --- a/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx +++ b/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx @@ -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})); - - } \ No newline at end of file +/// +/// Updates {entityTypeName} in the DbSet of . +/// +/// Database context +/// The data type with the add data to update {entityTypeName} +/// Returns the current data. +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})); +} \ No newline at end of file diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index 41b8b6d..a7a5530 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -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; } ///