ok state before introducing update

This commit is contained in:
CodeLiturgy 2022-08-28 19:39:40 +01:00
parent 13627dbcb6
commit e702debe48
7 changed files with 22 additions and 41 deletions

View File

@ -1,5 +1,6 @@
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;
@ -15,6 +16,11 @@ namespace BlueWest.WebApi.EF
/// Companies. /// Companies.
/// </summary> /// </summary>
[EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))] [EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))]
[EfAddUpdateMethods(
typeof(CompanyUnique),
typeof(CompanyUpdate),
"Id", typeof(int))
]
/*[EFUpdateMethods(typeof(int), /*[EFUpdateMethods(typeof(int),
"Id", "Id",
typeof(CompanyUpdate), typeof(CompanyUpdate),

View File

@ -10,7 +10,7 @@ namespace BlueWest.WebApi.EF
/// <summary> /// <summary>
/// Country table database extensions /// Country table database extensions
/// </summary> /// </summary>
public static class CountryDbExtensions public static class CountryExtensions2
{ {
/// <summary> /// <summary>

View File

@ -1,8 +1,8 @@
public static (bool, {readTypeFullName}) Add{entityTypeName}( public static (bool, {returnTypeFullName}) Add{entityTypeName}(
this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName}) { this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName}) {
var {newEntityVarName} = new {entityTypeFullName}({toCreateVarName}); var {newEntityVarName} = new {entityTypeFullName}({toCreateVarName});
dbContext.{propertyName}.Add({newEntityVarName}); dbContext.{propertyName}.Add({newEntityVarName});
var success = dbContext.SaveChanges() >= 0; var success = dbContext.SaveChanges() >= 0;
return (success, new {readTypeFullName}({newEntityVarName})); return (success, new {returnTypeFullName}({newEntityVarName}));
} }

View File

@ -1,18 +0,0 @@
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

@ -1,6 +0,0 @@
using Microsoft.EntityFrameworkCore;
namespace {Namespace}
{
{Code}
}

View File

@ -1,14 +1,13 @@
using Microsoft.EntityFrameworkCore; public static (bool, {returnTypeFullName}) Update{entityTypeName}(
this {contextFullName} dbContext,
{updateTypeFullName} {updateVarName},
{keyTypeFullName} {keyVarName})
{
var {existingEntityVarName} = dbContext.{propertyName}.FirstOrDefault(x => x.{keyPropertyName} == {keyVarName});
if ({existingEntityVarName} == null) return (false, null);
{existingEntityVarName}.Update({updateVarName});
dbContext.{propertyname}.Update({existingEntityVarName});
var result = dbContext.SaveChanges() >= 0;
return (result, {existingEntityVarName});
namespace {Namespace} }
{
public static partial class {entityTypeName}Extensions {
public static (bool, {readTypeFullName}) Update{entityTypeName}(
this {contextFullName} dbContext, {updateTypeFullName} {toUpdateVarName}) {
var {newEntityVarName} = new {entityTypeFullName}({toUpdateVarName});
dbContext.{propertyName}.Add({newEntityVarName});
var success = dbContext.SaveChanges() >= 0;
return (success, new {readTypeFullName}({newEntityVarName}));
}
}
}

@ -1 +1 @@
Subproject commit 12d5839f1660c0cf89be22d391b1f744a37c202e Subproject commit ca95c878399d057ee42007f04e4b3fb032deaab0