using System.Linq.Expressions; using MyHomePage.Api.Infrastructure.Database; using SqlSugar; namespace MyHomePage.Api.Repositories; /// 通用仓储接口:覆盖最常用的 CRUD 能力。 /// 实体类型 public interface IBaseRepository where T : class, new() { Task GetByIdAsync(int id); Task> ListAsync(Expression>? where = null); Task InsertAsync(T entity); Task UpdateAsync(T entity); Task DeleteAsync(int id); } /// 通用仓储实现:直接包装 SqlSugar 客户端。 public class BaseRepository : IBaseRepository where T : class, new() { protected readonly ISqlSugarClient Db; public BaseRepository(SqlSugarContext ctx) { Db = ctx.Db; } public Task GetByIdAsync(int id) => Db.Queryable().InSingleAsync(id); public Task> ListAsync(Expression>? where = null) => where is null ? Db.Queryable().ToListAsync() : Db.Queryable().Where(where).ToListAsync(); public Task InsertAsync(T entity) => Db.Insertable(entity).ExecuteReturnIdentityAsync(); public Task UpdateAsync(T entity) => Db.Updateable(entity).ExecuteCommandAsync(); public Task DeleteAsync(int id) => Db.Deleteable().In(id).ExecuteCommandAsync(); }