asp.net zero 8.2 学习-5-实现增删改查服务及API测试

系列目录:
asp.net zero 8.2 学习-1-安装

asp.net zero 8.2 学习-2-创建一个页面

asp.net zero 8.2 学习-3-添加实体,并迁移到数据库

asp.net zero 8.2 学习-4-创建接口及服务

asp.net zero 8.2 学习-5-实现增删改查服务及API测试

asp.net zero 8.2 学习-6-权限控制

asp.net zero 8.2 学习-7-展示实体列表,实现查询,分页,过滤,排序功能

asp.net zero 8.2 学习-8-实现在页面添加、编辑、删除、查看实体

asp.net zero 8.2 学习-9-多租户设置,发送邮件配置

asp.net zero 8.2 学习-10-发布到IIS

asp.net zero 8.2 学习-11-Metronic替换google字体,加速网页加载速度

在asp.net zero 中实现单表增删改查:

编写增删改查服务方法
API测试服务

删除实体

注意在删除实体Delete(EntityDto input),可以先查询再修改,也可以直接删除实体,删除实体注意要转换成主键类型

public async Task Delete(EntityDto input)
{
    //先查询再修改
    //var demoObject = await _demoObjectRepository.GetAsync(input.Id);
    //await _demoObjectRepository.DeleteAsync(demoObject);

    //直接删除实体,注意转换id,不转换则删不了
    await _demoObjectRepository.DeleteAsync((int)input.Id);
}

查询实体

在分页查询数据集合时,需要先排序再映射,可能是Entity Framework 3.1的问题,也可能是System.Linq.Dynamic.Core程序集的问题,如果先映射再排序分页,就会出现错误:具体原因还没找到,另外一点CountAsync需要引用using Abp.Linq.Extensions; OrderBy动态查询需要引用using System.Linq.Dynamic.Core;

查询实体报的错误:

下面是修改后的代码(先排序再映射):

public async Task<PagedResultDto<GetDemoObjectForViewDto>> GetAll(GetAllDemoObjectInput input)
{
    //注意这里要用Abp.Linq.Extensions,否则返回的是IEnumerable类型
    var filter = _demoObjectRepository.GetAll()
        .WhereIf(!string.IsNullOrWhiteSpace(input.Filter), e => e.Name.Contains(input.Filter))
        .WhereIf(!string.IsNullOrWhiteSpace(input.NameFilter), e => e.Name.Equals(input.NameFilter));

    //先排序,再映射
    filter = filter.OrderBy(input.Sorting ?? "id asc");//OrderBy来自System.Linq.Dynamic.Core

    var query = (from o in filter
                    orderby o.Id
                    select new GetDemoObjectForViewDto()
                    {
                        DemoObject = ObjectMapper.Map<DemoObjectDto>(o)
                    });

    var totalCount = await query.CountAsync(); //CountAsync来自Abp.Linq.Extensions

    var demoObject = await query
        .PageBy(input)
        .ToListAsync();


    var result = new PagedResultDto<GetDemoObjectForViewDto>()
    {
        TotalCount = totalCount,
        Items = demoObject
    };
    return result;

}

下面是整个服务类:

using Abp.Application.Services.Dto;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using EDU.SIS.Demo.Dtos;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Abp.Linq.Extensions;
using System.Linq.Dynamic.Core;

namespace EDU.SIS.Demo
{
    public class DemoObjectAppService : SISAppServiceBase, IDemoObjectAppService
    {
        private readonly IRepository<DemoObject> _demoObjectRepository;
        public DemoObjectAppService(IRepository<DemoObject> demoObjectRepository)
        {
            _demoObjectRepository = demoObjectRepository;
        }

        /// <summary>
        /// 创建和修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateOrEdit(CreateOrEditDemoObjectDto input)
        {
            if (input.Id==null)
            {
                //创建
               await Create(input);
            }
            else
            {
                //修改
                await Update(input);
            }
        }

        /// <summary>
        /// 创建实体
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private async Task Create(CreateOrEditDemoObjectDto input)
        {
            var demoObject = ObjectMapper.Map<DemoObject>(input);
            await  _demoObjectRepository.InsertAsync(demoObject);
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private async Task Update(CreateOrEditDemoObjectDto input)
        {
            var demoObject = await _demoObjectRepository.FirstOrDefaultAsync((int)input.Id);
            var demo = ObjectMapper.Map(input, demoObject);

            //await _demoObjectRepository.UpdateAsync(demo);
        }

        /// <summary>
        /// 数据删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task Delete(EntityDto input)
        {
            var demoObject = await _demoObjectRepository.GetAsync(input.Id);
            await _demoObjectRepository.DeleteAsync(demoObject);
        }

        /// <summary>
        /// 分页查询所有实体
        /// </summary>
        /// <param name="input">分页排序筛选</param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetDemoObjectForViewDto>> GetAll(GetAllDemoObjectInput input)
        {
            //注意这里要用Abp.Linq.Extensions,否则返回的是IEnumerable类型
            var filter = _demoObjectRepository.GetAll()
                .WhereIf(!string.IsNullOrWhiteSpace(input.Filter), e => e.Name.Contains(input.Filter))
                .WhereIf(!string.IsNullOrWhiteSpace(input.NameFilter), e => e.Name.Equals(input.NameFilter));

            //先排序,再映射
            filter = filter.OrderBy(input.Sorting ?? "id asc");//OrderBy来自System.Linq.Dynamic.Core

            var query = (from o in filter
                         orderby o.Id
                         select new GetDemoObjectForViewDto()
                         {
                             DemoObject = ObjectMapper.Map<DemoObjectDto>(o)
                         });

            var totalCount = await query.CountAsync(); //CountAsync来自Abp.Linq.Extensions

            var demoObject = await query
                .PageBy(input)
                .ToListAsync();


            var result = new PagedResultDto<GetDemoObjectForViewDto>()
            {
                TotalCount = totalCount,
                Items = demoObject
            };
            return result;

        }

        /// <summary>
        /// 获取修改数据详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<GetDemoObjectForEditOutput> GetDemoObjectForEdit(EntityDto input)
        {
            var demoObject = await _demoObjectRepository.GetAsync(input.Id);
            var result = new GetDemoObjectForEditOutput()
            {
                DemoObject = ObjectMapper.Map<DemoObjectDto>(demoObject)
            };
            return result;
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<GetDemoObjectForViewDto> GetDemoObjectForView(int id)
        {
            var demoObject = await _demoObjectRepository.GetAsync(id);
            var result = new GetDemoObjectForViewDto()
            {
               DemoObject =  ObjectMapper.Map<DemoObjectDto>(demoObject)
            };
            return result;
        }
    }
}

API测试

分页查询:

创建实体

修改实体

删除实体

在删除实体时,无法用post,提示405错误,还没找到原因,可能是需要传递http头部参数,这里只能用delete方法,可以删除实体:

查询单个实体

posted @ 2020-02-24 10:42  星空天宇  阅读(545)  评论(0编辑  收藏  举报