1、新建MenoDto.cs
/// <summary>
/// 备忘录传输实体
/// </summary>
public class MemoDto : BaseDto
{
private string title;
/// <summary>
/// 标题
/// </summary>
public string Title
{
get { return title; }
set { title = value; OnPropertyChanged(); }
}
private string content;
/// <summary>
/// 内容
/// </summary>
public string Content
{
get { return content; }
set { content = value; OnPropertyChanged(); }
}
}
2、添加映射关系
CreateMap<Memo, MemoDto>().ReverseMap();
3、新建服务接口IMemoService
public interface IMemoService : IBaseService<MemoDto>
{
}
4、新建服务MemoService
public class MemoService : IMemoService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper mapper;
public MemoService(IMapper mapper, IUnitOfWork unitOfWork)
{
this.mapper = mapper;
_unitOfWork = unitOfWork;
}
/// <summary>
/// 新增备忘录
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<ApiResponse> AddEntityAsync(MemoDto model)
{
try
{
var memo = mapper.Map<Memo>(model);
await _unitOfWork.GetRepository<Memo>().InsertAsync(memo);
if(await _unitOfWork.SaveChangesAsync() > 0)
{
return new ApiResponse(true, memo);
}
else
{
return new ApiResponse(false, "添加数据失败!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
/// <summary>
/// 删除备忘录
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResponse> DeleteEntityAsync(int id)
{
try
{
var repository = _unitOfWork.GetRepository<Memo>();
var memo = await repository.GetFirstOrDefaultAsync(predicate: t => t.Id.Equals(id));
if(memo != null)
{
repository.Delete(memo);
}
if(await _unitOfWork.SaveChangesAsync() > 0)
{
return new ApiResponse(true, "删除数据成功!");
}
else
{
return new ApiResponse(false, "删除数据失败!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
/// <summary>
/// 查询所有备忘录
/// </summary>
/// <returns></returns>
public async Task<ApiResponse> GetAllAsync()
{
try
{
var repository = _unitOfWork.GetRepository<Memo>();
var memo = await repository.GetAllAsync();
if(memo != null)
{
return new ApiResponse(true, memo);
}
else
{
return new ApiResponse(false, "查询数据失败!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
/// <summary>
/// 根据Id查询备忘录
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResponse> GetSingleAsync(int id)
{
try
{
var repository = _unitOfWork.GetRepository<Memo>();
var memo = await repository.GetFirstOrDefaultAsync(predicate: t => t.Id.Equals(id));
if(memo != null)
{
return new ApiResponse(true, memo);
}
else
{
return new ApiResponse(false, $"查询Id={id}的数据失败!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
/// <summary>
/// 更新备忘录
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<ApiResponse> UpdateEntityAsync(MemoDto model)
{
try
{
var dbmemo = mapper.Map<Memo>(model);
var repository = _unitOfWork.GetRepository<Memo>();
var memo = await repository.GetFirstOrDefaultAsync(predicate: t => t.Id.Equals(dbmemo.Id));
if( memo != null)
{
memo.Title = dbmemo.Title;
memo.Content = dbmemo.Content;
memo.UpdateDate = DateTime.Now;
repository.Update(memo);
if(await _unitOfWork.SaveChangesAsync() > 0)
{
return new ApiResponse(true, "更新数据成功!");
}
else
{
return new ApiResponse(false, "更新数据失败!");
}
}
else
{
return new ApiResponse(false, $"未查询到Id={dbmemo.Id}的数据!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
}
5、新增MemoController控制器
public class MemoController : BaseApiController
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMemoService memoService;
public MemoController(IMemoService memoService, IUnitOfWork unitOfWork)
{
this.memoService = memoService;
_unitOfWork = unitOfWork;
}
[HttpGet]
public async Task<ApiResponse> GetMemoById(int Id)
{
return await memoService.GetSingleAsync(Id);
}
[HttpPost]
public async Task<ApiResponse> AddMemo([FromBody] MemoDto memoDto)
{
return await memoService.AddEntityAsync(memoDto);
}
[HttpDelete]
public async Task<ApiResponse> DeleteMemo(int Id)
{
return await memoService.DeleteEntityAsync(Id);
}
[HttpGet]
public async Task<ApiResponse> GetAllMemo()
{
return await memoService.GetAllAsync();
}
[HttpPost]
public async Task<ApiResponse> UpdateMemo(MemoDto memoDto)
{
return await memoService.UpdateEntityAsync(memoDto);
}
}
6、在program.cs添加服务
builder.Services.AddTransient<IMemoService, MemoService>();
7、F5运行项目
8、针对查询数据可以做个优化,增加查询的页数、内容、数据量等,新建查询参数类
public class QueryParameter
{
public int PageIndex { get; set; }
public int PageSize { get; set; }
public string Search { get; set; }
}
9、在IToDoService.cs和IMemoService.cs中添加分页查询的接口
public interface IToDoService : IBaseService<TodoDto>
{
Task<ApiResponse> GetPageListAllAsync(QueryParameter parameter);
}
public interface IMemoService : IBaseService<MemoDto>
{
Task<ApiResponse> GetPageListAllAsync(QueryParameter parameter);
}
10、在ToDoService.cs中实现接口和MemoService.cs中实现接口
/// <summary>
/// 分页查询所有数据
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<ApiResponse> GetPageListAllAsync(QueryParameter parameter)
{
try
{
var repository = unitOfWork.GetRepository<ToDo>();
var todo = await repository.GetPagedListAsync(predicate: x => string.IsNullOrWhiteSpace(parameter.Search) ? true : x.Title.Contains(parameter.Search),
pageIndex: parameter.PageIndex,
pageSize: parameter.PageSize,
orderBy: y => y.OrderByDescending(t => t.CreateDate));
if (todo != null)
{
return new ApiResponse(true, todo);
}
else
{
return new ApiResponse(false, "查询数据失败!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
/// <summary>
/// 分页查询所有备忘录
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<ApiResponse> GetPageListAllAsync(QueryParameter parameter)
{
try
{
var repository = _unitOfWork.GetRepository<Memo>();
var memo = await repository.GetPagedListAsync(predicate: x => string.IsNullOrWhiteSpace(parameter.Search) ? true : x.Title.Contains(parameter.Search),
pageIndex: parameter.PageIndex,
pageSize: parameter.PageSize,
orderBy: y => y.OrderByDescending(t => t.CreateDate));
if (memo != null)
{
return new ApiResponse(true, memo);
}
else
{
return new ApiResponse(false, "查询数据失败!");
}
}
catch (Exception ex)
{
return new ApiResponse(false, ex.Message);
}
}
11、在ToDoController和MemoController中添加代码
[HttpGet]
public async Task<ApiResponse> GetAllPageListToDo([FromQuery] QueryParameter parameter)
{
return await toDoService.GetPageListAllAsync(parameter);
}
[HttpGet]
public async Task<ApiResponse> GetAllPageListMemo([FromQuery] QueryParameter parameter)
{
return await memoService.GetPageListAllAsync(parameter);
}
12、F5运行项目