Compare commits

...

2 Commits

Author SHA1 Message Date
Loretta 74d679a5a9 improvements, fixes 2024-06-09 11:13:23 +02:00
Loretta bd565146e6 TrackingState, etc 2024-06-07 06:07:33 +02:00
3 changed files with 28 additions and 9 deletions

View File

@ -6,10 +6,13 @@ using System.Threading.Tasks;
namespace AyCode.Core.Enums
{
public enum DataChangeMode
public enum TrackingState : byte
{
Add = 1,
Update = 2,
Remove = 3,
None = 0,
Get = 1,
GetAll = 2,
Add = 3,
Update = 4,
Remove = 5,
}
}

View File

@ -6,8 +6,9 @@ namespace AyCode.Core.Extensions
{
public static class CollectionExtensions
{
public static DataChangeMode UpdateCollection<TDataItem>(this IList<TDataItem> source, TDataItem dataItem, bool isRemove) where TDataItem : IId<Guid>
public static TrackingState UpdateCollection<TDataItem>(this IList<TDataItem> source, TDataItem dataItem, bool isRemove) where TDataItem : IId<Guid>
{
if (source == null) throw new ArgumentNullException(nameof(source), $"source == null");
if (dataItem.Id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(dataItem), "UpdateCollection->dataItem.Id.IsNullOrEmpty()");
var transferIndex = source.FindIndex(x => x.Id == dataItem.Id);
@ -16,17 +17,17 @@ namespace AyCode.Core.Extensions
{
if (transferIndex > -1) source.RemoveAt(transferIndex);
return DataChangeMode.Remove;
return TrackingState.Remove;
}
if (transferIndex > -1)
{
source[transferIndex] = dataItem;
return DataChangeMode.Update;
return TrackingState.Update;
}
source.Add(dataItem);
return DataChangeMode.Add;
return TrackingState.Add;
}
public static int FindIndex<T>(this IList<T> list, Predicate<T> predicate)

View File

@ -1,4 +1,6 @@
namespace AyCode.Services.SignalRs;
using AyCode.Core.Enums;
namespace AyCode.Services.SignalRs;
public sealed class SignalRCrudTags(int getAllTag, int getItemTag, int addTag, int updateTag, int removeTag)
{
@ -8,4 +10,17 @@ public sealed class SignalRCrudTags(int getAllTag, int getItemTag, int addTag, i
public int UpdateMessageTag { get; } = updateTag;
public int RemoveMessageTag { get; } = removeTag;
public int GetMessageTagByTrackingState(TrackingState trackingState)
{
return trackingState switch
{
TrackingState.None => 0,
TrackingState.Get => GetItemMessageTag,
TrackingState.GetAll => GetAllMessageTag,
TrackingState.Add => AddMessageTag,
TrackingState.Update => UpdateMessageTag,
TrackingState.Remove => RemoveMessageTag,
_ => throw new ArgumentOutOfRangeException()
};
}
}