namespace Berufsschule_HAM.Helpers;

public static class AttributesHelper
{
    public static void UpdateNonNullProperties(object source, object target)
    {
        if (source == null || target == null) return;

        var sourceProperties = source.GetType().GetProperties();
        var targetProperties = target.GetType().GetProperties();

        foreach (var sourceProperty in sourceProperties)
        {
            if (sourceProperty.CanRead)
            {
                var sourceValue = sourceProperty.GetValue(source);

                if (sourceValue == null) continue;

                var targetProperty = targetProperties.FirstOrDefault(p =>
                    p.Name == sourceProperty.Name &&
                    p.CanWrite &&
                    p.PropertyType == sourceProperty.PropertyType);

                if (targetProperty != null)
                {
                    targetProperty.SetValue(target, sourceValue);
                }
            }
        }
    }
}