From 693bbc0c0ddb146c93bcb0ef8332e37bd08703d3 Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Wed, 24 Aug 2022 17:56:51 +0100 Subject: [PATCH] Support ef add method --- BlueWest.Api/BlueWest.Api.csproj | 3 ++ BlueWest.Api/Context/CompanyDbContext.cs | 30 ++++++++++++++----- BlueWest.Api/Context/CountryDbContext.cs | 3 ++ .../Context/Extensions/CountryDbExtensions.cs | 14 ++++----- .../Context/Extensions/CurrencyExtensions.cs | 8 ++--- BlueWest.Api/Controllers/CountryController.cs | 5 ++-- .../Controllers/CurrencyController.cs | 11 ++++--- BlueWest.Api/EFAddMethods.cs | 11 ------- BlueWest.Api/EFUpdateMethodsAttribute.cs | 11 +++++++ BlueWest.Api/EfAddToListMethodAttribute.cs | 15 ++++++++++ BlueWest.Api/EfUpdateMethods.cs | 11 ------- BlueWest.Api/Extensions/AddToListTemplate.csx | 18 +++++++++++ .../Company/CompanyUnique.cs | 24 +++++++++++++++ .../Industry/IndustryUnique.cs | 13 ++++++++ .../Transaction/FinanceOpType.cs | 4 +-- include/BlueWest.MapTo | 2 +- 16 files changed, 134 insertions(+), 49 deletions(-) delete mode 100644 BlueWest.Api/EFAddMethods.cs create mode 100644 BlueWest.Api/EFUpdateMethodsAttribute.cs create mode 100644 BlueWest.Api/EfAddToListMethodAttribute.cs delete mode 100644 BlueWest.Api/EfUpdateMethods.cs create mode 100644 BlueWest.Api/Extensions/AddToListTemplate.csx create mode 100644 BlueWest.Data.Capital/Company/CompanyUnique.cs create mode 100644 BlueWest.Data.Capital/Industry/IndustryUnique.cs diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index 386a0eb..024c4a6 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -8,6 +8,7 @@ true bin\$(Configuration)\$(AssemblyName).xml true + preview @@ -21,7 +22,9 @@ + + diff --git a/BlueWest.Api/Context/CompanyDbContext.cs b/BlueWest.Api/Context/CompanyDbContext.cs index 7a7c03a..6b0e8a8 100644 --- a/BlueWest.Api/Context/CompanyDbContext.cs +++ b/BlueWest.Api/Context/CompanyDbContext.cs @@ -14,23 +14,39 @@ namespace BlueWest.WebApi.EF /// /// Companies. /// - [EFAddMethods(typeof(CompanyCreate))] - [EFUpdateMethods(typeof(CompanyUpdate))] + [EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))] + [EFUpdateMethods(typeof(int), + "Id", + typeof(CompanyUpdate), + typeof(CompanyUnique))] public DbSet Companies { get; set; } /// /// Industries. /// - [EFAddMethods(typeof(IndustryCreate))] - [EFUpdateMethods(typeof(IndustryUpdate))] - public DbSet Industries { get; set; } + [EfAddMethods(typeof(IndustryCreate), typeof(IndustryUnique))] + [EFUpdateMethods(typeof(int), + "Id", + typeof(IndustryUpdate), + typeof(IndustryUnique))] + public DbSet Industries { get; set; } + + void TestExtensions() + { + // var (result, obj) = this.AddCountry(new CountryCreate()); + } /// /// Products. /// - [EFAddMethods(typeof(ProductCreate))] - [EFUpdateMethods(typeof(IndustryUpdate))] + /* + [EfAddMethods(typeof(ProductCreate))] + [EFUpdateMethods(typeof(int), + "Id", + typeof(ProductUpdate))] + */ + public DbSet Products { get; set; } diff --git a/BlueWest.Api/Context/CountryDbContext.cs b/BlueWest.Api/Context/CountryDbContext.cs index 923efe2..280ed87 100644 --- a/BlueWest.Api/Context/CountryDbContext.cs +++ b/BlueWest.Api/Context/CountryDbContext.cs @@ -1,5 +1,6 @@ using BlueWest.Data; using BlueWest.WebApi.EF.Model; +using MapTo; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; @@ -15,11 +16,13 @@ namespace BlueWest.WebApi.EF /// /// Countries Database Table /// + [EfAddMethods(typeof(CountryCreate), typeof(CountryUnique))] public DbSet Countries { get; set; } /// /// Currencies Database Table /// + [EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))] public DbSet Currencies { get; set; } /// diff --git a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs index 4864aff..b88cfde 100644 --- a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs @@ -19,13 +19,13 @@ namespace BlueWest.WebApi.EF /// /// /// - public static (bool, CountryUnique) AddCountry(this CountryDbContext dbContext, CountryCreate countryCreate) + /*public static (bool, CountryUnique) AddCountry(this CountryDbContext dbContext, CountryCreate countryCreate) { Country newCountry = new Country(countryCreate); dbContext.Countries.Add(newCountry); bool success = dbContext.SaveChanges() >= 0; return (success, new CountryUnique(newCountry)); - } + } */ /// /// Updates a country data. /// @@ -54,7 +54,7 @@ namespace BlueWest.WebApi.EF /// /// /// - public static (bool, string, Country) AddCurrency( + /*public static (bool, string, CurrencyUnique) AddCurrency( this CountryDbContext dbContext, int countryId, CurrencyCreate currencyCreate) @@ -69,8 +69,8 @@ namespace BlueWest.WebApi.EF country.Currencies.Add(newCurrency); var success = dbContext.SaveChanges() >= 0; - return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, country); - } + return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, new CurrencyUnique(newCurrency)); + }*/ // country add (add currency) currency create, duplicatioon Validations /// @@ -81,7 +81,7 @@ namespace BlueWest.WebApi.EF /// Data to create currency /// List of expressions /// - public static (bool, string, CurrencyUnique) AddCurrency( + /*public static (bool, string, CurrencyUnique) AddCurrency( this CountryDbContext dbContext, int countryId, CurrencyCreate currencyCreate, Expression>[] duplicationValidations) @@ -109,7 +109,7 @@ namespace BlueWest.WebApi.EF country.Currencies.Add(newCurrency); var success = dbContext.SaveChanges() >= 0; return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, new CurrencyUnique(newCurrency)); - } + }*/ } } diff --git a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs index e3ef7d7..f794ae7 100644 --- a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; -using System.Threading.Tasks; using BlueWest.Data; using Microsoft.EntityFrameworkCore; @@ -20,6 +18,7 @@ namespace BlueWest.WebApi.EF /// /// /// + /* public static (bool, Currency) AddCurrency(this CountryDbContext dbContext, CurrencyCreate currencyToCreate) { var newCurrency = new Currency(currencyToCreate); @@ -27,6 +26,7 @@ namespace BlueWest.WebApi.EF var resultOperation = dbContext.SaveChanges() >= 0; return (resultOperation, newCurrency); } + */ /// @@ -55,7 +55,7 @@ namespace BlueWest.WebApi.EF /// /// /// - public static (bool, string, CountryUnique) AddCountry( + /*public static (bool, string, CountryUnique) AddCountry( this CountryDbContext dbContext, int currencyId, CountryCreate countryCreate, Expression>[] duplicationValidations) @@ -88,7 +88,7 @@ namespace BlueWest.WebApi.EF currency.Countries.Add(newCountry); var success = dbContext.SaveChanges() >= 0; return (success, string.Empty, new CountryUnique(newCountry)); - } + }*/ } } diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index d76d95b..ebc2d03 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -163,7 +163,7 @@ namespace BlueWest.WebApi.Controllers [HttpPost("{countryId}/currencies")] public ActionResult AddCurrency(int countryId, CurrencyCreate currencyCreate) { - var (result, message, country) = _dbContext.AddCurrency(countryId, currencyCreate, + /*var (result, message, country) = _dbContext.AddCurrency(countryId, currencyCreate, new Expression>[] { x => x.Code == currencyCreate.Code, @@ -175,7 +175,8 @@ namespace BlueWest.WebApi.Controllers return new ConflictObjectResult(message); } - return Ok(country); + return Ok(country);*/ + return null; } diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index 913c441..feb6b0f 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -77,7 +77,7 @@ namespace BlueWest.WebApi.Controllers [HttpPost] public ActionResult AddCurrency(CurrencyCreate currencyToCreate) { - var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate); + /*var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate); if (!success) { @@ -85,7 +85,9 @@ namespace BlueWest.WebApi.Controllers } return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, - new CurrencyUnique(newCurrency)); + new CurrencyUnique(newCurrency));*/ + + return null; } /// @@ -149,7 +151,7 @@ namespace BlueWest.WebApi.Controllers [HttpPost("{currencyId}/countries")] public ActionResult AddCountry(int currencyId, CountryCreate countryToCreate) { - var (success, message, country) = _dbContext.AddCountry(currencyId, countryToCreate, + /*var (success, message, country) = _dbContext.AddCountry(currencyId, countryToCreate, new Expression>[] { x => x.Code == countryToCreate.Code, @@ -162,7 +164,8 @@ namespace BlueWest.WebApi.Controllers return new ConflictObjectResult(message); } - return Ok(country); + return Ok(country);*/ + return null; } } diff --git a/BlueWest.Api/EFAddMethods.cs b/BlueWest.Api/EFAddMethods.cs deleted file mode 100644 index 8afc4c7..0000000 --- a/BlueWest.Api/EFAddMethods.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace BlueWest.WebApi -{ - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] - public class EFAddMethods : Attribute - { - public EFAddMethods(Type createDto = null) { } - } -} - diff --git a/BlueWest.Api/EFUpdateMethodsAttribute.cs b/BlueWest.Api/EFUpdateMethodsAttribute.cs new file mode 100644 index 0000000..74f83f1 --- /dev/null +++ b/BlueWest.Api/EFUpdateMethodsAttribute.cs @@ -0,0 +1,11 @@ +using System; + +namespace BlueWest.WebApi +{ + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] + public class EFUpdateMethodsAttribute : Attribute + { + public EFUpdateMethodsAttribute( Type keyType = null, string keyName = "Id", Type updateDto = null, Type returnType = null) { } + } +} + diff --git a/BlueWest.Api/EfAddToListMethodAttribute.cs b/BlueWest.Api/EfAddToListMethodAttribute.cs new file mode 100644 index 0000000..1020b9d --- /dev/null +++ b/BlueWest.Api/EfAddToListMethodAttribute.cs @@ -0,0 +1,15 @@ +using System; + +namespace BlueWest.WebApi +{ + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] + public class EfAddToListMethodAttribute : Attribute + { + public EfAddToListMethodAttribute( Type keyType = null, string keyName = "Id", Type updateDto = null, Type returnType = null) + { + + } + } + +} + diff --git a/BlueWest.Api/EfUpdateMethods.cs b/BlueWest.Api/EfUpdateMethods.cs deleted file mode 100644 index 7024f1a..0000000 --- a/BlueWest.Api/EfUpdateMethods.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace BlueWest.WebApi -{ - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] - public class EFUpdateMethods : Attribute - { - public EFUpdateMethods(Type updateDto = null) { } - } -} - diff --git a/BlueWest.Api/Extensions/AddToListTemplate.csx b/BlueWest.Api/Extensions/AddToListTemplate.csx new file mode 100644 index 0000000..95e5393 --- /dev/null +++ b/BlueWest.Api/Extensions/AddToListTemplate.csx @@ -0,0 +1,18 @@ + +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.Data.Capital/Company/CompanyUnique.cs b/BlueWest.Data.Capital/Company/CompanyUnique.cs new file mode 100644 index 0000000..bad00f5 --- /dev/null +++ b/BlueWest.Data.Capital/Company/CompanyUnique.cs @@ -0,0 +1,24 @@ +using System; +using MapTo; + +namespace BlueWest.Data +{ + [MapFrom(typeof(Company))] + public partial class CompanyUnique + { + public int Id { get; set; } + + public string Name { get; set; } + + public string Address { get; set; } + + public CompanyType CompanyType { get; set; } + + public Country CurrentCountry { get; set; } + + public Country OriginCountry { get; set; } + + public DateTime FoundingDate { get; set; } + } +} + diff --git a/BlueWest.Data.Capital/Industry/IndustryUnique.cs b/BlueWest.Data.Capital/Industry/IndustryUnique.cs new file mode 100644 index 0000000..3cc9216 --- /dev/null +++ b/BlueWest.Data.Capital/Industry/IndustryUnique.cs @@ -0,0 +1,13 @@ +using MapTo; + +namespace BlueWest.Data +{ + [MapFrom(typeof(Industry))] + public partial class IndustryUnique + { + public int Id { get; set; } + public string IndustryName { get; set; } + public Industry IndustryParent { get; set; } + } +} + diff --git a/BlueWest.Data.Capital/Transaction/FinanceOpType.cs b/BlueWest.Data.Capital/Transaction/FinanceOpType.cs index ab49a3f..832e7e4 100644 --- a/BlueWest.Data.Capital/Transaction/FinanceOpType.cs +++ b/BlueWest.Data.Capital/Transaction/FinanceOpType.cs @@ -6,9 +6,9 @@ namespace BlueWest.Data { [Key] public int Id { get; set; } - public string Name; + public string Name { get; set; } - private string Description; + public string Description { get; set; } } } diff --git a/include/BlueWest.MapTo b/include/BlueWest.MapTo index 7de9b48..340a89b 160000 --- a/include/BlueWest.MapTo +++ b/include/BlueWest.MapTo @@ -1 +1 @@ -Subproject commit 7de9b48c69bb86619278f4bec7a53cd26745dd05 +Subproject commit 340a89bbd2e3d94cfb84f9a365d5e7db10f99bb8