|
@@ -1,4 +1,5 @@
|
|
|
-using System;
|
|
|
+using InABox.Clients;
|
|
|
+using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
@@ -296,7 +297,9 @@ namespace InABox.Core
|
|
|
/// Get a list of columns that need to be loaded in a lookup for property <paramref name="column"/> on <paramref name="TEntity"/>.
|
|
|
/// </summary>
|
|
|
/// <remarks>
|
|
|
- /// <paramref name="column"/> can either be the name of an entity link, or the ID property of that entity link.
|
|
|
+ /// <paramref name="column"/> can either be the name of an entity link, or the ID property of that entity link.<br/>
|
|
|
+ /// Gives an aggregate of required columns on the entity link in question, the linked properties of <paramref name="TEntity"/>, and any defined on
|
|
|
+ /// a <see cref="LookupDefinitionAttribute"/> or <see cref="ILookupDefinition"/>.
|
|
|
/// </remarks>
|
|
|
/// <param name="TEntity"></param>
|
|
|
/// <param name="TLookup"></param>
|
|
@@ -354,6 +357,9 @@ namespace InABox.Core
|
|
|
/// <summary>
|
|
|
/// Get a list of columns that need to be loaded for an entity of type <paramref name="T"/>.
|
|
|
/// </summary>
|
|
|
+ /// <remarks>
|
|
|
+ /// Gives an aggregate of required columns, and columns found on <see cref="LookupDefinitionAttribute"/> and <see cref="ILookupDefinition"/>.
|
|
|
+ /// </remarks>
|
|
|
/// <param name="T"></param>
|
|
|
/// <returns></returns>
|
|
|
public static IColumns RequiredColumns(Type T)
|
|
@@ -475,6 +481,60 @@ namespace InABox.Core
|
|
|
where TLookup : BaseObject
|
|
|
=> OnCreateItem(typeof(TEntity), CoreUtils.GetFullPropertyName(column, "."), items, item);
|
|
|
|
|
|
+ public static void DoLookup<TEntity, TLookup, TLookupLink>(TEntity entity, Expression<Func<TEntity, TLookupLink>> column, Guid lookupID)
|
|
|
+ where TEntity : class
|
|
|
+ where TLookup : Entity, IRemotable, IPersistent, new()
|
|
|
+ where TLookupLink : IEntityLink<TLookup>
|
|
|
+ {
|
|
|
+ if(lookupID == Guid.Empty)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ var columns = DefineLookupColumns<TEntity, TLookup, TLookupLink>(column);
|
|
|
+ var row = Client.Query(new Filter<TLookup>(x => x.ID).IsEqualTo(lookupID), columns).Rows.FirstOrDefault();
|
|
|
+ if(row is null)
|
|
|
+ {
|
|
|
+ Logger.Send(LogType.Error, ClientFactory.UserID, $"Error in DoLookup: {typeof(TLookup).Name} with ID {lookupID} not found.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach(var subColumn in columns)
|
|
|
+ {
|
|
|
+ CoreUtils.SetPropertyValue(entity, CoreUtils.GetFullPropertyName(column, ".") + "." + subColumn.Property, row[subColumn.Property]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void DoLookups<TEntity, TLookup, TLookupLink>(IEnumerable<Tuple<TEntity, Guid>> lookups, Expression<Func<TEntity, TLookupLink>> column)
|
|
|
+ where TEntity : class
|
|
|
+ where TLookup : Entity, IRemotable, IPersistent, new()
|
|
|
+ where TLookupLink : IEntityLink<TLookup>
|
|
|
+ {
|
|
|
+ lookups = lookups.AsIList();
|
|
|
+ var lookupIDs = lookups.Select(x => x.Item2).Where(x => x != Guid.Empty).ToArray();
|
|
|
+
|
|
|
+ var columns = DefineLookupColumns<TEntity, TLookup, TLookupLink>(column).Add(x => x.ID);
|
|
|
+ var data = Client.Query(new Filter<TLookup>(x => x.ID).InList(lookupIDs), columns).Rows.ToDictionary(x => x.Get<TLookup, Guid>(x => x.ID));
|
|
|
+
|
|
|
+ foreach(var (entity, lookupID) in lookups)
|
|
|
+ {
|
|
|
+ if(lookupID == Guid.Empty)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!data.TryGetValue(lookupID, out var row))
|
|
|
+ {
|
|
|
+ Logger.Send(LogType.Error, ClientFactory.UserID, $"Error in DoLookup: {typeof(TLookup).Name} with ID {lookupID} not found.");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach(var subColumn in columns)
|
|
|
+ {
|
|
|
+ CoreUtils.SetPropertyValue(entity, CoreUtils.GetFullPropertyName(column, ".") + "." + subColumn.Property, row[subColumn.Property]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
#endregion
|
|
|
}
|
|
|
|