added helpers to DAM, additional methods

This commit is contained in:
Ryan Peters 2020-10-15 08:08:02 -04:00
parent ec52a8233a
commit d990c53029
14 changed files with 246 additions and 51 deletions

View File

@ -23,5 +23,12 @@ namespace COA.EnterpriseServices.Creditor
{ {
return creditorHelper.SetCreditorStatus(creditorId, status); return creditorHelper.SetCreditorStatus(creditorId, status);
} }
public void AddOfferResponse()
{
// just a stub until we add actual DataObjects
creditorHelper.SetOriginalCreditorAsPrimary(1);
}
} }
} }

View File

@ -34,20 +34,6 @@ namespace COA.EnterpriseServices.DataAccess.EntityFramework
return true; return true;
} }
public bool Update(int id, Action<T> update)
{
using (var context = new QuickBaseContext())
{
var item = context.Set<T>().FirstOrDefault(e => e.Id == id);
update(item);
context.SaveChanges();
}
return true;
}
public ICollection<T> Get(Expression<Func<T, bool>> query) public ICollection<T> Get(Expression<Func<T, bool>> query)
{ {
using (var context = new QuickBaseContext()) using (var context = new QuickBaseContext())

View File

@ -0,0 +1,119 @@
// <auto-generated />
using System;
using COA.EnterpriseServices.DataAccess.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace COA.EnterpriseServices.DataAccess.EntityFramework.Migrations
{
[DbContext(typeof(QuickBaseContext))]
[Migration("20201015115918_add-creditor-profile-properties")]
partial class addcreditorprofileproperties
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.9")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("COA.EnterpriseServices.DataAccess.Entities.Client", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Address")
.HasColumnType("nvarchar(max)");
b.Property<string>("City")
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("Created")
.HasColumnType("datetime2");
b.Property<string>("Email")
.HasColumnType("nvarchar(max)");
b.Property<string>("FirstName")
.HasColumnType("nvarchar(max)");
b.Property<string>("LastName")
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("Modified")
.HasColumnType("datetime2");
b.Property<long>("Phone")
.HasColumnType("bigint");
b.Property<string>("State")
.HasColumnType("nvarchar(max)");
b.Property<string>("Zip")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Clients");
});
modelBuilder.Entity("COA.EnterpriseServices.DataAccess.Entities.Creditor", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("ClientFirstName")
.HasColumnType("nvarchar(max)");
b.Property<string>("ClientLastName")
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("Created")
.HasColumnType("datetime2");
b.Property<int>("CurrentCreditorProfileId")
.HasColumnType("int");
b.Property<DateTime>("Modified")
.HasColumnType("datetime2");
b.Property<int>("OriginalCreditorProfileId")
.HasColumnType("int");
b.Property<string>("Status")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Creditors");
});
modelBuilder.Entity("COA.EnterpriseServices.DataAccess.Entities.SettlementAttempt", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<DateTime>("Created")
.HasColumnType("datetime2");
b.Property<DateTime>("Modified")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("SettlementAttempts");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace COA.EnterpriseServices.DataAccess.EntityFramework.Migrations
{
public partial class addcreditorprofileproperties : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "CurrentCreditorProfileId",
table: "Creditors",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "OriginalCreditorProfileId",
table: "Creditors",
nullable: false,
defaultValue: 0);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CurrentCreditorProfileId",
table: "Creditors");
migrationBuilder.DropColumn(
name: "OriginalCreditorProfileId",
table: "Creditors");
}
}
}

View File

@ -77,9 +77,15 @@ namespace COA.EnterpriseServices.DataAccess.EntityFramework.Migrations
b.Property<DateTime>("Created") b.Property<DateTime>("Created")
.HasColumnType("datetime2"); .HasColumnType("datetime2");
b.Property<int>("CurrentCreditorProfileId")
.HasColumnType("int");
b.Property<DateTime>("Modified") b.Property<DateTime>("Modified")
.HasColumnType("datetime2"); .HasColumnType("datetime2");
b.Property<int>("OriginalCreditorProfileId")
.HasColumnType("int");
b.Property<string>("Status") b.Property<string>("Status")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");

View File

@ -21,6 +21,8 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase
AddFieldMap<Creditor>(c => c.Status, 10); AddFieldMap<Creditor>(c => c.Status, 10);
AddFieldMap<Creditor>(c => c.ClientFirstName, 20); AddFieldMap<Creditor>(c => c.ClientFirstName, 20);
AddFieldMap<Creditor>(c => c.ClientLastName, 21); AddFieldMap<Creditor>(c => c.ClientLastName, 21);
AddFieldMap<Creditor>(c => c.CurrentCreditorProfileId, 90);
AddFieldMap<Creditor>(c => c.OriginalCreditorProfileId, 91);
AddFieldMap<Client>(c => c.Id, 1); AddFieldMap<Client>(c => c.Id, 1);
AddFieldMap<Client>(c => c.Created, 2); AddFieldMap<Client>(c => c.Created, 2);

View File

@ -17,11 +17,8 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase
public bool Update(T item) public bool Update(T item)
{ {
return true; var fieldData = GetFieldData(item);
}
public bool Update(int id, Action<T> update)
{
return true; return true;
} }

View File

@ -1,9 +1,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
namespace COA.EnterpriseServices.DataAccess namespace COA.EnterpriseServices.DataAccess
{ {
/// <summary>
/// Provides a wrapper around <see cref="IDataAccess{T}"/> as well as additional convenience CRUD methods
/// </summary>
/// <typeparam name="T"></typeparam>
public class DataAccessManager<T> where T : class, IRecord public class DataAccessManager<T> where T : class, IRecord
{ {
private static readonly IDictionary<string, string[]> enabledDataAccess = new Dictionary<string, string[]> private static readonly IDictionary<string, string[]> enabledDataAccess = new Dictionary<string, string[]>
@ -29,7 +34,47 @@ namespace COA.EnterpriseServices.DataAccess
#endregion #endregion
}; };
public TResult Invoke<TResult>(Func<IDataAccess<T>, TResult> func) public bool Add(T item)
{
return Invoke(d => d.Add(item));
}
public bool Update(T item)
{
item.Modified = DateTime.Now;
return Invoke(d => d.Update(item));
}
public bool Update(int id, Action<T> update)
{
var item = Get(id);
update(item);
return Update(item);
}
public T Get(int id)
{
var item = Invoke(d => d.Get(i => i.Id == id));
if (item != null && item.Any())
{
return item.FirstOrDefault();
}
return default;
}
public ICollection<T> Get(Expression<Func<T, bool>> query)
{
return Invoke(d => d.Get(query));
}
#region Private Methods
private TResult Invoke<TResult>(Func<IDataAccess<T>, TResult> func)
{ {
var results = new List<TResult>(); var results = new List<TResult>();
var dataAccessInstances = GetEnabledDataAccess(); var dataAccessInstances = GetEnabledDataAccess();
@ -60,5 +105,7 @@ namespace COA.EnterpriseServices.DataAccess
.Select(i => i.Instance) .Select(i => i.Instance)
.ToList(); .ToList();
} }
#endregion
} }
} }

View File

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
namespace COA.EnterpriseServices.DataAccess.Entities namespace COA.EnterpriseServices.DataAccess.Entities
{ {
@ -12,5 +10,7 @@ namespace COA.EnterpriseServices.DataAccess.Entities
public string Status { get; set; } public string Status { get; set; }
public string ClientFirstName { get; set; } public string ClientFirstName { get; set; }
public string ClientLastName { get; set; } public string ClientLastName { get; set; }
public int CurrentCreditorProfileId { get; set; }
public int OriginalCreditorProfileId { get; set; }
} }
} }

View File

@ -1,13 +0,0 @@
using System.Linq;
namespace COA.EnterpriseServices.DataAccess.Helpers
{
public class BaseHelper<T> where T : class, IRecord
{
protected DataAccessManager<T> DataAccessManager { get; } = Dependencies.Container.GetInstance<DataAccessManager<T>>();
public virtual T Get(int id) => DataAccessManager.Invoke(d => d.Get(i => i.Id == id).FirstOrDefault());
public virtual bool Add(T item) => DataAccessManager.Invoke(d => d.Add(item));
}
}

View File

@ -3,11 +3,18 @@ using System.Collections.Generic;
namespace COA.EnterpriseServices.DataAccess.Helpers namespace COA.EnterpriseServices.DataAccess.Helpers
{ {
public class ClientHelper : BaseHelper<Client> public class ClientHelper
{ {
private readonly DataAccessManager<Client> clientDataAccess;
public ClientHelper(DataAccessManager<Client> clientDataAccess)
{
this.clientDataAccess = clientDataAccess;
}
public ICollection<Client> FindByEmail(string email) public ICollection<Client> FindByEmail(string email)
{ {
return DataAccessManager.Invoke(d => d.Get(c => c.Email.Equals(email))); return clientDataAccess.Get(c => c.Email.Equals(email));
} }
} }
} }

View File

@ -1,11 +1,8 @@
using COA.EnterpriseServices.DataAccess.Entities; using COA.EnterpriseServices.DataAccess.Entities;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace COA.EnterpriseServices.DataAccess.Helpers namespace COA.EnterpriseServices.DataAccess.Helpers
{ {
// NOTE: This example does NOT use BaseHelper<> if you wish to manage it yourself
// It allows for multiple DataAccessManagers to be used however needed by the helper
public class CreditorHelper public class CreditorHelper
{ {
private readonly DataAccessManager<Creditor> creditorDataAccess; private readonly DataAccessManager<Creditor> creditorDataAccess;
@ -19,32 +16,41 @@ namespace COA.EnterpriseServices.DataAccess.Helpers
public Creditor GetCreditor(int id) public Creditor GetCreditor(int id)
{ {
return creditorDataAccess.Invoke(d => d.Get(d => d.Id == id)?.FirstOrDefault()); return creditorDataAccess.Get(id);
} }
public SettlementAttempt GetSettlementAttempt(int id) public SettlementAttempt GetSettlementAttempt(int id)
{ {
return settlementAttemptDataAccess.Invoke(d => d.Get(d => d.Id == id)?.FirstOrDefault()); return settlementAttemptDataAccess.Get(id);
} }
public ICollection<Creditor> FindByName(string name) public ICollection<Creditor> FindByName(string name)
{ {
return creditorDataAccess.Invoke(d => d.Get(c => c.ClientFirstName.Contains(name) || c.ClientLastName.Contains(name))); return creditorDataAccess.Get(c => c.ClientFirstName.Contains(name) || c.ClientLastName.Contains(name));
} }
public ICollection<Creditor> FindByStatus(string status) public ICollection<Creditor> FindByStatus(string status)
{ {
return creditorDataAccess.Invoke(d => d.Get(c => c.Status == status)); return creditorDataAccess.Get(c => c.Status == status);
} }
public bool AddCreditor(Creditor creditor) public bool AddCreditor(Creditor creditor)
{ {
return creditorDataAccess.Invoke(d => d.Add(creditor)); return creditorDataAccess.Add(creditor);
} }
public bool SetCreditorStatus(int creditorId, string status) public bool SetCreditorStatus(int creditorId, string status)
{ {
return creditorDataAccess.Invoke(d => d.Update(creditorId, c => c.Status = status)); return creditorDataAccess.Update(creditorId, c => c.Status = status);
}
public void SetOriginalCreditorAsPrimary(int creditorId)
{
var creditor = creditorDataAccess.Get(creditorId);
creditor.CurrentCreditorProfileId = creditor.OriginalCreditorProfileId;
creditorDataAccess.Update(creditor);
} }
} }
} }

View File

@ -10,8 +10,6 @@ namespace COA.EnterpriseServices.DataAccess
bool Update(T item); bool Update(T item);
bool Update(int id, Action<T> update);
ICollection<T> Get(Expression<Func<T, bool>> query); ICollection<T> Get(Expression<Func<T, bool>> query);
} }
} }

View File

@ -11,10 +11,10 @@ namespace COA.EnterpriseServices.Sandbox
{ {
var creditorController = Dependencies.Container.GetInstance<CreditorController>(); var creditorController = Dependencies.Container.GetInstance<CreditorController>();
Console.WriteLine(creditorController.GetCreditorStatus(1)); //Console.WriteLine(creditorController.GetCreditorStatus(1));
//Console.WriteLine(creditorController.SetCreditorStatus(1, "Active"));
Console.WriteLine(creditorController.SetCreditorStatus(1, "Active"));
creditorController.AddOfferResponse();
Console.ReadLine(); Console.ReadLine();
} }