Support ef add method

This commit is contained in:
CodeLiturgy 2022-08-24 17:56:51 +01:00
parent 5c5e9834f2
commit 693bbc0c0d
16 changed files with 134 additions and 49 deletions

View File

@ -8,6 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).xml</DocumentationFile> <DocumentationFile>bin\$(Configuration)\$(AssemblyName).xml</DocumentationFile>
<PublishDependencyDocumentationFiles>true</PublishDependencyDocumentationFiles> <PublishDependencyDocumentationFiles>true</PublishDependencyDocumentationFiles>
<AnalysisLevel>preview</AnalysisLevel>
</PropertyGroup> </PropertyGroup>
@ -21,7 +22,9 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\BlueWest.Data.Capital\BlueWest.Data.Capital.csproj" /> <ProjectReference Include="..\BlueWest.Data.Capital\BlueWest.Data.Capital.csproj" />
<ProjectReference Include="..\BlueWest\BlueWest.csproj" /> <ProjectReference Include="..\BlueWest\BlueWest.csproj" />
<ProjectReference Include="..\include\BlueWest.MapTo\src\BlueWest.MapTo\BlueWest.MapTo.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup> </ItemGroup>
<Import Project="..\include\BlueWest.MapTo\src\BlueWest.MapTo\MapTo.props" />
</Project> </Project>

View File

@ -14,23 +14,39 @@ namespace BlueWest.WebApi.EF
/// <summary> /// <summary>
/// Companies. /// Companies.
/// </summary> /// </summary>
[EFAddMethods(typeof(CompanyCreate))] [EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))]
[EFUpdateMethods(typeof(CompanyUpdate))] [EFUpdateMethods(typeof(int),
"Id",
typeof(CompanyUpdate),
typeof(CompanyUnique))]
public DbSet<Company> Companies { get; set; } public DbSet<Company> Companies { get; set; }
/// <summary> /// <summary>
/// Industries. /// Industries.
/// </summary> /// </summary>
[EFAddMethods(typeof(IndustryCreate))] [EfAddMethods(typeof(IndustryCreate), typeof(IndustryUnique))]
[EFUpdateMethods(typeof(IndustryUpdate))] [EFUpdateMethods(typeof(int),
public DbSet<Industry> Industries { get; set; } "Id",
typeof(IndustryUpdate),
typeof(IndustryUnique))]
public DbSet<Industry> Industries { get; set; }
void TestExtensions()
{
// var (result, obj) = this.AddCountry(new CountryCreate());
}
/// <summary> /// <summary>
/// Products. /// Products.
/// </summary> /// </summary>
[EFAddMethods(typeof(ProductCreate))] /*
[EFUpdateMethods(typeof(IndustryUpdate))] [EfAddMethods(typeof(ProductCreate))]
[EFUpdateMethods(typeof(int),
"Id",
typeof(ProductUpdate))]
*/
public DbSet<Product> Products { get; set; } public DbSet<Product> Products { get; set; }

View File

@ -1,5 +1,6 @@
using BlueWest.Data; using BlueWest.Data;
using BlueWest.WebApi.EF.Model; using BlueWest.WebApi.EF.Model;
using MapTo;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -15,11 +16,13 @@ namespace BlueWest.WebApi.EF
/// <summary> /// <summary>
/// Countries Database Table /// Countries Database Table
/// </summary> /// </summary>
[EfAddMethods(typeof(CountryCreate), typeof(CountryUnique))]
public DbSet<Country> Countries { get; set; } public DbSet<Country> Countries { get; set; }
/// <summary> /// <summary>
/// Currencies Database Table /// Currencies Database Table
/// </summary> /// </summary>
[EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))]
public DbSet<Currency> Currencies { get; set; } public DbSet<Currency> Currencies { get; set; }
/// <summary> /// <summary>

View File

@ -19,13 +19,13 @@ namespace BlueWest.WebApi.EF
/// <param name="dbContext"></param> /// <param name="dbContext"></param>
/// <param name="countryCreate"></param> /// <param name="countryCreate"></param>
/// <returns></returns> /// <returns></returns>
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); Country newCountry = new Country(countryCreate);
dbContext.Countries.Add(newCountry); dbContext.Countries.Add(newCountry);
bool success = dbContext.SaveChanges() >= 0; bool success = dbContext.SaveChanges() >= 0;
return (success, new CountryUnique(newCountry)); return (success, new CountryUnique(newCountry));
} } */
/// <summary> /// <summary>
/// Updates a country data. /// Updates a country data.
/// </summary> /// </summary>
@ -54,7 +54,7 @@ namespace BlueWest.WebApi.EF
/// <param name="countryId"></param> /// <param name="countryId"></param>
/// <param name="currencyCreate"></param> /// <param name="currencyCreate"></param>
/// <returns></returns> /// <returns></returns>
public static (bool, string, Country) AddCurrency( /*public static (bool, string, CurrencyUnique) AddCurrency(
this CountryDbContext dbContext, this CountryDbContext dbContext,
int countryId, int countryId,
CurrencyCreate currencyCreate) CurrencyCreate currencyCreate)
@ -69,8 +69,8 @@ namespace BlueWest.WebApi.EF
country.Currencies.Add(newCurrency); country.Currencies.Add(newCurrency);
var success = dbContext.SaveChanges() >= 0; 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 // country add (add currency) currency create, duplicatioon Validations
/// <summary> /// <summary>
@ -81,7 +81,7 @@ namespace BlueWest.WebApi.EF
/// <param name="currencyCreate">Data to create currency</param> /// <param name="currencyCreate">Data to create currency</param>
/// <param name="duplicationValidations">List of expressions</param> /// <param name="duplicationValidations">List of expressions</param>
/// <returns></returns> /// <returns></returns>
public static (bool, string, CurrencyUnique) AddCurrency( /*public static (bool, string, CurrencyUnique) AddCurrency(
this CountryDbContext dbContext, this CountryDbContext dbContext,
int countryId, CurrencyCreate currencyCreate, int countryId, CurrencyCreate currencyCreate,
Expression<Func<Currency,bool>>[] duplicationValidations) Expression<Func<Currency,bool>>[] duplicationValidations)
@ -109,7 +109,7 @@ namespace BlueWest.WebApi.EF
country.Currencies.Add(newCurrency); country.Currencies.Add(newCurrency);
var success = dbContext.SaveChanges() >= 0; var success = dbContext.SaveChanges() >= 0;
return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, new CurrencyUnique(newCurrency)); return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, new CurrencyUnique(newCurrency));
} }*/
} }
} }

View File

@ -1,8 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks;
using BlueWest.Data; using BlueWest.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -20,6 +18,7 @@ namespace BlueWest.WebApi.EF
/// <param name="dbContext"></param> /// <param name="dbContext"></param>
/// <param name="currencyToCreate"></param> /// <param name="currencyToCreate"></param>
/// <returns></returns> /// <returns></returns>
/*
public static (bool, Currency) AddCurrency(this CountryDbContext dbContext, CurrencyCreate currencyToCreate) public static (bool, Currency) AddCurrency(this CountryDbContext dbContext, CurrencyCreate currencyToCreate)
{ {
var newCurrency = new Currency(currencyToCreate); var newCurrency = new Currency(currencyToCreate);
@ -27,6 +26,7 @@ namespace BlueWest.WebApi.EF
var resultOperation = dbContext.SaveChanges() >= 0; var resultOperation = dbContext.SaveChanges() >= 0;
return (resultOperation, newCurrency); return (resultOperation, newCurrency);
} }
*/
/// <summary> /// <summary>
@ -55,7 +55,7 @@ namespace BlueWest.WebApi.EF
/// <param name="countryCreate"></param> /// <param name="countryCreate"></param>
/// <param name="duplicationValidations"></param> /// <param name="duplicationValidations"></param>
/// <returns></returns> /// <returns></returns>
public static (bool, string, CountryUnique) AddCountry( /*public static (bool, string, CountryUnique) AddCountry(
this CountryDbContext dbContext, this CountryDbContext dbContext,
int currencyId, CountryCreate countryCreate, int currencyId, CountryCreate countryCreate,
Expression<Func<Country,bool>>[] duplicationValidations) Expression<Func<Country,bool>>[] duplicationValidations)
@ -88,7 +88,7 @@ namespace BlueWest.WebApi.EF
currency.Countries.Add(newCountry); currency.Countries.Add(newCountry);
var success = dbContext.SaveChanges() >= 0; var success = dbContext.SaveChanges() >= 0;
return (success, string.Empty, new CountryUnique(newCountry)); return (success, string.Empty, new CountryUnique(newCountry));
} }*/
} }
} }

View File

@ -163,7 +163,7 @@ namespace BlueWest.WebApi.Controllers
[HttpPost("{countryId}/currencies")] [HttpPost("{countryId}/currencies")]
public ActionResult AddCurrency(int countryId, CurrencyCreate currencyCreate) 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<Func<Currency, bool>>[] new Expression<Func<Currency, bool>>[]
{ {
x => x.Code == currencyCreate.Code, x => x.Code == currencyCreate.Code,
@ -175,7 +175,8 @@ namespace BlueWest.WebApi.Controllers
return new ConflictObjectResult(message); return new ConflictObjectResult(message);
} }
return Ok(country); return Ok(country);*/
return null;
} }

View File

@ -77,7 +77,7 @@ namespace BlueWest.WebApi.Controllers
[HttpPost] [HttpPost]
public ActionResult AddCurrency(CurrencyCreate currencyToCreate) public ActionResult AddCurrency(CurrencyCreate currencyToCreate)
{ {
var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate); /*var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate);
if (!success) if (!success)
{ {
@ -85,7 +85,9 @@ namespace BlueWest.WebApi.Controllers
} }
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code},
new CurrencyUnique(newCurrency)); new CurrencyUnique(newCurrency));*/
return null;
} }
/// <summary> /// <summary>
@ -149,7 +151,7 @@ namespace BlueWest.WebApi.Controllers
[HttpPost("{currencyId}/countries")] [HttpPost("{currencyId}/countries")]
public ActionResult AddCountry(int currencyId, CountryCreate countryToCreate) 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<Func<Country, bool>>[] new Expression<Func<Country, bool>>[]
{ {
x => x.Code == countryToCreate.Code, x => x.Code == countryToCreate.Code,
@ -162,7 +164,8 @@ namespace BlueWest.WebApi.Controllers
return new ConflictObjectResult(message); return new ConflictObjectResult(message);
} }
return Ok(country); return Ok(country);*/
return null;
} }
} }

View File

@ -1,11 +0,0 @@
using System;
namespace BlueWest.WebApi
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class EFAddMethods : Attribute
{
public EFAddMethods(Type createDto = null) { }
}
}

View File

@ -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) { }
}
}

View File

@ -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)
{
}
}
}

View File

@ -1,11 +0,0 @@
using System;
namespace BlueWest.WebApi
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class EFUpdateMethods : Attribute
{
public EFUpdateMethods(Type updateDto = null) { }
}
}

View File

@ -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));
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -6,9 +6,9 @@ namespace BlueWest.Data
{ {
[Key] public int Id { get; set; } [Key] public int Id { get; set; }
public string Name; public string Name { get; set; }
private string Description; public string Description { get; set; }
} }
} }

@ -1 +1 @@
Subproject commit 7de9b48c69bb86619278f4bec7a53cd26745dd05 Subproject commit 340a89bbd2e3d94cfb84f9a365d5e7db10f99bb8