欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

abp(net core)+easyui+efcore仓储系统——展现层实现增删改查之控制器(六)

程序员文章站 2022-07-09 21:00:57
通过前面三篇文章的介绍,我们学习了如何创建实体,如何创建数据库操作,如何创建应用服务。在上一文章中我们在应用层实现了对数据库的CURD操作。在本篇文章中,主要是使用常规的MVC方式来实现增删改查的功能,通过完善Controller、View、ViewModel,以及调试修改控制器来实现展示层的增删改... ......

abp(net core)+easyui+efcore仓储系统目录

abp(net core)+easyui+efcore仓储系统——abp总体介绍(一)

abp(net core)+easyui+efcore仓储系统——解决方案介绍(二)

abp(net core)+easyui+efcore仓储系统——领域层创建实体(三)

 abp(net core)+easyui+efcore仓储系统——定义仓储并实现 (四)

abp(net core)+easyui+efcore仓储系统——创建应用服务(五)

 

       通过前面三篇文章的介绍,我们学习了如何创建实体,如何创建数据库操作,如何创建应用服务。在上一文章中我们在应用层实现了对数据库的curd操作。在本篇文章中,主要是使用常规的mvc方式来实现增删改查的功能,通过完善controller、view、viewmodel,以及调试修改控制器来实现展示层的增删改查。最终实现效果如下图:

 abp(net core)+easyui+efcore仓储系统——展现层实现增删改查之控制器(六)

一、创建modulecontroller

      abp对asp.net net core mvc  controllers进行了集成,通过abp网站创建的项目会自动创建一个controller基类,这个controller基类继承自abpcontroller, 我们即可使用abp附加给我们的以下强大功能:

  • 本地化
  • 异常处理
  • 对返回的jsonresult进行包装
  • 审计日志
  • 权限认证([abpmvcauthorize]特性)
  • 工作单元(默认未开启,通过添加[unitofwork]开启)

      我们创建的abp.tplms项目,也同样创建了一个控制器基类,具体位置如下图。

 abp(net core)+easyui+efcore仓储系统——展现层实现增删改查之控制器(六)

      1. 在visual studio 2017的“解决方案资源管理器”中,右键单击在领域层“abp.tplms.web.mvc”项目中的controller目录。 选择“添加” > “新建项…”。如下图。

 abp(net core)+easyui+efcore仓储系统——展现层实现增删改查之控制器(六)

    2. 在弹出对话框“添加新项-abp.tplms.web.mvc”中选择“控制器类”,然后在名称输入框中输入“modulecontroller”,然后点击“添加”按钮。如下图。

 abp(net core)+easyui+efcore仓储系统——展现层实现增删改查之控制器(六)

      3.在visual studio 2017中打开我们刚才创建modulecontroller.cs,并继承自tplmscontrollerbase,并增加列表与修改方法。通过构造函数注入对应用服务的依赖。具体代码如下。

 

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using abp.aspnetcore.mvc.authorization;
using abp.runtime.validation;
using abp.tplms.controllers;
using abp.tplms.modules;
using abp.tplms.modules.dto;
using abp.tplms.web.models.module;
using microsoft.aspnetcore.mvc;
using microsoft.entityframeworkcore; 

// for more information on enabling mvc for empty projects, visit https://go.microsoft.com/fwlink/?linkid=397860 

namespace abp.tplms.web.controllers
{

    [abpmvcauthorize]
    public class modulecontroller : tplmscontrollerbase
    {

        // get: /<controller>/
        public iactionresult index()
        {

            var output = _moduleappservice.getallasync();
            var model = new editmodulemodalviewmodel
            {
                module = output.result.items.first(),
                modules = output.result.items
            };
            return view(model);
        }
   
            private readonly imoduleappservice _moduleappservice;      

            public modulecontroller(imoduleappservice moduleappservice)
            {
            _moduleappservice = moduleappservice;          

            }

        [httppost]
        [validateantiforgerytoken]
        public actionresult create(createupdatemoduledto updatedto)
        {
            _moduleappservice.createasync(updatedto);
            var output = _moduleappservice.getallasync();
            return partialview("_list", output.result);
        }

     public iactionresult create()
        {
            return view();
        }

        [httppost]
        [disablevalidation]
        public actionresult edit(int id,editmodulemodalviewmodel updatedto)
        {
            if (id != updatedto.module.id)
            {
                return notfound();
            }

            if (modelstate.isvalid)
            {
                try
                {
                   var module= automapper.mapper.map<createupdatemoduledto>(updatedto.module);

                    _moduleappservice.updateasync(module);           

                }
                catch (dbupdateconcurrencyexception ex)
                {
                    if (!dtoexists(updatedto.module.id))
                    {
                        return notfound();
                    }
                    else
                    {
                        throw ex;
                    }
                }
                return redirecttoaction(nameof(index));
            }
            return view(updatedto);          
        }

        private bool dtoexists(long id)
        {
            return _moduleappservice.getallasync().result.items.any(e => e.id == id);
        }

        // get: module/edit/5
        public iactionresult edit(int? id)
        {
            if (id == null)
            {
                return notfound();
            }

            var module =  _moduleappservice.getallasync().result.items.singleordefault(m => m.id == id);

            if (module == null)
            {
                return notfound();
            }
            var model = new editmodulemodalviewmodel
            {
                module = module
            };
            return view(model);
            //return ok(cargo.result);
        }

  // get: module/delete/5
        public  iactionresult delete(int? id)
        {
            if (id == null)
            {
                return notfound();
            }
                var module = _moduleappservice.getallasync().result.items.singleordefault(m => m.id == id);

            if (module == null)
            {
                return notfound();
            }

            var model = new editmodulemodalviewmodel
            {
                module = automapper.mapper.map<createupdatemoduledto>(module)
            };

            return view(model);
        }

        // post: module/delete/5
        [httppost, actionname("delete")]
        [validateantiforgerytoken]
        public async task<iactionresult> deleteconfirmed(int id)
        {
            try
            {
                await _moduleappservice.deleteasync(id);
            }
            catch (exception ex)
            {
                return view(ex.message);
                //throw;
            }
          return redirecttoaction(nameof(index));
        }
}
}