freetubesync/FreeTubeSync/Database/MapData.cs
Mario Steele 4985dc4179 Started the Split
Split Json data models coming from REST Api, from the Database models
storing them in a SQLite database.
Work to re-engineer endpoints to use Database objects, and copy/update
data from the json objects.
More work is needed.
2025-07-22 17:03:33 -05:00

36 lines
No EOL
1,002 B
C#

using System.Collections;
namespace FreeTubeSync.Database;
public static class MapData
{
public static void MapFrom(this object obj1, object obj2)
{
var props = new Dictionary<string, object?>();
var t1Type = obj1.GetType();
var t2Type = obj2.GetType();
var properties = t1Type.GetProperties();
foreach (var property in properties)
{
props[property.Name] = property.GetValue(obj1);
}
properties = t2Type.GetProperties();
foreach (var property in properties)
{
if (props[property.Name] is not IEnumerable)
property.SetValue(obj2, props[property.Name]);
}
}
public static void MapTo<T1, T2>(this IEnumerable<T1> obj1, IList<T2> obj2)
where T1 : class where T2 : class, new()
{
foreach (var obj in obj1)
{
var newObj = new T2();
newObj.MapFrom(obj);
obj2.Add(newObj);
}
}
}