WPF实战学习笔记10-创建todo接口

创建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 Get(int id) => await service.GetSingleAsync(id);

        [HttpGet]
        public async Task GetAll(int id) => await service.GetAllAsync();

        [HttpPost]
        public async Task Update([FromBody] Todo model) => await service.UpdateAsync(model);

        [HttpPost]
        public async Task Add([FromBody] Todo model)=> await service.AddAsync(model);

        [HttpDelete]
        public async Task 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
    {
        Task GetAllAsync();
        Task GetSingleAsync(int id);

        Task AddAsync(T model);

        Task UpdateAsync(T model);

        Task 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接口实现

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 AddAsync(Todo model)
        {
            try
            {
                await work.GetRepository().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 DeleteAsync(int id)
        {
            try
            {
                //获取数据
                var resposity = work.GetRepository();
                
                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 GetAllAsync()
        {
            try
            {
                //获取数据
                var resposity = work.GetRepository();

                //
                var todos = await resposity.GetPagedListAsync();

                return new ApiReponse(true,todos);
            }
            catch (Exception ex)
            {

                return new ApiReponse(ex.Message, false);
            }
        }

        public async Task GetSingleAsync(int id)
        {
            try
            {
                //获取数据
                var resposity = work.GetRepository();

                //
                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 UpdateAsync(Todo model)
        {
            try
            {
                //获取数据
                var resposity = work.GetRepository();
                //
                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();后面

//添加注入
builder.Services.AddTransient();

你可能感兴趣的:(WPF实战学习笔记,wpf,学习,笔记)