using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using ServiceStack.OrmLite;
using zero.Models;
namespace zero.Sqlite;
public partial class DbOperations : IDbOperations
{
///
public virtual Task> Delete(T model) where T : ZeroIdEntity, new()
=> Delete(model.Id);
///
public virtual async Task> Delete(string id) where T : ZeroIdEntity, new()
{
T model = await Load(id);
if (model == null)
{
Logger.LogWarning("Could not delete entity (model is null) for type {type}", typeof(T));
return Result.Fail("@errors.ondelete.idnotfound");
}
if (model is ISupportsSoftDelete softDeleteModel)
{
softDeleteModel.IsDeleted = true;
}
else
{
await Db.DeleteByIdAsync(model.Id);
}
Logger.LogInformation("{id} ({type}) successfully deleted", typeof(T), model.Id);
return Result.Success();
}
}