WPF实战学习笔记12-创建备忘录接口

创建备忘录接口

添加文件

新建文件

  • 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();
相关推荐
V搜xhliang024620 分钟前
基于深度学习的地物类型的提取
开发语言·人工智能·python·深度学习·神经网络·学习·conda
豆 腐21 分钟前
MySQL【四】
android·数据库·笔记·mysql
青椒大仙KI1134 分钟前
24/11/14 算法笔记<强化学习> 马尔可夫
人工智能·笔记·机器学习
南城夏季34 分钟前
蓝领招聘二期笔记
前端·javascript·笔记
昔舍1 小时前
C#笔记(3)
笔记·c#
VertexGeek1 小时前
Rust学习(四):作用域、所有权和生命周期:
java·学习·rust
小小码神Sundayx1 小时前
三、模板与配置(下)
笔记·微信小程序
spy47_1 小时前
JavaEE 重要的API阅读
java·笔记·java-ee·api文档阅读
抱走江江2 小时前
SpringCloud框架学习(第二部分:Consul、LoadBalancer和openFeign)
学习·spring·spring cloud
不会编程的懒洋洋3 小时前
Spring Cloud Eureka 服务注册与发现
java·笔记·后端·学习·spring·spring cloud·eureka