first commit
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using System.Linq.Expressions;
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Common;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.Repositories;
|
||||
|
||||
/// <summary>通用仓储实现(基于 FreeSql)</summary>
|
||||
public class Repository<T> : IRepository<T> where T : class, IEntity
|
||||
{
|
||||
protected readonly IFreeSql _fsql;
|
||||
|
||||
public Repository(IFreeSql fsql)
|
||||
{
|
||||
_fsql = fsql;
|
||||
}
|
||||
|
||||
public virtual async Task<T> GetByIdAsync(long id)
|
||||
=> await _fsql.Select<T>().Where(e => e.Id == id).FirstAsync();
|
||||
|
||||
public virtual async Task<List<T>> GetAllAsync()
|
||||
=> await _fsql.Select<T>().ToListAsync();
|
||||
|
||||
public virtual async Task<List<T>> FindAsync(Expression<Func<T, bool>> predicate)
|
||||
=> await _fsql.Select<T>().Where(predicate).ToListAsync();
|
||||
|
||||
public virtual async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate)
|
||||
=> await _fsql.Select<T>().Where(predicate).FirstAsync();
|
||||
|
||||
public virtual async Task<bool> AnyAsync(Expression<Func<T, bool>> predicate)
|
||||
=> await _fsql.Select<T>().Where(predicate).AnyAsync();
|
||||
|
||||
public virtual async Task<long> CountAsync(Expression<Func<T, bool>> predicate)
|
||||
=> await _fsql.Select<T>().Where(predicate).CountAsync();
|
||||
|
||||
public virtual async Task<T> AddAsync(T entity)
|
||||
{
|
||||
entity.CreatedAt = DateTime.Now;
|
||||
await _fsql.Insert(entity).ExecuteAffrowsAsync();
|
||||
// FreeSql 在 SQLite 下不回填自增主键,手动用 last_insert_rowid 获取并回填
|
||||
if (entity.Id == 0)
|
||||
{
|
||||
entity.Id = Convert.ToInt64(await _fsql.Ado.ExecuteScalarAsync("SELECT last_insert_rowid();"));
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task AddRangeAsync(IEnumerable<T> entities)
|
||||
{
|
||||
foreach (var e in entities) e.CreatedAt = DateTime.Now;
|
||||
await _fsql.Insert(entities).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public virtual async Task UpdateAsync(T entity)
|
||||
{
|
||||
entity.UpdatedAt = DateTime.Now;
|
||||
await _fsql.Update<T>().SetSource(entity).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public virtual async Task DeleteAsync(T entity)
|
||||
{
|
||||
entity.IsDeleted = true;
|
||||
entity.UpdatedAt = DateTime.Now;
|
||||
await _fsql.Update<T>().SetSource(entity).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public virtual async Task DeleteRangeAsync(IEnumerable<T> entities)
|
||||
{
|
||||
foreach (var e in entities)
|
||||
{
|
||||
e.IsDeleted = true;
|
||||
e.UpdatedAt = DateTime.Now;
|
||||
}
|
||||
await _fsql.Update<T>().SetSource(entities).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public virtual async Task HardDeleteAsync(T entity)
|
||||
=> await _fsql.Delete<T>().Where(e => e.Id == entity.Id).ExecuteAffrowsAsync();
|
||||
|
||||
public virtual async Task HardDeleteRangeAsync(IEnumerable<T> entities)
|
||||
{
|
||||
var ids = entities.Select(e => e.Id).ToList();
|
||||
if (ids.Count > 0)
|
||||
await _fsql.Delete<T>().Where(e => ids.Contains(e.Id)).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public virtual Task<int> SaveChangesAsync()
|
||||
=> Task.FromResult(1); // FreeSql 即时提交,无 SaveChanges 语义,保持接口兼容
|
||||
}
|
||||
Reference in New Issue
Block a user