using COA.Common; using COA.PartnerApis.QuickBase; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; namespace COA.EnterpriseServices.DataAccess.QuickBase { /// /// Represents a map between a CLR type, its QuickBase table, and fields /// internal abstract class RecordMap { internal Type ItemType { get; set; } internal string Table { get; set; } internal ICollection FieldMaps { get; } = new List(); } internal class RecordMap : RecordMap where TEntity : IRecord where TEnumFieldMap : Enum { internal RecordMap() { ItemType = typeof(TEntity); var tableNameAttribute = typeof(TEnumFieldMap).GetCustomAttribute(); if (tableNameAttribute != null) { Table = tableNameAttribute.Name; } } /// /// Adds a propery mapping for an entity property /// /// /// /// internal RecordMap WithProperty(Expression> property, TEnumFieldMap field) { if (property.Body is MemberExpression memberExpression) { FieldMaps.Add(new FieldMap { PropertyName = memberExpression.Member.Name, FieldId = field.To() }); } else if (property.Body is UnaryExpression unaryExpression && unaryExpression.Operand is MemberExpression unaryMemberExpression) { FieldMaps.Add(new FieldMap { PropertyName = unaryMemberExpression.Member.Name, FieldId = field.To() }); } return this; } /// /// Adds default property mappings for common entity fields /// /// /// /// /// internal RecordMap WithDefaults() { FieldMaps.Add(new FieldMap { PropertyName = nameof(IRecord.Id), FieldId = PartnerApis.QuickBase.Constants.RecordIdFieldId }); FieldMaps.Add(new FieldMap { PropertyName = nameof(IRecord.Created), FieldId = PartnerApis.QuickBase.Constants.DateCreatedFieldId }); FieldMaps.Add(new FieldMap { PropertyName = nameof(IRecord.Modified), FieldId = PartnerApis.QuickBase.Constants.DateModifiedFieldId }); return this; } } }