diff --git a/zero.Sqlite/Operations/DbOperations.Write.cs b/zero.Sqlite/Operations/DbOperations.Write.cs index 02fcfa9d..85be9cd3 100644 --- a/zero.Sqlite/Operations/DbOperations.Write.cs +++ b/zero.Sqlite/Operations/DbOperations.Write.cs @@ -18,6 +18,13 @@ public partial class DbOperations : IDbOperations /// public virtual Task> Update(T model, Func> validate = null) where T : ZeroIdEntity, new() => Save(model, validate, true); + /// + public virtual async Task> CreateOrUpdate(T model, Func> validate = null) where T : ZeroIdEntity, new() + { + bool update = !model.Id.IsNullOrEmpty() && await Any(x => x.Id == model.Id); + return await Save(model, validate, update); + } + /// protected virtual async Task> Save(T model, Func> validate = null, bool update = false) where T : ZeroIdEntity, new() { @@ -88,4 +95,24 @@ public partial class DbOperations : IDbOperations return Result.Success(model); } + + + /// + public virtual async Task Sort(IEnumerable ids) where T : ZeroEntity, new() + { + List items = await LoadAll(); + + uint sort = 0; + foreach (string id in ids) + { + T item = items.FirstOrDefault(x => x.Id == id); + if (item != null) + { + sort += 10; + item.Sort = sort; + item.LastModifiedDate = DateTimeOffset.Now; + } + } + await Db.UpdateAllAsync(items); + } } \ No newline at end of file diff --git a/zero.Sqlite/Operations/DbOperations.cs b/zero.Sqlite/Operations/DbOperations.cs index 7ac6cc37..086a78d5 100644 --- a/zero.Sqlite/Operations/DbOperations.cs +++ b/zero.Sqlite/Operations/DbOperations.cs @@ -186,6 +186,16 @@ public interface IDbOperations /// Task> Update(T model, Func> validate = null) where T : ZeroIdEntity, new(); + /// + /// Checks if an entity exists (via ID) and creates or updates it afterwards accordingly + /// + Task> CreateOrUpdate(T model, Func> validate = null) where T : ZeroIdEntity, new(); + + /// + /// Updates sorting of all items in a collection based on the given enumerable + /// + Task Sort(IEnumerable ids) where T : ZeroEntity, new(); + /// /// Deletes an entity /// diff --git a/zero/zero.csproj b/zero/zero.csproj index 2f9e77cd..a7ce3195 100644 --- a/zero/zero.csproj +++ b/zero/zero.csproj @@ -16,7 +16,7 @@ - + \ No newline at end of file