using adas_core.Domain.Enums; using adas_core.Domain.Utils; namespace adas_core.Domain.Models; /// /// Represents an action related to the location of a patient. /// /// /// This type encapsulates an operation or behavior associated with managing or interacting with a patient's location information. /// public class PatientLocationAction { public ActionsEnum.ResourceAction? Action { get; set; } public PatientLocation? Location { get; set; } /// /// Applies a collection of entries to produce the resulting set of records. /// /// The list of patient location actions to apply. May be null. /// The resulting produced by applying the actions, or null if no locations are produced. public static List? Apply(List? actions) { return Apply(null, actions); } /// /// Applies a list of entries to a source list of objects, supporting add, delete, and update operations, and returns the resulting list or null when empty. /// /// The list of patient locations to mutate. If null, an empty list is used. /// The actions to apply to the source list. If null, no actions are applied; actions with a null Location are skipped. /// The resulting list of after applying the actions, or null if the list is empty. public static List? Apply(List? source, List? actions) { source ??= []; (actions ?? []).ForEach(s => { if (s.Location != null) switch (s.Action) { case ActionsEnum.ResourceAction.Add: if (!source.Contains(s.Location)) source.Add(s.Location); break; case ActionsEnum.ResourceAction.Delete: if (source.Contains(s.Location)) source.Remove(s.Location); break; case ActionsEnum.ResourceAction.Update: if (source.Contains(s.Location)) source[source.IndexOf(s.Location)] = s.Location; else source.Add(s.Location); break; } }); return CollectionsUtils.NullIfEmpty(source); } }