diff --git a/BlueWest.Api/Context/CompanyDbContext.cs b/BlueWest.Api/Context/CompanyDbContext.cs index 71e909b..53a2cd6 100644 --- a/BlueWest.Api/Context/CompanyDbContext.cs +++ b/BlueWest.Api/Context/CompanyDbContext.cs @@ -1,5 +1,6 @@ using BlueWest.Data; +using BlueWest.WebApi.Context; using BlueWest.WebApi.EF.Model; using MapTo; using Microsoft.EntityFrameworkCore; @@ -15,6 +16,11 @@ namespace BlueWest.WebApi.EF /// Companies. /// [EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))] + [EfAddUpdateMethods( + typeof(CompanyUnique), + typeof(CompanyUpdate), + "Id", typeof(int)) + ] /*[EFUpdateMethods(typeof(int), "Id", typeof(CompanyUpdate), diff --git a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs b/BlueWest.Api/Context/Extensions/CountryExtensions2.cs similarity index 99% rename from BlueWest.Api/Context/Extensions/CountryDbExtensions.cs rename to BlueWest.Api/Context/Extensions/CountryExtensions2.cs index b88cfde..9fbd688 100644 --- a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CountryExtensions2.cs @@ -10,7 +10,7 @@ namespace BlueWest.WebApi.EF /// /// Country table database extensions /// - public static class CountryDbExtensions + public static class CountryExtensions2 { /// diff --git a/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx b/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx index cc863fd..493a3fb 100644 --- a/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx +++ b/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx @@ -1,8 +1,8 @@ -public static (bool, {readTypeFullName}) Add{entityTypeName}( +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 {readTypeFullName}({newEntityVarName})); + return (success, new {returnTypeFullName}({newEntityVarName})); } diff --git a/BlueWest.Api/Context/Templates/AddToListTemplate.csx b/BlueWest.Api/Context/Templates/AddToListTemplate.csx deleted file mode 100644 index 95e5393..0000000 --- a/BlueWest.Api/Context/Templates/AddToListTemplate.csx +++ /dev/null @@ -1,18 +0,0 @@ - -public static (bool, string, {ReturnTypeFullyQualifiedType}) Add{ItemListTypeName}( - this {PropertyParentType} dbContext, - {KeyFullyQualifiedType} {TargetItemPropertyTypeName.ToLowerCase()}{KeyPropertyName}, - {ItemListCreateFullyQualifiedType} {ItemListCreateTypeName.FirstLower()}) -{ - var {TargetItemPropertyTypeName.FirstLower()} = dbContext.{TargetItemPropertyName}.FirstOrDefault(d => d.{KeyPropertyName} == {TargetItemPropertyTypeName.ToLowerCase()}{KeyPropertyName}); - - if ({TargetItemPropertyTypeName.FirstLower()} == null) return (false, "{TargetItemPropertyTypeName} Not found.", null); - - var new{ItemListTypeTypeName} = new {ItemListTypeFullyQualifiedType}({ItemListCreateTypeName.FirstLower()}); - - country.{ItemListPropertyName}.Add(new{ItemListTypeTypeName}); - - var success = dbContext.SaveChanges() >= 0; - - return !success ? (false, "Error saving changes.", null) : (true, string.Empty, new {ReturnTypeFullyQualifiedType}(newCurrency)); -} \ No newline at end of file diff --git a/BlueWest.Api/Context/Templates/EfEntityClass.csx b/BlueWest.Api/Context/Templates/EfEntityClass.csx deleted file mode 100644 index 5ff8576..0000000 --- a/BlueWest.Api/Context/Templates/EfEntityClass.csx +++ /dev/null @@ -1,6 +0,0 @@ -using Microsoft.EntityFrameworkCore; - -namespace {Namespace} -{ - {Code} -} \ No newline at end of file diff --git a/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx b/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx index e710861..0919b5c 100644 --- a/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx +++ b/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx @@ -1,14 +1,13 @@ -using Microsoft.EntityFrameworkCore; +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, {existingEntityVarName}); -namespace {Namespace} -{ - public static partial class {entityTypeName}Extensions { - public static (bool, {readTypeFullName}) Update{entityTypeName}( - this {contextFullName} dbContext, {updateTypeFullName} {toUpdateVarName}) { - var {newEntityVarName} = new {entityTypeFullName}({toUpdateVarName}); - dbContext.{propertyName}.Add({newEntityVarName}); - var success = dbContext.SaveChanges() >= 0; - return (success, new {readTypeFullName}({newEntityVarName})); - } - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/include/BlueWest.MapTo b/include/BlueWest.MapTo index 12d5839..ca95c87 160000 --- a/include/BlueWest.MapTo +++ b/include/BlueWest.MapTo @@ -1 +1 @@ -Subproject commit 12d5839f1660c0cf89be22d391b1f744a37c202e +Subproject commit ca95c878399d057ee42007f04e4b3fb032deaab0