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>
<AdditionalFiles Include=".\Context\Templates\AddToEntityTemplate.csx" />
<AdditionalFiles Include=".\Context\Templates\UpdateEntityTemplate.csx" />
</ItemGroup>
<ItemGroup>

View File

@ -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.
/// </summary>
[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.
/// </summary>
[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<Industry> Industries { get; set; }

View File

@ -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
/// </summary>
[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<Country> 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<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="countryId">Country Id.</param>
/// <returns></returns>
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);
}
}*/
/// <summary>
/// Adds a new Currency to the specified country

View File

@ -36,7 +36,7 @@ namespace BlueWest.WebApi.EF
/// <param name="currencyId"></param>
/// <param name="currencyToUpdate"></param>
/// <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);
if (currency == null) return (false, null);
@ -45,7 +45,7 @@ namespace BlueWest.WebApi.EF
var resultOperation = dbContext.SaveChanges() >= 0;
return (resultOperation, currency);
}
}*/
/// <summary>
/// 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});
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}));
}

View File

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

View File

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