From 9ce87d4ecb6539df3dcb4a32b4e10199136540e5 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Thu, 15 Oct 2020 13:47:44 -0400 Subject: [PATCH] progress. in the middle of reworking QB api binding to use fluent api --- .../COA.EnterpriseServices.Creditors.csproj | 1 + ...rpriseServices.DataAccess.QuickBase.csproj | 1 + .../FieldMap.cs | 5 + .../FieldMapRegistry.cs | 93 ++++++++++++++----- .../QuickBaseDataAccess.cs | 16 +++- .../COA.EnterpriseServices.DataAccess.csproj | 1 + COA.EnterpriseServices.DataAccess/Mapping.cs | 9 +- COA.EnterpriseServices.Sandbox/Program.cs | 4 +- 8 files changed, 96 insertions(+), 34 deletions(-) diff --git a/COA.EnterpriseServices.Creditors/COA.EnterpriseServices.Creditors.csproj b/COA.EnterpriseServices.Creditors/COA.EnterpriseServices.Creditors.csproj index 286947f..ae96d01 100644 --- a/COA.EnterpriseServices.Creditors/COA.EnterpriseServices.Creditors.csproj +++ b/COA.EnterpriseServices.Creditors/COA.EnterpriseServices.Creditors.csproj @@ -6,6 +6,7 @@ + diff --git a/COA.EnterpriseServices.DataAccess.QuickBase/COA.EnterpriseServices.DataAccess.QuickBase.csproj b/COA.EnterpriseServices.DataAccess.QuickBase/COA.EnterpriseServices.DataAccess.QuickBase.csproj index 1072e82..4648fa0 100644 --- a/COA.EnterpriseServices.DataAccess.QuickBase/COA.EnterpriseServices.DataAccess.QuickBase.csproj +++ b/COA.EnterpriseServices.DataAccess.QuickBase/COA.EnterpriseServices.DataAccess.QuickBase.csproj @@ -5,6 +5,7 @@ + diff --git a/COA.EnterpriseServices.DataAccess.QuickBase/FieldMap.cs b/COA.EnterpriseServices.DataAccess.QuickBase/FieldMap.cs index 7f2b231..7a08141 100644 --- a/COA.EnterpriseServices.DataAccess.QuickBase/FieldMap.cs +++ b/COA.EnterpriseServices.DataAccess.QuickBase/FieldMap.cs @@ -6,4 +6,9 @@ internal string PropertyName { get; set; } internal int FieldId { get; set; } } + + internal class TableMap + { + + } } diff --git a/COA.EnterpriseServices.DataAccess.QuickBase/FieldMapRegistry.cs b/COA.EnterpriseServices.DataAccess.QuickBase/FieldMapRegistry.cs index 0d6bd96..7a6e97c 100644 --- a/COA.EnterpriseServices.DataAccess.QuickBase/FieldMapRegistry.cs +++ b/COA.EnterpriseServices.DataAccess.QuickBase/FieldMapRegistry.cs @@ -1,4 +1,6 @@ -using COA.EnterpriseServices.DataAccess.Entities; +using COA.Common; +using COA.EnterpriseServices.DataAccess.Entities; +using COA.PartnerApis.QuickBase; using System; using System.Collections.Generic; using System.Linq; @@ -7,59 +9,104 @@ using System.Reflection; namespace COA.EnterpriseServices.DataAccess.QuickBase { + internal class QuickBaseRecordContext + { + internal string Table { get; set; } + internal IDictionary FieldIds { get; set; } = new Dictionary(); + + } + internal static class FieldMapRegistry { - private static readonly ICollection registry = new List(); + private static readonly ICollection fieldRegistry = new List(); + private static readonly IDictionary tableRegistry = new Dictionary(); static FieldMapRegistry() { // Map to QB field IDs. Obviously we will use actual IDs - AddFieldMap(c => c.Id, 1); - AddFieldMap(c => c.Created, 2); - AddFieldMap(c => c.Modified, 3); - AddFieldMap(c => c.Status, 10); - AddFieldMap(c => c.ClientFirstName, 20); - AddFieldMap(c => c.ClientLastName, 21); - AddFieldMap(c => c.CurrentCreditorProfileId, 90); - AddFieldMap(c => c.OriginalCreditorProfileId, 91); + AddTable() + .WithProperty(c => c.Id, CreditorsFieldMap.RecordId) + .WithProperty(c => c.Modified, CreditorsFieldMap.DateModified); - AddFieldMap(c => c.Id, 1); - AddFieldMap(c => c.Created, 2); - AddFieldMap(c => c.Modified, 3); - AddFieldMap(c => c.Email, 10); - AddFieldMap(c => c.Phone, 11); - AddFieldMap(c => c.FirstName, 20); - AddFieldMap(c => c.LastName, 21); + AddFieldMap(c => c.Id, CreditorsFieldMap.RecordId); + AddFieldMap(c => c.Created, CreditorsFieldMap.DateCreated); + AddFieldMap(c => c.Modified, CreditorsFieldMap.DateModified); + AddFieldMap(c => c.Status, CreditorsFieldMap.CreditorStatus); + AddFieldMap(c => c.ClientFirstName, CreditorsFieldMap.ClientFirstName); + AddFieldMap(c => c.ClientLastName, CreditorsFieldMap.ClientLastName); + AddFieldMap(c => c.CurrentCreditorProfileId, CreditorsFieldMap.RelatedCurrentCreditorPrimary); + AddFieldMap(c => c.OriginalCreditorProfileId, CreditorsFieldMap.RelatedOriginalCreditor); + AddFieldMap(c => c.AccountNumber, CreditorsFieldMap.AccountNum); + + //AddFieldMap(c => c.Id, 1); + //AddFieldMap(c => c.Created, 2); + //AddFieldMap(c => c.Modified, 3); + //AddFieldMap(c => c.Email, 10); + //AddFieldMap(c => c.Phone, 11); + //AddFieldMap(c => c.FirstName, 20); + //AddFieldMap(c => c.LastName, 21); } - internal static void AddFieldMap(Expression> property, int fieldId) + public static TableMap AddTable() + { + tableRegistry.Add(typeof(TEntity), "asdas"); + + throw new NotImplementedException(); + } + + public static TableMap WithProperty(this TableMap table, Expression> property, Enum field) + { + throw new NotImplementedException(); + } + + internal static void AddFieldMap(Expression> property, Enum field) { if (property.Body is MemberExpression memberExpression) { - registry.Add(new FieldMap + fieldRegistry.Add(new FieldMap { DeclaringTypeName = memberExpression.Member.DeclaringType.FullName, PropertyName = memberExpression.Member.Name, - FieldId = fieldId + FieldId = field.To() }); } else if (property.Body is UnaryExpression unaryExpression && unaryExpression.Operand is MemberExpression unaryMemberExpression) { - registry.Add(new FieldMap + fieldRegistry.Add(new FieldMap { DeclaringTypeName = unaryMemberExpression.Member.DeclaringType.FullName, PropertyName = unaryMemberExpression.Member.Name, - FieldId = fieldId + //FieldId = fieldId, + FieldId = field.To() }); } } internal static int? GetFieldId(PropertyInfo info) { - var map = registry.FirstOrDefault(m => m.DeclaringTypeName == info.DeclaringType.FullName && m.PropertyName == info.Name); + var map = fieldRegistry.FirstOrDefault(m => m.DeclaringTypeName == info.DeclaringType.FullName && m.PropertyName == info.Name); return map != null ? map.FieldId : default; } + + internal static QuickBaseRecordContext GetTableContext(object item) + { + var context = new QuickBaseRecordContext(); + var properties = item.GetType().GetProperties(); + + foreach (var property in properties) + { + var fieldId = FieldMapRegistry.GetFieldId(property); + var value = property.GetValue(item); + + if (fieldId != null) + { + context.FieldIds.Add(new KeyValuePair(fieldId.Value, value)); + } + } + + return context; + } } } diff --git a/COA.EnterpriseServices.DataAccess.QuickBase/QuickBaseDataAccess.cs b/COA.EnterpriseServices.DataAccess.QuickBase/QuickBaseDataAccess.cs index 0c8bc7e..5761858 100644 --- a/COA.EnterpriseServices.DataAccess.QuickBase/QuickBaseDataAccess.cs +++ b/COA.EnterpriseServices.DataAccess.QuickBase/QuickBaseDataAccess.cs @@ -1,4 +1,5 @@ -using System; +using COA.PartnerApis.QuickBase; +using System; using System.Collections.Generic; using System.Linq.Expressions; @@ -6,9 +7,18 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase { public class QuickBaseDataAccess : IDataAccess where T : class, IRecord, new() { + private readonly QuickBaseApiClient client; + + public QuickBaseDataAccess(QuickBaseApiClient client) + { + this.client = client; + } + public bool Add(T item) { - var fieldData = GetFieldData(item); + var fieldData = FieldMapRegistry.GetTableContext(item); + + //client.AddRecord() // use fieldData to create mapped data to push to quickbase API @@ -17,8 +27,6 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase public bool Update(T item) { - var fieldData = GetFieldData(item); - return true; } diff --git a/COA.EnterpriseServices.DataAccess/COA.EnterpriseServices.DataAccess.csproj b/COA.EnterpriseServices.DataAccess/COA.EnterpriseServices.DataAccess.csproj index 77436bb..1036480 100644 --- a/COA.EnterpriseServices.DataAccess/COA.EnterpriseServices.DataAccess.csproj +++ b/COA.EnterpriseServices.DataAccess/COA.EnterpriseServices.DataAccess.csproj @@ -6,6 +6,7 @@ + diff --git a/COA.EnterpriseServices.DataAccess/Mapping.cs b/COA.EnterpriseServices.DataAccess/Mapping.cs index e6a35ae..40c6062 100644 --- a/COA.EnterpriseServices.DataAccess/Mapping.cs +++ b/COA.EnterpriseServices.DataAccess/Mapping.cs @@ -7,12 +7,11 @@ namespace COA.EnterpriseServices.DataAccess { public static IMapper GetMapper() { - var cfg = new MapperConfigurationExpression(); - cfg.AddProfile(); + var configuration = new MapperConfigurationExpression(); + + configuration.AddProfile(); - //Mapper.Initialize(cfg); - // or - var mapperConfig = new MapperConfiguration(cfg); + var mapperConfig = new MapperConfiguration(configuration); return new Mapper(mapperConfig); } diff --git a/COA.EnterpriseServices.Sandbox/Program.cs b/COA.EnterpriseServices.Sandbox/Program.cs index d48cc8c..6ecf421 100644 --- a/COA.EnterpriseServices.Sandbox/Program.cs +++ b/COA.EnterpriseServices.Sandbox/Program.cs @@ -10,8 +10,8 @@ namespace COA.EnterpriseServices.Sandbox { var creditorController = Dependencies.Container.GetInstance(); - Console.WriteLine(creditorController.GetCreditorStatus(1)); - //Console.WriteLine(creditorController.SetCreditorStatus(1, "Active")); + //Console.WriteLine(creditorController.GetCreditorStatus(1)); + Console.WriteLine(creditorController.SetCreditorStatus(1, "Active")); //creditorController.AddOfferResponse(new OfferResponse //{