From 96b6f7839ff63cb52964cc90b35eeddfc3fb1804 Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Mon, 29 Aug 2022 01:40:57 +0100 Subject: [PATCH] Update template working --- BlueWest.Api/BlueWest.Api.csproj | 2 ++ BlueWest.Api/Context/CompanyDbContext.cs | 21 +++++++++++-------- BlueWest.Api/Context/CountryDbContext.cs | 15 +++++++------ .../Context/EfAddUpdateMethodsAttribute.cs | 11 ---------- .../Context/Extensions/CountryExtensions2.cs | 4 ++-- .../Context/Extensions/CurrencyExtensions.cs | 4 ++-- .../Templates/UpdateEntityTemplate.csx | 4 ++-- BlueWest.Api/Controllers/CountryController.cs | 3 +-- .../Controllers/CurrencyController.cs | 4 ++-- 9 files changed, 30 insertions(+), 38 deletions(-) delete mode 100644 BlueWest.Api/Context/EfAddUpdateMethodsAttribute.cs diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index 6018318..abae3ba 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -19,6 +19,8 @@ + + diff --git a/BlueWest.Api/Context/CompanyDbContext.cs b/BlueWest.Api/Context/CompanyDbContext.cs index 53a2cd6..c40f13a 100644 --- a/BlueWest.Api/Context/CompanyDbContext.cs +++ b/BlueWest.Api/Context/CompanyDbContext.cs @@ -1,6 +1,5 @@ using BlueWest.Data; -using BlueWest.WebApi.Context; using BlueWest.WebApi.EF.Model; using MapTo; using Microsoft.EntityFrameworkCore; @@ -16,11 +15,13 @@ namespace BlueWest.WebApi.EF /// Companies. /// [EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))] - [EfAddUpdateMethods( - typeof(CompanyUnique), - typeof(CompanyUpdate), - "Id", typeof(int)) + [EfUpdateMethods( + updateType: typeof(CompanyUpdate), + returnType: typeof(CompanyUnique), + keyPropertyMemberName: "Id", + keyPropertyMemberType: typeof(int)) ] + /*[EFUpdateMethods(typeof(int), "Id", typeof(CompanyUpdate), @@ -32,10 +33,12 @@ namespace BlueWest.WebApi.EF /// Industries. /// [EfAddMethods(typeof(IndustryCreate), typeof(IndustryUnique))] - /*[EFUpdateMethods(typeof(int), - "Id", - typeof(IndustryUpdate), - typeof(IndustryUnique))]*/ + [EfUpdateMethods( + updateType: typeof(IndustryUpdate), + returnType: typeof(IndustryUnique), + keyPropertyMemberName: "Id", + keyPropertyMemberType: typeof(int)) + ] public DbSet Industries { get; set; } diff --git a/BlueWest.Api/Context/CountryDbContext.cs b/BlueWest.Api/Context/CountryDbContext.cs index d6a276d..37bf0be 100644 --- a/BlueWest.Api/Context/CountryDbContext.cs +++ b/BlueWest.Api/Context/CountryDbContext.cs @@ -1,5 +1,4 @@ using BlueWest.Data; -using BlueWest.WebApi.Context; using BlueWest.WebApi.EF.Model; using MapTo; using Microsoft.EntityFrameworkCore; @@ -18,15 +17,15 @@ namespace BlueWest.WebApi.EF /// Countries Database Table /// [EfAddMethods( - createDto: typeof(CountryCreate), + createType: typeof(CountryCreate), returnType: typeof(CountryUnique)) ] - [EfAddUpdateMethods( + [EfUpdateMethods( updateType: typeof(CountryUpdate), returnType: typeof(CountryUnique), - keyPropertyName: "Id", - keyPropertyType: typeof(System.Int64)) + keyPropertyMemberName: "Id", + keyPropertyMemberType: typeof(int)) ] public DbSet Countries { get; set; } @@ -36,11 +35,11 @@ namespace BlueWest.WebApi.EF // Generate Add extension methods [EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))] - [EfAddUpdateMethods( + [EfUpdateMethods( updateType: typeof(CurrencyUpdate), returnType: typeof(CurrencyUnique), - keyPropertyName: "Id", - keyPropertyType: typeof(System.Int64)) + keyPropertyMemberName: "Id", + keyPropertyMemberType: typeof(int)) ] public DbSet Currencies { get; set; } diff --git a/BlueWest.Api/Context/EfAddUpdateMethodsAttribute.cs b/BlueWest.Api/Context/EfAddUpdateMethodsAttribute.cs deleted file mode 100644 index 8959196..0000000 --- a/BlueWest.Api/Context/EfAddUpdateMethodsAttribute.cs +++ /dev/null @@ -1,11 +0,0 @@ -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/Extensions/CountryExtensions2.cs b/BlueWest.Api/Context/Extensions/CountryExtensions2.cs index 9fbd688..62d6588 100644 --- a/BlueWest.Api/Context/Extensions/CountryExtensions2.cs +++ b/BlueWest.Api/Context/Extensions/CountryExtensions2.cs @@ -33,7 +33,7 @@ namespace BlueWest.WebApi.EF /// Country data to update. /// Country Id. /// - public static (bool, Country) UpdateCountry( + /*public static (bool, Country) UpdateCountry( this CountryDbContext dbContext, CountryUpdate countryUpdate, int countryId) @@ -45,7 +45,7 @@ namespace BlueWest.WebApi.EF var result = dbContext.SaveChanges() >= 0; return (result, country); - } + }*/ /// /// Adds a new Currency to the specified country diff --git a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs index f794ae7..5bb1bc4 100644 --- a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs @@ -36,7 +36,7 @@ namespace BlueWest.WebApi.EF /// /// /// - public static (bool, Currency) UpdateCurrency(this CountryDbContext dbContext, int currencyId, CurrencyUpdate currencyToUpdate) + /*public static (bool, Currency) UpdateCurrency(this CountryDbContext dbContext, int currencyId, CurrencyUpdate currencyToUpdate) { var currency = dbContext.Currencies.SingleOrDefault(x => x.Id == currencyId); if (currency == null) return (false, null); @@ -45,7 +45,7 @@ namespace BlueWest.WebApi.EF var resultOperation = dbContext.SaveChanges() >= 0; return (resultOperation, currency); - } + }*/ /// /// Add Country associated with specified Currency. diff --git a/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx b/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx index 0919b5c..33147a4 100644 --- a/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx +++ b/BlueWest.Api/Context/Templates/UpdateEntityTemplate.csx @@ -6,8 +6,8 @@ public static (bool, {returnTypeFullName}) Update{entityTypeName}( var {existingEntityVarName} = dbContext.{propertyName}.FirstOrDefault(x => x.{keyPropertyName} == {keyVarName}); if ({existingEntityVarName} == null) return (false, null); {existingEntityVarName}.Update({updateVarName}); - dbContext.{propertyname}.Update({existingEntityVarName}); + dbContext.{propertyName}.Update({existingEntityVarName}); var result = dbContext.SaveChanges() >= 0; - return (result, {existingEntityVarName}); + return (result, new {returnTypeFullName}({existingEntityVarName})); } \ No newline at end of file diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index ebc2d03..27cf9e4 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -144,8 +144,7 @@ namespace BlueWest.WebApi.Controllers if (success) { - var countryReply = new CountryUnique(country); - return Ok(countryReply); + return Ok(country); } return new NotFoundResult(); diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index feb6b0f..41b8b6d 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -101,11 +101,11 @@ namespace BlueWest.WebApi.Controllers [HttpPut("{currencyNumber}")] public ActionResult UpdateCurrency(int currencyNumber, CurrencyUpdate currencyToUpdate) { - var (success, currency) = _dbContext.UpdateCurrency(currencyNumber, currencyToUpdate); + var (success, currency) = _dbContext.UpdateCurrency(currencyToUpdate, currencyNumber); if (success) { - return Ok(new CurrencyUnique(currency)); + return Ok(currency); } return new NotFoundResult();