using System; using System.Collections.Generic; using API.Entities; namespace API.Helpers; #nullable enable public static class OrderableHelper { public static void ReorderItems(List items, int itemId, int toPosition) { if (toPosition < 0) throw new ArgumentException("toPosition cannot be less than 0"); var item = items.Find(r => r.Id == itemId); if (item != null) { items.Remove(item); items.Insert(toPosition, item); } for (var i = 0; i < items.Count; i++) { items[i].Order = i; } } public static void ReorderItems(List items, int itemId, int toPosition) { if (toPosition < 0) throw new ArgumentException("toPosition cannot be less than 0"); var item = items.Find(r => r.Id == itemId); if (item != null && toPosition < items.Count) { items.Remove(item); items.Insert(toPosition, item); } for (var i = 0; i < items.Count; i++) { items[i].Order = i; } } public static void ReorderItems(IList items) { for (var i = 0; i < items.Count; i++) { items[i].Order = i; } } public static void ReorderItems(List items, int readingListItemId, int toPosition) { if (toPosition < 0) throw new ArgumentException("toPosition cannot be less than 0"); var item = items.Find(r => r.Id == readingListItemId); if (item != null) { items.Remove(item); // Ensure toPosition is within the new list bounds toPosition = Math.Min(toPosition, items.Count); items.Insert(toPosition, item); } for (var i = 0; i < items.Count; i++) { items[i].Order = i; } } }