コード例 #1
0
        public object DeleteItems([FromBody] List <int> listId)
        {
            var msg = new JMessage()
            {
                Error = false
            };

            try
            {
                //_logger.LogInformation(LoggingEvents.LogDb, "Delete list application");
                List <int>           listRef         = new List <int>();
                List <int>           listDel         = new List <int>();
                List <AdApplication> listApplication = new List <AdApplication>();
                foreach (var id in listId)
                {
                    AdApplication obj = _context.AdApplications.FirstOrDefault(x => x.ApplicationId == id);
                    if (obj != null)
                    {
                        var pms     = _context.AdPermissions.FirstOrDefault(x => x.ApplicationCode == obj.ApplicationCode);
                        var appFunc = _context.AdAppFunctions.FirstOrDefault(x => x.ApplicationCode == obj.ApplicationCode);
                        if (appFunc != null || pms != null)
                        {
                            listRef.Add(id);
                        }
                        else
                        {
                            listDel.Add(id);
                        }
                    }
                }
                if (listRef.Count > 0)
                {
                    if (listDel.Count > 0)
                    {
                        foreach (var id in listDel)
                        {
                            AdApplication obj = _context.AdApplications.FirstOrDefault(x => x.ApplicationId == id);
                            listApplication.Add(obj);
                            _context.Remove(obj);
                        }
                        _context.SaveChanges();
                        msg.Error = true;
                        msg.Title = String.Format(CommonUtil.ResourceValue("COM_DEL_SUCCESS_LIST_ITEM_BUT_REF"), CommonUtil.ResourceValue("ADM_APP_LBL_APP").ToLower());
                        //_logger.LogError(LoggingEvents.LogDb, "Delete part of the application list successfully");
                        _actionLog.InsertActionLogDeleteItem("VIB_APPLICATION", "Delete part of the application list successfully", listApplication.ToArray(), null, "Delete");
                    }
                    else
                    {
                        msg.Error = true;
                        msg.Title = String.Format(CommonUtil.ResourceValue("COM_ERR_LIST_OBJ_REF"), CommonUtil.ResourceValue("ADM_APP_LBL_APP").ToLower());
                        //_logger.LogError(LoggingEvents.LogDb, "Delete list application fail");
                        _actionLog.InsertActionLogDeleteItem("VIB_APPLICATION", "Delete list application fail", null, null, "Error");
                    }
                }
                else
                {
                    if (listDel.Count > 0)
                    {
                        foreach (var id in listDel)
                        {
                            AdApplication obj = _context.AdApplications.FirstOrDefault(x => x.ApplicationId == id);
                            listApplication.Add(obj);

                            _context.AdApplications.Attach(obj);
                            _context.AdApplications.Remove(obj);
                        }
                        _context.SaveChanges();
                        msg.Title = String.Format(CommonUtil.ResourceValue("COM_MSG_DELETE_LIST_SUCCESS"),
                                                  CommonUtil.ResourceValue("ADM_APP_LBL_APP").ToLower());
                        //_logger.LogError(LoggingEvents.LogDb, "Delete part of the application list successfully");
                        _actionLog.InsertActionLogDeleteItem("VIB_APPLICATION", "Delete application list successfully", listApplication.ToArray(), null, "Delete");
                    }
                }
            }
            catch (Exception ex)
            {
                msg.Error  = true;
                msg.Object = ex;
                msg.Title  = String.Format(CommonUtil.ResourceValue("COM_MSG_DELETE_LIST_FAIL"), CommonUtil.ResourceValue("ADM_APP_LBL_APP").ToLower());
                //_logger.LogError(LoggingEvents.LogDb, "Delete list application fail");
                _actionLog.InsertActionLogDeleteItem("VIB_APPLICATION", "Delete list application failed: " + ex.Message, null, null, "Error");
            }
            return(Json(msg));
        }
コード例 #2
0
        public async Task <JsonResult> DeleteItems([FromBody] List <string> listId)
        {
            var msg = new JMessage()
            {
                Error = false
            };

            try
            {
                ////_logger.LogInformation(LoggingEvents.LogDb, "Delete list role");
                List <string>     listRef        = new List <string>();
                List <string>     listDel        = new List <string>();
                List <AspNetRole> listAspNetRole = new List <AspNetRole>();
                foreach (var id in listId)
                {
                    AspNetRole obj = await _roleManager.FindByIdAsync(id);

                    if (obj != null)
                    {
                        var lstRef = _context.AdUserInGroups.FirstOrDefault(x => x.RoleId == id);
                        if (lstRef != null)
                        {
                            listRef.Add(id);
                        }
                        else
                        {
                            var p = _context.AdPermissions.FirstOrDefault(x => x.RoleId == id);
                            if (p != null)
                            {
                                listRef.Add(id);
                            }
                            else
                            {
                                listDel.Add(id);
                            }
                        }
                    }
                }
                if (listRef.Count > 0)
                {
                    if (listDel.Count > 0)
                    {
                        foreach (var id in listDel)
                        {
                            AspNetRole obj = await _roleManager.FindByIdAsync(id);

                            listAspNetRole.Add(obj);
                            await _roleManager.DeleteAsync(obj);
                        }
                        msg.Error = true;
                        msg.Title = String.Format(CommonUtil.ResourceValue("COM_DEL_SUCCESS_LIST_ITEM_BUT_REF"), CommonUtil.ResourceValue("ADM_ROLE_LBL_ROLE").ToLower());
                        //_logger.LogError(LoggingEvents.LogDb, "Delete part of the role list successfully");
                        _actionLog.InsertActionLogDeleteItem("ASP_NET_ROLES", "Delete part of the role list successfully", listAspNetRole.ToArray(), null, "Delete");
                    }
                    else
                    {
                        msg.Error = true;
                        msg.Title = String.Format(CommonUtil.ResourceValue("COM_ERR_LIST_OBJ_REF"), CommonUtil.ResourceValue("ADM_ROLE_LBL_ROLE").ToLower());
                        //_logger.LogError(LoggingEvents.LogDb, "Delete list role fail");
                        _actionLog.InsertActionLogDeleteItem("ASP_NET_ROLES", "Delete list role fail", null, null, "Error");
                    }
                }
                else
                {
                    if (listDel.Count > 0)
                    {
                        foreach (var id in listDel)
                        {
                            AspNetRole obj = await _roleManager.FindByIdAsync(id);

                            listAspNetRole.Add(obj);
                            await _roleManager.DeleteAsync(obj);
                        }
                        msg.Title = String.Format(CommonUtil.ResourceValue("COM_MSG_DELETE_LIST_SUCCESS"), CommonUtil.ResourceValue("ADM_ROLE_LBL_ROLE").ToLower());
                        ////_logger.LogInformation(LoggingEvents.LogDb, "Delete list role successfully");
                        _actionLog.InsertActionLogDeleteItem("ASP_NET_ROLES", "Delete list role successfully", listAspNetRole.ToArray(), null, "Delete");
                    }
                }
            }
            catch (Exception ex)
            {
                msg.Error  = true;
                msg.Object = ex;
                msg.Title  = String.Format(CommonUtil.ResourceValue("COM_MSG_DELETE_LIST_FAIL"), CommonUtil.ResourceValue("ADM_ROLE_LBL_ROLE").ToLower());
                //_logger.LogError(LoggingEvents.LogDb, "Delete list role fail");
                _actionLog.InsertActionLogDeleteItem("ASP_NET_ROLES", "Delete list role failed: " + ex.Message, null, null, "Error");
            }
            return(Json(msg));
        }