createorupdate + sort for db-ops in sqlite package

This commit is contained in:
2025-12-04 14:56:54 +01:00
parent 73abb7bf17
commit d4512f99d6
3 changed files with 38 additions and 1 deletions
@@ -18,6 +18,13 @@ public partial class DbOperations : IDbOperations
/// <inheritdoc />
public virtual Task<Result<T>> Update<T>(T model, Func<T, Task<ValidationResult>> validate = null) where T : ZeroIdEntity, new() => Save(model, validate, true);
/// <inheritdoc />
public virtual async Task<Result<T>> CreateOrUpdate<T>(T model, Func<T, Task<ValidationResult>> validate = null) where T : ZeroIdEntity, new()
{
bool update = !model.Id.IsNullOrEmpty() && await Any<T>(x => x.Id == model.Id);
return await Save(model, validate, update);
}
/// <inheritdoc />
protected virtual async Task<Result<T>> Save<T>(T model, Func<T, Task<ValidationResult>> validate = null, bool update = false) where T : ZeroIdEntity, new()
{
@@ -88,4 +95,24 @@ public partial class DbOperations : IDbOperations
return Result<T>.Success(model);
}
/// <inheritdoc />
public virtual async Task Sort<T>(IEnumerable<string> ids) where T : ZeroEntity, new()
{
List<T> items = await LoadAll<T>();
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);
}
}
+10
View File
@@ -186,6 +186,16 @@ public interface IDbOperations
/// </summary>
Task<Result<T>> Update<T>(T model, Func<T, Task<ValidationResult>> validate = null) where T : ZeroIdEntity, new();
/// <summary>
/// Checks if an entity exists (via ID) and creates or updates it afterwards accordingly
/// </summary>
Task<Result<T>> CreateOrUpdate<T>(T model, Func<T, Task<ValidationResult>> validate = null) where T : ZeroIdEntity, new();
/// <summary>
/// Updates sorting of all items in a collection based on the given enumerable
/// </summary>
Task Sort<T>(IEnumerable<string> ids) where T : ZeroEntity, new();
/// <summary>
/// Deletes an entity
/// </summary>
+1 -1
View File
@@ -16,7 +16,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="9.0.4" />
<PackageReference Include="FluentValidation" Version="11.11.0" />
<PackageReference Include="SixLabors.ImageSharp.Web" Version="3.1.5" />
<PackageReference Include="SixLabors.ImageSharp.Web" Version="3.2.0" />
</ItemGroup>
</Project>