diff --git a/src/Infrastructure/Repository/IRepositoryBase.cs b/src/Infrastructure/Repository/IRepositoryBase.cs new file mode 100644 index 0000000..78f47f6 --- /dev/null +++ b/src/Infrastructure/Repository/IRepositoryBase.cs @@ -0,0 +1,30 @@ +using System.Linq.Expressions; +using SqlSugar; + +namespace Infrastructure.Repository; + +public interface IRepositoryBase where T : class, new() +{ + ISqlSugarClient DbContext { get; } + + Task GetByIdAsync(long id); + + Task> GetAllAsync(); + + Task GetFirstByExpressionAsync(Expression> expression); + + Task AddSnowflakeAsync(T entity); + + Task> AddSnowflakesAsync(IList entities); + + Task> QueryPageAsync(Expression>? whereExpression, int pageIndex = 1, int pageSize = 20, + Expression>? orderExpression = null, OrderByType orderByType = OrderByType.Asc); + + Task UpdateColumnsAsync(T entity, Expression> expression); + + Task DeleteByIdAsync(long id); + + Task DeleteAsync(T entity); + + Task DeletedByExpressionAsync(Expression> expression); +} \ No newline at end of file diff --git a/src/Infrastructure/Repository/RepositoryBase.cs b/src/Infrastructure/Repository/RepositoryBase.cs new file mode 100644 index 0000000..38a1b86 --- /dev/null +++ b/src/Infrastructure/Repository/RepositoryBase.cs @@ -0,0 +1,68 @@ +using System.Linq.Expressions; +using SqlSugar; +using SqlSugar.Extensions; + +namespace Infrastructure.Repository; + +public class RepositoryBase(ISqlSugarClient context) : IRepositoryBase where T : class, new() +{ + public ISqlSugarClient DbContext => context; + + public async Task GetByIdAsync(long id) + { + return await context.Queryable().InSingleAsync(id); + } + + public async Task AddSnowflakeAsync(T entity) + { + return await context.Insertable(entity).ExecuteReturnSnowflakeIdAsync(); + } + + public async Task> AddSnowflakesAsync(IList entities) + { + return await context.Insertable(entities).ExecuteReturnSnowflakeIdListAsync(); + } + + public async Task> QueryPageAsync(Expression>? whereExpression, int pageIndex = 1, + int pageSize = 20, + Expression>? orderExpression = null, OrderByType orderByType = OrderByType.Asc) + { + RefAsync totalCount = 0; + var list = await context.Queryable() + .OrderByIF(orderExpression != null, orderExpression, orderByType) + .WhereIF(whereExpression != null, whereExpression) + .ToPageListAsync(pageIndex, pageSize, totalCount); + var pageCount = Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal()).ObjToInt(); + return new PageData(pageIndex, pageCount, totalCount, pageSize, list); + } + + public async Task> GetAllAsync() + { + return await context.Queryable().ToListAsync(); + } + + public async Task GetFirstByExpressionAsync(Expression> expression) + { + return await context.Queryable().Where(expression).FirstAsync(); + } + + public async Task UpdateColumnsAsync(T entity, Expression> expression) + { + return await context.Updateable(entity).UpdateColumns(expression).ExecuteCommandHasChangeAsync(); + } + + public async Task DeleteByIdAsync(long id) + { + return await context.Deleteable().In(id).ExecuteCommandHasChangeAsync(); + } + + public async Task DeleteAsync(T entity) + { + return await context.Deleteable(entity).ExecuteCommandHasChangeAsync(); + } + + public async Task DeletedByExpressionAsync(Expression> expression) + { + return await context.Deleteable().Where(expression).ExecuteCommandHasChangeAsync(); + } +} \ No newline at end of file