namespace zero.Collections;
public abstract partial class CollectionOperations
{
///
public virtual async Task> Delete(string id) where T : ZeroIdEntity, new()
{
if (id.IsNullOrEmpty())
{
return EntityResult.Fail("@errors.ondelete.idnotfound");
}
T entity = await Session.LoadAsync(id);
if (entity == null)
{
return EntityResult.Fail("@errors.ondelete.idnotfound");
}
InterceptorInstruction instruction = Interceptors.ForDelete(entity);
if (!await instruction.Start())
{
return instruction.Result;
}
Session.Delete(entity);
await instruction.Complete();
await Session.SaveChangesAsync();
return EntityResult.Success();
}
///
public virtual async Task> Delete(T model) where T : ZeroIdEntity, new() => await Delete(model?.Id);
///
public virtual async Task Delete(IEnumerable models) where T : ZeroIdEntity, new() => await Delete(models.Select(x => x.Id));
///
public virtual async Task Delete(IEnumerable ids) where T : ZeroIdEntity, new()
{
int successCount = 0;
foreach (string id in ids)
{
EntityResult result = await Delete(id);
successCount += result.IsSuccess ? 1 : 0;
}
return successCount;
}
}