@ -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 < int , object > FieldIds { get ; set ; } = new Dictionary < int , object > ( ) ;
}
internal static class FieldMapRegistry
{
private static readonly ICollection < FieldMap > r egistry = new List < FieldMap > ( ) ;
private static readonly ICollection < FieldMap > fieldR egistry = new List < FieldMap > ( ) ;
private static readonly IDictionary < Type , string > tableRegistry = new Dictionary < Type , string > ( ) ;
static FieldMapRegistry ( )
{
// Map to QB field IDs. Obviously we will use actual IDs
AddFieldMap < Creditor > ( c = > c . Id , 1 ) ;
AddFieldMap < Creditor > ( c = > c . Created , 2 ) ;
AddFieldMap < Creditor > ( c = > c . Modified , 3 ) ;
AddFieldMap < Creditor > ( c = > c . Status , 10 ) ;
AddFieldMap < Creditor > ( c = > c . ClientFirstName , 20 ) ;
AddFieldMap < Creditor > ( c = > c . ClientLastName , 21 ) ;
AddFieldMap < Creditor > ( c = > c . CurrentCreditorProfileId , 90 ) ;
AddFieldMap < Creditor > ( c = > c . OriginalCreditorProfileId , 91 ) ;
AddTable < Creditor , CreditorsFieldMap > ( )
. WithProperty ( c = > c . Id , CreditorsFieldMap . RecordId )
. WithProperty ( c = > c . Modified , CreditorsFieldMap . DateModified ) ;
AddFieldMap < Entities . Client > ( c = > c . Id , 1 ) ;
AddFieldMap < Entities . Client > ( c = > c . Created , 2 ) ;
AddFieldMap < Entities . Client > ( c = > c . Modified , 3 ) ;
AddFieldMap < Entities . Client > ( c = > c . Email , 10 ) ;
AddFieldMap < Entities . Client > ( c = > c . Phone , 11 ) ;
AddFieldMap < Entities . Client > ( c = > c . Fir stName, 20 ) ;
AddFieldMap < Entities . Client > ( c = > c . LastName , 21 ) ;
AddFieldMap < Creditor > ( c = > c . Id , CreditorsFieldMap . RecordId ) ;
AddFieldMap < Creditor > ( c = > c . Created , CreditorsFieldMap . DateCreated ) ;
AddFieldMap < Creditor > ( c = > c . Modified , CreditorsFieldMap . DateModified ) ;
AddFieldMap < Creditor > ( c = > c . Status , CreditorsFieldMap . CreditorStatus ) ;
AddFieldMap < Creditor > ( c = > c . ClientFirstName , CreditorsFieldMap . ClientFirstName ) ;
AddFieldMap < Creditor > ( c = > c . ClientLa stName, CreditorsFieldMap . ClientLastName ) ;
AddFieldMap < Creditor > ( c = > c . CurrentCreditorProfileId , CreditorsFieldMap . RelatedCurrentCreditorPrimary ) ;
AddFieldMap < Creditor > ( c = > c . OriginalCreditorProfileId , CreditorsFieldMap . RelatedOriginalCreditor ) ;
AddFieldMap < Creditor > ( c = > c . AccountNumber , CreditorsFieldMap . AccountNum ) ;
//AddFieldMap<Entities.Client>(c => c.Id, 1);
//AddFieldMap<Entities.Client>(c => c.Created, 2);
//AddFieldMap<Entities.Client>(c => c.Modified, 3);
//AddFieldMap<Entities.Client>(c => c.Email, 10);
//AddFieldMap<Entities.Client>(c => c.Phone, 11);
//AddFieldMap<Entities.Client>(c => c.FirstName, 20);
//AddFieldMap<Entities.Client>(c => c.LastName, 21);
}
internal static void AddField Map< TEntity > ( Expression < Func < TEntity , object > > property , int fieldId )
public static Table Map< TEntity , TEnumFieldMap > AddTable < TEntity , TEnumFieldMap > ( )
{
tableRegistry . Add ( typeof ( TEntity ) , "asdas" ) ;
throw new NotImplementedException ( ) ;
}
public static TableMap < TEntity , TEnumFieldMap > WithProperty < TEntity , TEnumFieldMap > ( this TableMap < TEntity , TEnumFieldMap > table , Expression < Func < TEntity , object > > property , Enum field )
{
throw new NotImplementedException ( ) ;
}
internal static void AddFieldMap < TEntity > ( Expression < Func < TEntity , object > > property , Enum field )
{
if ( property . Body is MemberExpression memberExpression )
{
r egistry. Add ( new FieldMap
fieldR egistry. Add ( new FieldMap
{
DeclaringTypeName = memberExpression . Member . DeclaringType . FullName ,
PropertyName = memberExpression . Member . Name ,
FieldId = fieldId
FieldId = field. To < int > ( )
} ) ;
}
else if ( property . Body is UnaryExpression unaryExpression & & unaryExpression . Operand is MemberExpression unaryMemberExpression )
{
r egistry. Add ( new FieldMap
fieldR egistry. Add ( new FieldMap
{
DeclaringTypeName = unaryMemberExpression . Member . DeclaringType . FullName ,
PropertyName = unaryMemberExpression . Member . Name ,
FieldId = fieldId
// FieldId = fieldId,
FieldId = field . To < int > ( )
} ) ;
}
}
internal static int? GetFieldId ( PropertyInfo info )
{
var map = r egistry. FirstOrDefault ( m = > m . DeclaringTypeName = = info . DeclaringType . FullName & & m . PropertyName = = info . Name ) ;
var map = fieldR egistry. 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 < int , object > ( fieldId . Value , value ) ) ;
}
}
return context ;
}
}
}