WPF Practical Study Notes 12 - Creating a Memo Interface

Create a memo interface

add files

create a new file

  • MyToDo.Api

./Controllers/MemoController.cs

./Service/IMemoService.cs

./Service/MemoService.cs

  • MyToDo.Share

    ./Parameters/QueryParameter.cs

QueryParameter.cs

query parameter class

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

memo interface

using MyToDo.Api.Context;

namespace MyToDo.Api.Service
{
    public interface IMemoService : IBaseService<Memo>
    {
    }
}

MemoService.cs

Memorandum interface implementation, pay attention to the addition of query parameters

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

Deleted the id parameter required by getall and added the QueryParameter parameter

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);

    }
}

Modify the todo interface

  • TodoController.cs

    [HttpGet]
    public async Task<ApiReponse> GetAll([FromQuery] QueryParameter param) => await service.GetAllAsync(param);
    
  • ToDoService.cs

    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);
        }
    }
    

dependency injection

Program.cs
builder.Services.AddTransient<IMemoService,MemoService>();
AutoMapperProfilec.s
CreateMap<Memo, MemoDto>().ReverseMap();

Guess you like

Origin blog.csdn.net/xinzhiya001/article/details/131909377