创建备忘录接口
添加文件
新建文件
- MyToDo.Api
./Controllers/MemoController.cs
./Service/IMemoService.cs
./Service/MemoService.cs
-
MyToDo.Share
./Parameters/QueryParameter.cs
QueryParameter.cs
查询参数类
c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyToDo.Share.Parameters
{
/// <summary>
/// 查询参数
/// </summary>
public class QueryParameter
{
/// <summary>
/// 页序号
/// </summary>
public int PageIndex { get;set; }
/// <summary>
/// 页内项数量
/// </summary>
public int PageSize { get;set; } = 10;
/// <summary>
/// 查询字符串
/// </summary>
public string Search { get; set; } = "";
}
}
IMemoService.cs
备忘录接口
using MyToDo.Api.Context;
namespace MyToDo.Api.Service
{
public interface IMemoService : IBaseService<Memo>
{
}
}
MemoService.cs
备忘录接口实现,注意在增加了查询参数
using Arch.EntityFrameworkCore.UnitOfWork;
using AutoMapper;
using MyToDo.Api.Context;
using MyToDo.Share.Parameters;
using System.Reflection.Metadata;
namespace MyToDo.Api.Service
{
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<ApiReponse> AddAsync(Memo model)
{
try
{
var memo = mapper.Map<Memo>(model);
await work.GetRepository<Memo>().InsertAsync(memo);
if (await work.SaveChangesAsync() > 0)
return new ApiReponse(true, model);
return new ApiReponse(false);
}
catch (Exception ex)
{
return new ApiReponse(false, ex);
}
}
public async Task<ApiReponse> DeleteAsync(int id)
{
try
{
//获取数据
var resposity = work.GetRepository<Memo>();
var memo = await resposity.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(id));
resposity.Delete(memo);
if (await work.SaveChangesAsync() > 0)
return new ApiReponse(true);
return new ApiReponse(false);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message, false);
}
}
public async Task<ApiReponse> GetAllAsync(QueryParameter parameter)
{
try
{
//获取数据
var resposity = work.GetRepository<Memo>();
//根据查询条件查询
var memos = await resposity.GetPagedListAsync(predicate:x=> string.IsNullOrWhiteSpace(parameter.Search)?true:x.Title.Equals(parameter.Search),pageIndex:parameter.PageIndex,pageSize:parameter.PageSize,orderBy:source=> source.OrderByDescending(t=>t.CreateDate));
return new ApiReponse(true, memos);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message, false);
}
}
public async Task<ApiReponse> GetSingleAsync(int id)
{
try
{
//获取数据
var resposity = work.GetRepository<Memo>();
//
var memo = await resposity.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(id));
return new ApiReponse(true, memo);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message, false);
}
}
public async Task<ApiReponse> UpdateAsync(Memo model)
{
try
{
var dbmemo = mapper.Map<Memo>(model);
//获取数据
var resposity = work.GetRepository<Memo>();
//
var memo = await resposity.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(dbmemo.Id));
if (memo == null)
return new ApiReponse("修改失败,数据库中无给定条件的数据项", false);
memo.Title = dbmemo.Title;
memo.UpdateDate = DateTime.Now;
memo.CreateDate = dbmemo.CreateDate;
memo.Content = dbmemo.Content;
resposity.Update(memo);
if (await work.SaveChangesAsync() > 0)
return new ApiReponse(true);
return new ApiReponse(false);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message, false);
}
}
}
}
MemoController.cs
删除了getall所需要的id参数,增加了QueryParameter参数
c#
using Microsoft.AspNetCore.Mvc;
using MyToDo.Api.Context;
using MyToDo.Api.Service;
using MyToDo.Share.Parameters;
namespace MyToDo.Api.Controllers
{
public class MemoController:ControllerBase
{
private readonly IMemoService service;
public MemoController(IMemoService tService)
{
this.service = tService;
}
[HttpGet]
public async Task<ApiReponse> Get(int id) => await service.GetSingleAsync(id);
[HttpGet]
public async Task<ApiReponse> GetAll([FromQuery] QueryParameter param) => await service.GetAllAsync(param);
[HttpPost]
public async Task<ApiReponse> Update([FromBody] Memo model) => await service.UpdateAsync(model);
[HttpPost]
public async Task<ApiReponse> Add([FromBody] Memo model) => await service.AddAsync(model);
[HttpDelete]
public async Task<ApiReponse> Delete(int id) => await service.DeleteAsync(id);
}
}
修改todo接口
-
TodoController.cs
c#[HttpGet] public async Task<ApiReponse> GetAll([FromQuery] QueryParameter param) => await service.GetAllAsync(param);
-
ToDoService.cs
c#public async Task<ApiReponse> GetAllAsync(QueryParameter parameter) { try { //获取数据 var resposity = work.GetRepository<Todo>(); //根据查询条件查询 var todos = await resposity.GetPagedListAsync(predicate: x => string.IsNullOrWhiteSpace(parameter.Search) ? true : x.Title.Equals(parameter.Search), pageIndex: parameter.PageIndex, pageSize: parameter.PageSize, orderBy: source => source.OrderByDescending(t => t.CreateDate)); return new ApiReponse(true, todos); } catch (Exception ex) { return new ApiReponse(ex.Message, false); } }
依赖注入
Program.cs
c#
builder.Services.AddTransient<IMemoService,MemoService>();
AutoMapperProfilec.s
c#
CreateMap<Memo, MemoDto>().ReverseMap();