using COA.Common; using COA.EnterpriseServices.DataAccess.Entities; using COA.PartnerApis.QuickBase; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; 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 fieldRegistry = new List(); private static readonly IDictionary tableRegistry = new Dictionary(); static FieldMapRegistry() { // Map to QB field IDs. Obviously we will use actual IDs AddTable() .WithProperty(c => c.Id, CreditorsFieldMap.RecordId) .WithProperty(c => c.Modified, CreditorsFieldMap.DateModified); 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); } 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) { fieldRegistry.Add(new FieldMap { DeclaringTypeName = memberExpression.Member.DeclaringType.FullName, PropertyName = memberExpression.Member.Name, FieldId = field.To() }); } else if (property.Body is UnaryExpression unaryExpression && unaryExpression.Operand is MemberExpression unaryMemberExpression) { fieldRegistry.Add(new FieldMap { DeclaringTypeName = unaryMemberExpression.Member.DeclaringType.FullName, PropertyName = unaryMemberExpression.Member.Name, //FieldId = fieldId, FieldId = field.To() }); } } internal static int? GetFieldId(PropertyInfo info) { 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; } } }