using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using TaskManager.Entity; using TaskManager.EntityFramework; using TaskManager.EntityFramework.Repository; namespace Wood.Service.Controllers { [AllowAnonymous] public class NormalBaseController:ControllerBase where T:BaseEntity { protected readonly JobDbContext _context; protected readonly IServiceProvider _builder; protected readonly IConfiguration _configuration; protected readonly IRepository _repository; public NormalBaseController(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository) { _builder = builder; _context = context; _configuration = configuration; _repository = repository; } /// /// 通过UID获得实体 /// /// /// [HttpGet("{id}")] public async Task> GetById(int id) { var entity = await _repository.GetByIdAsync(id); if (entity == null) return NotFound(); return entity; } /// /// 创建实体 /// /// /// [HttpPost] public async Task> Create(T entity) { entity.CreationTime = DateTime.Now; var createdEntity = await _repository.AddAsync(entity); return new JsonResult(new { Code = 200, Message = "创建成功!" }); } /// /// 更新实体UID /// /// /// [HttpPut("{id}")] public async Task Update(T entity) { var _first = await _repository.GetByIdAsync(entity.UId); if (_first == null) { return new JsonResult(new { Code = 400, Message = "修改失败!" }); } await _repository.UpdateAsync(entity); return new JsonResult(new { Code = 200, Message = "修改成功!" }); } /// /// 删除实体通过UID /// /// /// [HttpDelete("{id}")] public async Task Delete(int id) { await _repository.DeleteAsync(id); return new JsonResult(new { Code = 200, Message = "删除成功!" }); ; } [HttpGet] public async Task>> GetPaged( [FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 10, [FromQuery] string sortBy = "", [FromQuery] bool isAscending = true, [FromQuery] Dictionary filters = null) { var pagingParams = new PagingParams { PageNumber = pageNumber, PageSize = pageSize, SortBy = sortBy, IsAscending = isAscending, Filters = filters }; // 可以在这里构建表达式树过滤条件 Expression> filter = null; var pagedResult = await _repository.GetPagedAsync(filter, pagingParams); return Ok(pagedResult); } } }