备忘录接口增删(CURD)改查实现
一.添加备忘录控制器(MemoController)
备忘录控制器(MemoController)和待办事项控制器 (ToDoController)功能实现差不多一样。基本套路就是:
- 定义控制器(Controller)
- 定义数据传输层(Dto)
- 配置实体类(Entity)和数据传输类(Dto) 关系映射(Auto Mapper)
- 定义服务接口(IService)
- 实现服务接口 (Service)
- 把服务注入控制器中使用
- 最后在 Program.cs 进行依赖注入
1.在 MyToDo.Api 项目Controllers文件夹中,定义(MemoController)备忘录控制器
/// <summary>
/// 备忘录控制器
/// </summary>
[ApiController]
[Route("api/[controller]/[action]")]
public class MemoController : ControllerBase
{
}
2.在 MyToDo.Shared 项目Dtos文件夹中,定义(MemoDto)备忘录数据传输层
/// <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(); }
}
}
3.在MyToDo.Api 项目 Extensions 文件夹的 AutoMapperProFile 类中配置Auto Mapper
public class AutoMapperProFile:MapperConfigurationExpression
{
public AutoMapperProFile()
{
/// 实体类和数据传输类进行映射
CreateMap<ToDo, ToDoDto>().ReverseMap();
CreateMap<Memo, MemoDto>().ReverseMap();
}
}
4.在MyToDo.Api 项目Service 文件夹中,定义备忘录服务接口(IMemoService)
public interface IMemoService: IBaseService<MemoDto>
{
}
5.同样,在MyToDo.Api 项目Service 文件夹中,实现(MemoService)备忘录服务接口
/// <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);
}
}
}
6.接着,在 MemoController 控制器中注入并使用IMemoService 服务
/// <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);
}
7.最后,在 Program.cs 中注入 IMemoService 服务
builder.Services.AddTransient<IMemoService, MemoService>();
二.高级查询实现
根据传入的条件进行分页查询。
1.在MyToDo.Shared 项目中,创建通用的查询实体类(QueryParameter)
public class QueryParameter
{
/// <summary>
/// 页数
/// </summary>
public int PageIndex { get; set; }
/// <summary>
/// 总数
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 查询条件
/// </summary>
public string? Search { get; set; }
}
2.改造 IBaseService 基类服务接口,传入通用查询实体类(QueryParameter)
3.在备忘录或待办事项接口服务实现层,去改造实现高级查询逻辑
例如: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);
}
}
4.最后,修改备忘录(MemoController)控制器和待办事项(ToDoController)控制器 GetAll 方法的参数传入。
[FromQuery]
特性作用:将查询字符串参数值绑定到对应的 QueryParameter 参数上