using adas_core.Domain.Enums; using adas_core.Domain.Utils; namespace adas_core.Domain.Models; /// /// Represents an action that can be performed on a person resource. /// /// /// Typically used to model operations, commands, or permissions associated with a person entity within a resource-based system. /// public class PersonResourceAction { public ActionsEnum.ResourceAction? Action { get; set; } public PersonResource? Resource { get; set; } /// /// Applies the specified list of items and returns the resulting collection. /// This overload delegates to another Apply implementation, passing null as the initial state. /// /// The list of actions to be applied. /// A containing the result of applying the actions. public static List? Apply(List actions) { return Apply(null, actions); } /// /// Applies a list of add, update, and delete actions to a source list of person resources, returning the resulting list. /// Resources are added only if not already present, removed when marked for deletion, and replaced in place when updated (or added if not found). /// /// The source list of person resources to mutate. If null, an empty list is used. /// The list of resource actions (add, delete, update) to apply to the source. If null, no actions are applied. /// The resulting list of person resources after applying the actions, or null if the resulting list is empty. public static List? Apply(List? source, List? actions) { source ??= []; (actions ?? []).ForEach(s => { if (s.Resource != null) switch (s.Action) { case ActionsEnum.ResourceAction.Add: if (!source.Contains(s.Resource)) source.Add(s.Resource); break; case ActionsEnum.ResourceAction.Delete: if (source.Contains(s.Resource)) source.Remove(s.Resource); break; case ActionsEnum.ResourceAction.Update: if (source.Contains(s.Resource)) source[source.IndexOf(s.Resource)] = s.Resource; else source.Add(s.Resource); break; } }); return CollectionsUtils.NullIfEmpty(source); } }