more cleanup
This commit is contained in:
parent
9ce87d4ecb
commit
68f4c7f3db
@ -1,8 +1,10 @@
|
|||||||
namespace COA.EnterpriseServices.DataAccess.QuickBase
|
using System;
|
||||||
|
|
||||||
|
namespace COA.EnterpriseServices.DataAccess.QuickBase
|
||||||
{
|
{
|
||||||
internal class FieldMap
|
internal class FieldMap
|
||||||
{
|
{
|
||||||
internal string DeclaringTypeName { get; set; }
|
internal Type ItemType { get; set; }
|
||||||
internal string PropertyName { get; set; }
|
internal string PropertyName { get; set; }
|
||||||
internal int FieldId { get; set; }
|
internal int FieldId { get; set; }
|
||||||
}
|
}
|
||||||
|
@ -9,13 +9,6 @@ using System.Reflection;
|
|||||||
|
|
||||||
namespace COA.EnterpriseServices.DataAccess.QuickBase
|
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
|
internal static class FieldMapRegistry
|
||||||
{
|
{
|
||||||
private static readonly ICollection<FieldMap> fieldRegistry = new List<FieldMap>();
|
private static readonly ICollection<FieldMap> fieldRegistry = new List<FieldMap>();
|
||||||
@ -23,50 +16,90 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase
|
|||||||
|
|
||||||
static FieldMapRegistry()
|
static FieldMapRegistry()
|
||||||
{
|
{
|
||||||
// Map to QB field IDs. Obviously we will use actual IDs
|
|
||||||
|
|
||||||
AddTable<Creditor, CreditorsFieldMap>()
|
AddTable<Creditor, CreditorsFieldMap>()
|
||||||
.WithProperty(c => c.Id, CreditorsFieldMap.RecordId)
|
.WithDefaults()
|
||||||
.WithProperty(c => c.Modified, CreditorsFieldMap.DateModified);
|
.WithProperty(c => c.Status, CreditorsFieldMap.CreditorStatus)
|
||||||
|
.WithProperty(c => c.ClientFirstName, CreditorsFieldMap.ClientFirstName)
|
||||||
|
.WithProperty(c => c.ClientLastName, CreditorsFieldMap.ClientLastName)
|
||||||
|
.WithProperty(c => c.CurrentCreditorProfileId, CreditorsFieldMap.RelatedCurrentCreditorPrimary)
|
||||||
|
.WithProperty(c => c.OriginalCreditorProfileId, CreditorsFieldMap.RelatedOriginalCreditor)
|
||||||
|
.WithProperty(c => c.AccountNumber, CreditorsFieldMap.AccountNum);
|
||||||
|
|
||||||
AddFieldMap<Creditor>(c => c.Id, CreditorsFieldMap.RecordId);
|
AddTable<Entities.Client, ClientFieldMap>()
|
||||||
AddFieldMap<Creditor>(c => c.Created, CreditorsFieldMap.DateCreated);
|
.WithDefaults()
|
||||||
AddFieldMap<Creditor>(c => c.Modified, CreditorsFieldMap.DateModified);
|
.WithProperty(c => c.FirstName, ClientFieldMap.FirstName)
|
||||||
AddFieldMap<Creditor>(c => c.Status, CreditorsFieldMap.CreditorStatus);
|
.WithProperty(c => c.LastName, ClientFieldMap.LastName)
|
||||||
AddFieldMap<Creditor>(c => c.ClientFirstName, CreditorsFieldMap.ClientFirstName);
|
.WithProperty(c => c.Email, ClientFieldMap.ClientPrimaryEmail)
|
||||||
AddFieldMap<Creditor>(c => c.ClientLastName, CreditorsFieldMap.ClientLastName);
|
.WithProperty(c => c.Phone, ClientFieldMap.ClientPrimaryPhone);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TableMap<TEntity, TEnumFieldMap> AddTable<TEntity, TEnumFieldMap>()
|
/// <summary>
|
||||||
|
/// Adds a table mapping for a QuickBase field map
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <typeparam name="TEnumFieldMap"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static TableMap<TEntity, TEnumFieldMap> AddTable<TEntity, TEnumFieldMap>() where TEntity : IRecord
|
||||||
{
|
{
|
||||||
tableRegistry.Add(typeof(TEntity), "asdas");
|
var tableNameAttribute = typeof(TEnumFieldMap).GetCustomAttribute<QuickBaseNameAttribute>();
|
||||||
|
|
||||||
throw new NotImplementedException();
|
if (tableNameAttribute != null)
|
||||||
}
|
|
||||||
|
|
||||||
public static TableMap<TEntity, TEnumFieldMap> WithProperty<TEntity, TEnumFieldMap>(this TableMap<TEntity, TEnumFieldMap> table, Expression<Func<TEntity, object>> property, Enum field)
|
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
tableRegistry.Add(typeof(TEntity), tableNameAttribute.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void AddFieldMap<TEntity>(Expression<Func<TEntity, object>> property, Enum field)
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds default property mappings for common fields
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <typeparam name="TEnumFieldMap"></typeparam>
|
||||||
|
/// <param name="table"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
internal static TableMap<TEntity, TEnumFieldMap> WithDefaults<TEntity, TEnumFieldMap>(this TableMap<TEntity, TEnumFieldMap> table) where TEntity : IRecord where TEnumFieldMap : Enum
|
||||||
|
{
|
||||||
|
fieldRegistry.Add(new FieldMap
|
||||||
|
{
|
||||||
|
ItemType = typeof(TEntity),
|
||||||
|
PropertyName = nameof(IRecord.Id),
|
||||||
|
FieldId = PartnerApis.QuickBase.Constants.RecordIdFieldId
|
||||||
|
});
|
||||||
|
|
||||||
|
fieldRegistry.Add(new FieldMap
|
||||||
|
{
|
||||||
|
ItemType = typeof(TEntity),
|
||||||
|
PropertyName = nameof(IRecord.Created),
|
||||||
|
FieldId = PartnerApis.QuickBase.Constants.DateCreatedFieldId
|
||||||
|
});
|
||||||
|
|
||||||
|
fieldRegistry.Add(new FieldMap
|
||||||
|
{
|
||||||
|
ItemType = typeof(TEntity),
|
||||||
|
PropertyName = nameof(IRecord.Modified),
|
||||||
|
FieldId = PartnerApis.QuickBase.Constants.DateModifiedFieldId
|
||||||
|
});
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a propert mapping for an entity property
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <typeparam name="TEnumFieldMap"></typeparam>
|
||||||
|
/// <param name="table"></param>
|
||||||
|
/// <param name="property"></param>
|
||||||
|
/// <param name="field"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
internal static TableMap<TEntity, TEnumFieldMap> WithProperty<TEntity, TEnumFieldMap>(this TableMap<TEntity, TEnumFieldMap> table, Expression<Func<TEntity, object>> property, TEnumFieldMap field)
|
||||||
{
|
{
|
||||||
if (property.Body is MemberExpression memberExpression)
|
if (property.Body is MemberExpression memberExpression)
|
||||||
{
|
{
|
||||||
fieldRegistry.Add(new FieldMap
|
fieldRegistry.Add(new FieldMap
|
||||||
{
|
{
|
||||||
DeclaringTypeName = memberExpression.Member.DeclaringType.FullName,
|
ItemType = memberExpression.Member.DeclaringType,
|
||||||
PropertyName = memberExpression.Member.Name,
|
PropertyName = memberExpression.Member.Name,
|
||||||
FieldId = field.To<int>()
|
FieldId = field.To<int>()
|
||||||
});
|
});
|
||||||
@ -75,17 +108,18 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase
|
|||||||
{
|
{
|
||||||
fieldRegistry.Add(new FieldMap
|
fieldRegistry.Add(new FieldMap
|
||||||
{
|
{
|
||||||
DeclaringTypeName = unaryMemberExpression.Member.DeclaringType.FullName,
|
ItemType = unaryMemberExpression.Member.DeclaringType,
|
||||||
PropertyName = unaryMemberExpression.Member.Name,
|
PropertyName = unaryMemberExpression.Member.Name,
|
||||||
//FieldId = fieldId,
|
|
||||||
FieldId = field.To<int>()
|
FieldId = field.To<int>()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static int? GetFieldId(PropertyInfo info)
|
internal static int? GetFieldId(PropertyInfo info)
|
||||||
{
|
{
|
||||||
var map = fieldRegistry.FirstOrDefault(m => m.DeclaringTypeName == info.DeclaringType.FullName && m.PropertyName == info.Name);
|
var map = fieldRegistry.FirstOrDefault(m => m.ItemType == info.DeclaringType && m.PropertyName == info.Name);
|
||||||
|
|
||||||
return map != null ? map.FieldId : default;
|
return map != null ? map.FieldId : default;
|
||||||
}
|
}
|
||||||
@ -93,11 +127,14 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase
|
|||||||
internal static QuickBaseRecordContext GetTableContext(object item)
|
internal static QuickBaseRecordContext GetTableContext(object item)
|
||||||
{
|
{
|
||||||
var context = new QuickBaseRecordContext();
|
var context = new QuickBaseRecordContext();
|
||||||
var properties = item.GetType().GetProperties();
|
var itemType = item.GetType();
|
||||||
|
var properties = itemType.GetProperties();
|
||||||
|
|
||||||
|
context.Table = tableRegistry.FirstOrDefault(r => r.Key == itemType).Value;
|
||||||
|
|
||||||
foreach (var property in properties)
|
foreach (var property in properties)
|
||||||
{
|
{
|
||||||
var fieldId = FieldMapRegistry.GetFieldId(property);
|
var fieldId = GetFieldId(property);
|
||||||
var value = property.GetValue(item);
|
var value = property.GetValue(item);
|
||||||
|
|
||||||
if (fieldId != null)
|
if (fieldId != null)
|
||||||
|
@ -27,6 +27,8 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase
|
|||||||
|
|
||||||
public bool Update(T item)
|
public bool Update(T item)
|
||||||
{
|
{
|
||||||
|
var fieldData = FieldMapRegistry.GetTableContext(item);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,21 +37,5 @@ namespace COA.EnterpriseServices.DataAccess.QuickBase
|
|||||||
// we'll never get data from QB
|
// we'll never get data from QB
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<KeyValuePair<int, object>> GetFieldData(T item)
|
|
||||||
{
|
|
||||||
var properties = item.GetType().GetProperties();
|
|
||||||
|
|
||||||
foreach (var property in properties)
|
|
||||||
{
|
|
||||||
var fieldId = FieldMapRegistry.GetFieldId(property);
|
|
||||||
var value = property.GetValue(item);
|
|
||||||
|
|
||||||
if (fieldId != null)
|
|
||||||
{
|
|
||||||
yield return new KeyValuePair<int, object>(fieldId.Value, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace COA.EnterpriseServices.DataAccess.QuickBase
|
||||||
|
{
|
||||||
|
internal class QuickBaseRecordContext
|
||||||
|
{
|
||||||
|
internal string Table { get; set; }
|
||||||
|
internal IDictionary<int, object> FieldIds { get; set; } = new Dictionary<int, object>();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user