public async Task <IHttpActionResult> AddPermission(tbl_Permission item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            CRUDOperation  operation = new CRUDOperation();
            tbl_Permission dbitem    = operation.AddPermission(item);

            return(Ok(dbitem));
        }
        public async Task <IHttpActionResult> UpdatePermission(tbl_Permission item)
        {
            CRUDOperation operation = new CRUDOperation();

            if (item == null)
            {
                return(NotFound());
            }
            else
            {
                var dbitem = operation.UpdatePermission(item);
                return(Ok(dbitem));
            }
        }
Exemple #3
0
        public ActionResult Edit(string id, tbl_Role tbl_Role, string selectedItems)
        {
            using (var _context = new Entities())
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    List <TreeViewNode> parentItems = new List <TreeViewNode>();
                    var vm = new RolesViewModel();
                    try
                    {
                        if (id != null)
                        {
                            _context.tbl_RolePermission.RemoveRange(_context.tbl_RolePermission.Where(x => x.RoleId == id));
                            _context.SaveChanges();
                        }
                        if (selectedItems != "[]")
                        {
                            List <TreeViewNode> items           = (new JavaScriptSerializer()).Deserialize <List <TreeViewNode> >(selectedItems);
                            List <string>       newListOfParent = new List <string>();
                            foreach (var temp in items.Select(x => x.parents))
                            {
                                newListOfParent = newListOfParent.Concat(temp).ToList();
                            }
                            newListOfParent = newListOfParent.Distinct().ToList();

                            foreach (var selected in items)
                            {
                                if (selected.parent == "#")
                                {
                                    selected.parent = "0";
                                }

                                if (!_context.tbl_RolePermission.Where(x => x.RoleId == tbl_Role.RoleId).Any(x => x.PermissionId == selected.id))
                                {
                                    var model = new tbl_RolePermission
                                    {
                                        PermissionId = selected.id,
                                        RoleId       = tbl_Role.RoleId,
                                    };
                                    _context.tbl_RolePermission.Add(model);
                                }
                            }

                            foreach (var selected in newListOfParent)
                            {
                                if (!items.Where(x => x.id == selected).Any() && selected != "#")
                                {
                                    tbl_Permission permission = _context.tbl_Permission.Where(x => x.PermissionId == selected).FirstOrDefault();

                                    if (!_context.tbl_RolePermission.Where(x => x.RoleId == tbl_Role.RoleId).Any(x => x.PermissionId == selected))
                                    {
                                        var model = new tbl_RolePermission
                                        {
                                            PermissionId = selected,
                                            RoleId       = tbl_Role.RoleId,
                                        };
                                        _context.tbl_RolePermission.Add(model);
                                    }
                                }
                            }

                            _context.SaveChanges();
                        }


                        //Serialize to JSON string.
                        // Alert("Data Saved Sucessfully!!!", NotificationType.success);
                        transaction.Commit();
                        return(RedirectToAction("ViewRoles"));
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        //   Exception(ex);
                        //  Alert("Their is something went wrong!!!", NotificationType.error);
                        return(RedirectToAction("ViewRoles"));
                    }
                }
            }
        }