From 6540959778767d6dbbe31da79f48c51e87fb2286 Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Sat, 27 Aug 2022 21:02:46 +0100 Subject: [PATCH] Refactoring and prepare to add update template --- BlueWest.Api/BlueWest.Api.csproj | 1 + BlueWest.Api/Context/AttributeNavigation.cs | 10 ++++++++ BlueWest.Api/Context/CountryDbContext.cs | 24 ++++++++++++++++++- .../Context/EfAddUpdateMethodsAttribute.cs | 11 +++++++++ .../Context/Templates/AddToEntityTemplate.csx | 10 ++++++++ .../Templates}/AddToListTemplate.csx | 0 .../Context/Templates/EfEntityClass.csx | 6 +++++ .../Templates/UpdateEntityTemplate.csx} | 6 ++--- include/BlueWest.MapTo | 2 +- 9 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 BlueWest.Api/Context/AttributeNavigation.cs create mode 100644 BlueWest.Api/Context/EfAddUpdateMethodsAttribute.cs create mode 100644 BlueWest.Api/Context/Templates/AddToEntityTemplate.csx rename BlueWest.Api/{Extensions => Context/Templates}/AddToListTemplate.csx (100%) create mode 100644 BlueWest.Api/Context/Templates/EfEntityClass.csx rename BlueWest.Api/{Extensions/AddToEntityTemplate.csx => Context/Templates/UpdateEntityTemplate.csx} (69%) diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index e594f7c..c63356d 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -19,6 +19,7 @@ + diff --git a/BlueWest.Api/Context/AttributeNavigation.cs b/BlueWest.Api/Context/AttributeNavigation.cs new file mode 100644 index 0000000..b00cbc4 --- /dev/null +++ b/BlueWest.Api/Context/AttributeNavigation.cs @@ -0,0 +1,10 @@ +namespace BlueWest.WebApi.Context; + +public class AttributeNavigation +{ + object HasForeignKey( + Expression> foreignKeyExpression) + { + + } +} \ No newline at end of file diff --git a/BlueWest.Api/Context/CountryDbContext.cs b/BlueWest.Api/Context/CountryDbContext.cs index 280ed87..d6a276d 100644 --- a/BlueWest.Api/Context/CountryDbContext.cs +++ b/BlueWest.Api/Context/CountryDbContext.cs @@ -1,4 +1,5 @@ using BlueWest.Data; +using BlueWest.WebApi.Context; using BlueWest.WebApi.EF.Model; using MapTo; using Microsoft.EntityFrameworkCore; @@ -16,15 +17,36 @@ namespace BlueWest.WebApi.EF /// /// Countries Database Table /// - [EfAddMethods(typeof(CountryCreate), typeof(CountryUnique))] + [EfAddMethods( + createDto: typeof(CountryCreate), + returnType: typeof(CountryUnique)) + ] + + [EfAddUpdateMethods( + updateType: typeof(CountryUpdate), + returnType: typeof(CountryUnique), + keyPropertyName: "Id", + keyPropertyType: typeof(System.Int64)) + ] public DbSet Countries { get; set; } /// /// Currencies Database Table /// + // Generate Add extension methods [EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))] + + [EfAddUpdateMethods( + updateType: typeof(CurrencyUpdate), + returnType: typeof(CurrencyUnique), + keyPropertyName: "Id", + keyPropertyType: typeof(System.Int64)) + ] public DbSet Currencies { get; set; } + //////////////////////////////////////////////// + //////////////////////////////////////////////// + /// /// App Configuration /// diff --git a/BlueWest.Api/Context/EfAddUpdateMethodsAttribute.cs b/BlueWest.Api/Context/EfAddUpdateMethodsAttribute.cs new file mode 100644 index 0000000..8959196 --- /dev/null +++ b/BlueWest.Api/Context/EfAddUpdateMethodsAttribute.cs @@ -0,0 +1,11 @@ +using System; +using System.Linq.Expressions; +using BlueWest.Data; + +namespace BlueWest.WebApi.Context; + +[AttributeUsage(AttributeTargets.Property)] +public class EfAddUpdateMethodsAttribute : Attribute +{ + public EfAddUpdateMethodsAttribute(Type returnType, Type updateType, string keyPropertyName, Type keyPropertyType) { } +} \ No newline at end of file diff --git a/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx b/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx new file mode 100644 index 0000000..1c12f32 --- /dev/null +++ b/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx @@ -0,0 +1,10 @@ + +public static partial class {entityTypeName}Extensions { + public static (bool, {readTypeFullName}) 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})); + } +} diff --git a/BlueWest.Api/Extensions/AddToListTemplate.csx b/BlueWest.Api/Context/Templates/AddToListTemplate.csx similarity index 100% rename from BlueWest.Api/Extensions/AddToListTemplate.csx rename to BlueWest.Api/Context/Templates/AddToListTemplate.csx diff --git a/BlueWest.Api/Context/Templates/EfEntityClass.csx b/BlueWest.Api/Context/Templates/EfEntityClass.csx new file mode 100644 index 0000000..5ff8576 --- /dev/null +++ b/BlueWest.Api/Context/Templates/EfEntityClass.csx @@ -0,0 +1,6 @@ +using Microsoft.EntityFrameworkCore; + +namespace {Namespace} +{ + {Code} +} \ No newline at end of file diff --git a/BlueWest.Api/Extensions/AddToEntityTemplate.csx b/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx similarity index 69% rename from BlueWest.Api/Extensions/AddToEntityTemplate.csx rename to BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx index 5c98dcf..e710861 100644 --- a/BlueWest.Api/Extensions/AddToEntityTemplate.csx +++ b/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx @@ -3,9 +3,9 @@ using Microsoft.EntityFrameworkCore; namespace {Namespace} { public static partial class {entityTypeName}Extensions { - public static (bool, {readTypeFullName}) Add{entityTypeName}( - this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName}) { - var {newEntityVarName} = new {entityTypeFullName}({toCreateVarName}); + 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})); diff --git a/include/BlueWest.MapTo b/include/BlueWest.MapTo index 340a89b..4d9cc4b 160000 --- a/include/BlueWest.MapTo +++ b/include/BlueWest.MapTo @@ -1 +1 @@ -Subproject commit 340a89bbd2e3d94cfb84f9a365d5e7db10f99bb8 +Subproject commit 4d9cc4b029b15049b5d9a8bb8e87f6d9a513b712