创建todo接口
新建控制器
新建文件
+ webapi工程
./Controllers/TodoController.cs
添加类
using Microsoft.AspNetCore.Mvc;
using MyToDo.Api.Context;
using MyToDo.Api.Service;
namespace MyToDo.Api.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class TodoController:ControllerBase
{
private readonly IToDoService service;
public TodoController(IToDoService toDoService)
{
this.service = toDoService;
}
[HttpGet]
public async Task<ApiReponse> Get(int id) => await service.GetSingleAsync(id);
[HttpGet]
public async Task<ApiReponse> GetAll(int id) => await service.GetAllAsync();
[HttpPost]
public async Task<ApiReponse> Update([FromBody] Todo model) => await service.UpdateAsync(model);
[HttpPost]
public async Task<ApiReponse> Add([FromBody] Todo model)=> await service.AddAsync(model);
[HttpDelete]
public async Task<ApiReponse> Delete(int todo)=> await service.DeleteAsync(todo);
}
}
### 新建服务
#### 新建文件
+ webapi工程
./Service/ApiReponse.cs
./Service/IBaseService.cs
./Service/IToDoService.cs
./Service/ToDoService.cs
添加通用返回结果类
ApiReponse.cs
using Microsoft.AspNetCore.Components.Web;
namespace MyToDo.Api.Service
{
public class ApiReponse
{
public ApiReponse(bool status)
{
this.Status = status;
}
public ApiReponse(bool status,object Result)
{
this.Status = status;
this.Result = Result;
}
public ApiReponse(string message, bool status)
{
this.Message = message;
this.Status = status;
}
public string? Message { get; set; }
public bool Status { get; set; }
public object? Result { get; set; }
}
}
添加基础接口
IBaseService.cs
namespace MyToDo.Api.Service
{
public interface IBaseService<T>
{
Task<ApiReponse> GetAllAsync();
Task<ApiReponse> GetSingleAsync(int id);
Task<ApiReponse> AddAsync(T model);
Task<ApiReponse> UpdateAsync(T model);
Task<ApiReponse> DeleteAsync(int id);
}
}
添加todo接口
IToDoService.cs
using Arch.EntityFrameworkCore.UnitOfWork;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using MyToDo.Api.Context;
using System.Diagnostics.Eventing.Reader;
namespace MyToDo.Api.Service
{
public interface IToDoService : IBaseService<Todo>
{
}
}
添加TODO接口实现
using MyToDo.Api.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Arch.EntityFrameworkCore.UnitOfWork;
namespace MyToDo.Api.Service
{
public class ToDoService : IToDoService
{
private readonly IUnitOfWork work;
public ToDoService(IUnitOfWork work)
{
this.work = work;
}
public async Task<ApiReponse> AddAsync(Todo model)
{
try
{
await work.GetRepository<Todo>().InsertAsync(model);
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<Todo>();
var todo = await resposity.GetFirstOrDefaultAsync(predicate:x=>x.Id.Equals(id));
resposity.Delete(todo);
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()
{
try
{
//获取数据
var resposity = work.GetRepository<Todo>();
//
var todos = await resposity.GetPagedListAsync();
return new ApiReponse(true,todos);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message, false);
}
}
public async Task<ApiReponse> GetSingleAsync(int id)
{
try
{
//获取数据
var resposity = work.GetRepository<Todo>();
//
var todo = await resposity.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(id));
return new ApiReponse(true, todo);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message, false);
}
}
public async Task<ApiReponse> UpdateAsync(Todo model)
{
try
{
//获取数据
var resposity = work.GetRepository<Todo>();
//
var todo = await resposity.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(model.Id));
if(todo == null)
return new ApiReponse("修改失败,数据库中无给定条件的数据项",false);
todo.Title=model.Title;
todo.UpdateDate=DateTime.Now;
todo.CreateDate = model.CreateDate;
todo.Content = model.Content;
resposity.Update(todo);
if (await work.SaveChangesAsync() > 0)
return new ApiReponse(true);
return new ApiReponse(false);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message, false);
}
}
}
}
依赖注入
放在.AddCustomRepository<User, UserRepository>();
后面
//添加注入
builder.Services.AddTransient<IToDoService, ToDoService>();