2021-11-23 11:35:01 +01:00
|
|
|
using FluentValidation.Results;
|
|
|
|
|
|
2021-11-23 22:56:22 +01:00
|
|
|
namespace zero.Stores;
|
2021-11-23 11:35:01 +01:00
|
|
|
|
2021-11-27 18:09:27 +01:00
|
|
|
public partial class StoreOperations : IStoreOperations
|
2021-11-23 11:35:01 +01:00
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual Task<Result<T>> Create<T>(T model, Func<T, Task<ValidationResult>> validate = null) where T : ZeroIdEntity, new() => Save(model, validate);
|
2021-11-23 15:43:21 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual Task<Result<T>> Update<T>(T model, Func<T, Task<ValidationResult>> validate = null) where T : ZeroIdEntity, new() => Save(model, validate);
|
2021-11-23 15:43:21 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
protected virtual async Task<Result<T>> Save<T>(T model, Func<T, Task<ValidationResult>> validate = null) where T : ZeroIdEntity, new()
|
2021-11-23 11:35:01 +01:00
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
2021-11-26 15:47:11 +01:00
|
|
|
return Result<T>.Fail("@errors.onsave.empty");
|
2021-11-23 11:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-02 14:44:48 +01:00
|
|
|
bool isUpdate = false;
|
|
|
|
|
|
|
|
|
|
// check if the Id for a model already exists
|
|
|
|
|
if (!model.Id.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
bool exists = await Session.Advanced.ExistsAsync(model.Id);
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
|
{
|
|
|
|
|
return Result<T>.Fail("@errors.onsave.noidmatch");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isUpdate = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate flavor
|
|
|
|
|
if (model is ISupportsFlavors flavorModel && !flavorModel.Flavor.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
if (!Flavors.Exists<T>(flavorModel.Flavor))
|
|
|
|
|
{
|
|
|
|
|
return Result<T>.Fail("@errors.onsave.flavornotfound");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-23 11:35:01 +01:00
|
|
|
|
|
|
|
|
// prepare model
|
|
|
|
|
PrepareForSave(model);
|
|
|
|
|
|
|
|
|
|
// run validator
|
|
|
|
|
if (validate != null)
|
|
|
|
|
{
|
|
|
|
|
ValidationResult validation = await validate(model);
|
|
|
|
|
if (!validation.IsValid)
|
|
|
|
|
{
|
2021-11-26 15:47:11 +01:00
|
|
|
return Result<T>.Fail(validation);
|
2021-11-23 11:35:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create ID before-hand so interceptors can use it
|
|
|
|
|
if (!isUpdate)
|
|
|
|
|
{
|
|
|
|
|
model.Id = await GenerateId(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// run interceptor
|
|
|
|
|
InterceptorInstruction<T> instruction = isUpdate ? Interceptors.ForUpdate(model) : Interceptors.ForCreate(model);
|
|
|
|
|
|
2021-12-28 13:23:14 +01:00
|
|
|
if (!await instruction.Start(this))
|
2021-11-23 11:35:01 +01:00
|
|
|
{
|
|
|
|
|
return instruction.Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// store our model
|
|
|
|
|
await Session.StoreAsync(model);
|
|
|
|
|
await instruction.Complete();
|
|
|
|
|
await Session.SaveChangesAsync();
|
|
|
|
|
|
2021-11-26 15:47:11 +01:00
|
|
|
return Result<T>.Success(model);
|
2021-11-23 11:35:01 +01:00
|
|
|
}
|
2021-12-29 15:29:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task<Result<IOrderedEnumerable<T>>> Sort<T>(string[] sortedIds) where T : ZeroIdEntity, ISupportsSorting, new()
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, T> items = await Load<T>(sortedIds);
|
2022-01-04 17:56:34 +01:00
|
|
|
uint index = 10;
|
2021-12-29 15:29:33 +01:00
|
|
|
|
|
|
|
|
// contains multiple parents, therefore fail
|
|
|
|
|
if (typeof(ISupportsTrees).IsAssignableFrom(typeof(T)) && items.Select(x => (x.Value as ISupportsTrees)?.ParentId).Distinct().Count() > 1)
|
|
|
|
|
{
|
|
|
|
|
return Result<IOrderedEnumerable<T>>.Fail("@errors.treeentity.sortingmultipleparents");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
|
|
|
|
item.Value.Sort = index;
|
|
|
|
|
index += 10;
|
|
|
|
|
await Update(item.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result<IOrderedEnumerable<T>>.Success(items.Select(x => x.Value).OrderByDescending(x => x.Sort));
|
|
|
|
|
}
|
2021-11-23 11:35:01 +01:00
|
|
|
}
|