备忘录控制器(MemoController)和待办事项控制器?(ToDoController)功能实现差不多一样。基本套路就是:
- 定义控制器(Controller)
- 定义数据传输层(Dto)
- 配置实体类(Entity)和数据传输类(Dto) 关系映射(Auto Mapper)
- 定义服务接口(IService)
- 实现服务接口 (Service)
- 把服务注入控制器中使用
- 最后在 Program.cs 进行依赖注入
/// <summary>
/// 备忘录控制器
/// </summary>
[ApiController]
[Route("api/[controller]/[action]")]
public class MemoController : ControllerBase
{
}
/// <summary>
/// 备忘录数据传输实体
/// </summary>
public class MemoDto : BaseDto
{
private string title;
private string content;
public string Title
{
get { return title; }
set { title = value; OnPropertyChanged(); }
}
public string Content
{
get { return content; }
set { content = value; OnPropertyChanged(); }
}
}
public class AutoMapperProFile:MapperConfigurationExpression
{
public AutoMapperProFile()
{
/// 实体类和数据传输类进行映射
CreateMap<ToDo, ToDoDto>().ReverseMap();
CreateMap<Memo, MemoDto>().ReverseMap();
}
}
public interface IMemoService: IBaseService<MemoDto>
{
}
/// <summary>
///备忘录的实现
/// </summary>
public class MemoService : IMemoService
{
private readonly IUnitOfWork work;
private readonly IMapper mapper;
public MemoService(IUnitOfWork work,IMapper mapper)
{
this.work = work;
this.mapper = mapper;
}
public async Task<ApiResponse> AddAsync(MemoDto model)
{
try
{
var doto= mapper.Map<Memo>(model);//进行数据映射转换
await work.GetRepository<Memo>().InsertAsync(doto);
if (await work.SaveChangesAsync() > 0) //保存成功
{
return new ApiResponse(true, model); //返回true,并把添加的实体返回
}
return new ApiResponse("添加数据失败");
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
public async Task<ApiResponse> DeleteAsync(int id)
{
try
{
var repository= work.GetRepository<Memo>();//获取仓储
//删除之前,先进行查询
var todo = await repository.GetFirstOrDefaultAsync(predicate:x=>x.Id.Equals(id));
repository.Delete(todo);
if (await work.SaveChangesAsync() > 0) //删除成功
{
return new ApiResponse(true, "删除成功");
}
return new ApiResponse("删除数据失败");
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
public async Task<ApiResponse> GetAllAsync()
{
try
{
var todos= await work.GetRepository<Memo>().GetAllAsync();
return new ApiResponse(true, todos); //返回true,并返回所有数据
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
public async Task<ApiResponse> GetSingleAsync(int id)
{
try
{
var todo= await work.GetRepository<Memo>().GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(id));
return new ApiResponse(true, todo); //把找到的数据返回
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
public async Task<ApiResponse> UpdateAsync(MemoDto model)
{
try
{
var dbdoto = mapper.Map<Memo>(model);
var repository = work.GetRepository<Memo>();//获取仓储
//更新之前,先拿到要更新的数据
var todo = await repository.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(dbdoto.Id));
todo.Title = dbdoto.Title;
todo.Content = dbdoto.Content;
todo.UpdateDate = DateTime.Now;
repository.Update(todo);
if (await work.SaveChangesAsync() > 0) //更新成功
{
return new ApiResponse(true, "更新成功");
}
return new ApiResponse("更新数据失败");
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
}
/// <summary>
/// 备忘录控制器
/// </summary>
[ApiController]
[Route("api/[controller]/[action]")]
public class MemoController : ControllerBase
{
private readonly IMemoService service;
public MemoController(IMemoService service)
{
this.service = service;
}
[HttpGet]
public async Task<ApiResponse> Get(int id) => await service.GetSingleAsync(id);
[HttpGet]
public async Task<ApiResponse> GetAll() => await service.GetAllAsync();
[HttpPost]
public async Task<ApiResponse> Add([FromBody] MemoDto model) => await service.AddAsync(model);
[HttpPost]
public async Task<ApiResponse> Update([FromBody] MemoDto model) => await service.UpdateAsync(model);
[HttpDelete]
public async Task<ApiResponse> Delete(int id) => await service.DeleteAsync(id);
}
builder.Services.AddTransient<IMemoService, MemoService>();
根据传入的条件进行分页查询。
public class QueryParameter
{
/// <summary>
/// 页数
/// </summary>
public int PageIndex { get; set; }
/// <summary>
/// 总数
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 查询条件
/// </summary>
public string? Search { get; set; }
}
例如:MemoService 服务实现层,改造GetAllAsync 查询接口
public async Task<ApiResponse> GetAllAsync(QueryParameter query)
{
try
{
var todos = await work.GetRepository<Memo>()
//根据标题查,如果传过来的Search 为空,直接过。否则就匹配标题。
.GetPagedListAsync(predicate: x => string.IsNullOrWhiteSpace(query.Search) ? true : x.Title.Equals(query.Search),
pageIndex: query.PageIndex,
pageSize: query.PageSize,
orderBy:source=>source.OrderByDescending(t=>t.CreateDate) //根据创建时间进行排序
);
return new ApiResponse(true, todos); //返回true,并返回所有数据
}
catch (Exception ex)
{
return new ApiResponse(ex.Message);
}
}
[FromQuery]
?特性作用:将查询字符串参数值绑定到对应的 QueryParameter?参数上