Update template working

This commit is contained in:
CodeLiturgy 2022-08-29 01:40:57 +01:00
parent a54e2b051c
commit 96b6f7839f
9 changed files with 30 additions and 38 deletions

View File

@ -19,6 +19,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<AdditionalFiles Include=".\Context\Templates\AddToEntityTemplate.csx" /> <AdditionalFiles Include=".\Context\Templates\AddToEntityTemplate.csx" />
<AdditionalFiles Include=".\Context\Templates\UpdateEntityTemplate.csx" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,6 +1,5 @@
using BlueWest.Data; using BlueWest.Data;
using BlueWest.WebApi.Context;
using BlueWest.WebApi.EF.Model; using BlueWest.WebApi.EF.Model;
using MapTo; using MapTo;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -16,11 +15,13 @@ namespace BlueWest.WebApi.EF
/// Companies. /// Companies.
/// </summary> /// </summary>
[EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))] [EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))]
[EfAddUpdateMethods( [EfUpdateMethods(
typeof(CompanyUnique), updateType: typeof(CompanyUpdate),
typeof(CompanyUpdate), returnType: typeof(CompanyUnique),
"Id", typeof(int)) keyPropertyMemberName: "Id",
keyPropertyMemberType: typeof(int))
] ]
/*[EFUpdateMethods(typeof(int), /*[EFUpdateMethods(typeof(int),
"Id", "Id",
typeof(CompanyUpdate), typeof(CompanyUpdate),
@ -32,10 +33,12 @@ namespace BlueWest.WebApi.EF
/// Industries. /// Industries.
/// </summary> /// </summary>
[EfAddMethods(typeof(IndustryCreate), typeof(IndustryUnique))] [EfAddMethods(typeof(IndustryCreate), typeof(IndustryUnique))]
/*[EFUpdateMethods(typeof(int), [EfUpdateMethods(
"Id", updateType: typeof(IndustryUpdate),
typeof(IndustryUpdate), returnType: typeof(IndustryUnique),
typeof(IndustryUnique))]*/ keyPropertyMemberName: "Id",
keyPropertyMemberType: typeof(int))
]
public DbSet<Industry> Industries { get; set; } public DbSet<Industry> Industries { get; set; }

View File

@ -1,5 +1,4 @@
using BlueWest.Data; using BlueWest.Data;
using BlueWest.WebApi.Context;
using BlueWest.WebApi.EF.Model; using BlueWest.WebApi.EF.Model;
using MapTo; using MapTo;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -18,15 +17,15 @@ namespace BlueWest.WebApi.EF
/// Countries Database Table /// Countries Database Table
/// </summary> /// </summary>
[EfAddMethods( [EfAddMethods(
createDto: typeof(CountryCreate), createType: typeof(CountryCreate),
returnType: typeof(CountryUnique)) returnType: typeof(CountryUnique))
] ]
[EfAddUpdateMethods( [EfUpdateMethods(
updateType: typeof(CountryUpdate), updateType: typeof(CountryUpdate),
returnType: typeof(CountryUnique), returnType: typeof(CountryUnique),
keyPropertyName: "Id", keyPropertyMemberName: "Id",
keyPropertyType: typeof(System.Int64)) keyPropertyMemberType: typeof(int))
] ]
public DbSet<Country> Countries { get; set; } public DbSet<Country> Countries { get; set; }
@ -36,11 +35,11 @@ namespace BlueWest.WebApi.EF
// Generate Add extension methods // Generate Add extension methods
[EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))] [EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))]
[EfAddUpdateMethods( [EfUpdateMethods(
updateType: typeof(CurrencyUpdate), updateType: typeof(CurrencyUpdate),
returnType: typeof(CurrencyUnique), returnType: typeof(CurrencyUnique),
keyPropertyName: "Id", keyPropertyMemberName: "Id",
keyPropertyType: typeof(System.Int64)) keyPropertyMemberType: typeof(int))
] ]
public DbSet<Currency> Currencies { get; set; } public DbSet<Currency> Currencies { get; set; }

View File

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

View File

@ -33,7 +33,7 @@ namespace BlueWest.WebApi.EF
/// <param name="countryUpdate">Country data to update.</param> /// <param name="countryUpdate">Country data to update.</param>
/// <param name="countryId">Country Id.</param> /// <param name="countryId">Country Id.</param>
/// <returns></returns> /// <returns></returns>
public static (bool, Country) UpdateCountry( /*public static (bool, Country) UpdateCountry(
this CountryDbContext dbContext, this CountryDbContext dbContext,
CountryUpdate countryUpdate, CountryUpdate countryUpdate,
int countryId) int countryId)
@ -45,7 +45,7 @@ namespace BlueWest.WebApi.EF
var result = dbContext.SaveChanges() >= 0; var result = dbContext.SaveChanges() >= 0;
return (result, country); return (result, country);
} }*/
/// <summary> /// <summary>
/// Adds a new Currency to the specified country /// Adds a new Currency to the specified country

View File

@ -36,7 +36,7 @@ namespace BlueWest.WebApi.EF
/// <param name="currencyId"></param> /// <param name="currencyId"></param>
/// <param name="currencyToUpdate"></param> /// <param name="currencyToUpdate"></param>
/// <returns></returns> /// <returns></returns>
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); var currency = dbContext.Currencies.SingleOrDefault(x => x.Id == currencyId);
if (currency == null) return (false, null); if (currency == null) return (false, null);
@ -45,7 +45,7 @@ namespace BlueWest.WebApi.EF
var resultOperation = dbContext.SaveChanges() >= 0; var resultOperation = dbContext.SaveChanges() >= 0;
return (resultOperation, currency); return (resultOperation, currency);
} }*/
/// <summary> /// <summary>
/// Add Country associated with specified Currency. /// Add Country associated with specified Currency.

View File

@ -6,8 +6,8 @@ public static (bool, {returnTypeFullName}) Update{entityTypeName}(
var {existingEntityVarName} = dbContext.{propertyName}.FirstOrDefault(x => x.{keyPropertyName} == {keyVarName}); var {existingEntityVarName} = dbContext.{propertyName}.FirstOrDefault(x => x.{keyPropertyName} == {keyVarName});
if ({existingEntityVarName} == null) return (false, null); if ({existingEntityVarName} == null) return (false, null);
{existingEntityVarName}.Update({updateVarName}); {existingEntityVarName}.Update({updateVarName});
dbContext.{propertyname}.Update({existingEntityVarName}); dbContext.{propertyName}.Update({existingEntityVarName});
var result = dbContext.SaveChanges() >= 0; var result = dbContext.SaveChanges() >= 0;
return (result, {existingEntityVarName}); return (result, new {returnTypeFullName}({existingEntityVarName}));
} }

View File

@ -144,8 +144,7 @@ namespace BlueWest.WebApi.Controllers
if (success) if (success)
{ {
var countryReply = new CountryUnique(country); return Ok(country);
return Ok(countryReply);
} }
return new NotFoundResult(); return new NotFoundResult();

View File

@ -101,11 +101,11 @@ namespace BlueWest.WebApi.Controllers
[HttpPut("{currencyNumber}")] [HttpPut("{currencyNumber}")]
public ActionResult UpdateCurrency(int currencyNumber, CurrencyUpdate currencyToUpdate) public ActionResult UpdateCurrency(int currencyNumber, CurrencyUpdate currencyToUpdate)
{ {
var (success, currency) = _dbContext.UpdateCurrency(currencyNumber, currencyToUpdate); var (success, currency) = _dbContext.UpdateCurrency(currencyToUpdate, currencyNumber);
if (success) if (success)
{ {
return Ok(new CurrencyUnique(currency)); return Ok(currency);
} }
return new NotFoundResult(); return new NotFoundResult();