//insert/update module api
        public IHttpActionResult Post(Module module)
        {
            try
            {
                if (module.ModuleID == 0)
                {
                    if (db.Modules.Any((x => x.ModuleName == module.ModuleName && x.IsDeleted == false)))
                    {
                        res["success"] = 0;
                        res["message"] = "Module already exist.";
                    }
                    else
                    {
                        //get Module Slug
                        module.PageSlug  = generalHelper.GetModuleSlug(module.ModuleName, Convert.ToInt32(module.ParentModuleID));
                        module.IsActive  = true;
                        module.IsDefault = false;
                        module.IsDeleted = false;
                        db.Modules.Add(module);
                        db.SaveChanges();
                        res["success"] = 1;
                        res["message"] = "Module created successfully.";
                    }
                }
                else
                {
                    if (db.Modules.Any((x => x.ModuleName == module.ModuleName && x.ModuleID != module.ModuleID && x.IsDeleted == false)))
                    {
                        res["success"] = 0;
                        res["message"] = "Module already exist.";
                    }
                    else
                    {
                        if (module.IsDefault == true)
                        {
                            res["success"] = 0;
                            res["message"] = "Module can not update.";
                        }
                        else
                        {
                            db.Entry(module).State = EntityState.Modified;
                            db.SaveChanges();
                            res["success"] = 1;
                            res["message"] = "Module updated Successfully.";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                res["success"] = 0;
                res["message"] = ex.Message.ToString();
            }

            return(Ok(res));
        }
Exemple #2
0
        public JsonResult ChangeNodePosition(int currentNode, int?parentNode, int position)
        {
            using (BatiFrenDBEntities db = new BatiFrenDBEntities())
            {
                var location         = db.Menus.First(x => x.MenuID == currentNode);
                var newSiblingsBelow = db.Menus.Where(l => l.ParentID == parentNode && l.OrderNumber >= position);
                foreach (var sibling in newSiblingsBelow)
                {
                    sibling.OrderNumber = sibling.OrderNumber + 1;
                }

                var oldSiblingsBelow = db.Menus.Where(l => l.ParentID == parentNode && l.OrderNumber > location.OrderNumber);
                foreach (var sibling in oldSiblingsBelow)
                {
                    sibling.OrderNumber = sibling.OrderNumber - 1;
                }

                location.ParentID    = parentNode;
                location.OrderNumber = position;
                db.SaveChanges();
            }
            return(this.Json(true));
        }
        //Roles insert/update api
        public IHttpActionResult Post(Roles role)
        {
            try
            {
                if (role.RoleID == 0)
                {
                    if (db.Roles.Any((x => x.RoleName == role.RoleName && x.IsDeleted == false)))
                    {
                        res["success"] = 0;
                        res["message"] = "Role already exist.";
                    }
                    else
                    {
                        Role objrole = new Role();
                        objrole.RoleID      = role.RoleID;
                        objrole.Description = role.Description;
                        objrole.DisplayName = role.DisplayName;
                        objrole.RoleName    = role.RoleName;
                        objrole.IsDeleted   = false;
                        objrole.InsertDate  = DateTime.Now;
                        objrole.IsDeleted   = false;

                        db.Roles.Add(objrole);
                        db.SaveChanges();

                        for (var i = 0; i < role.UserPermission.Count(); i++)
                        {
                            role.UserPermission[i].RoleID = objrole.RoleID;
                            db.UserPermissions.Add(role.UserPermission[i]);
                            db.SaveChanges();
                        }

                        res["success"] = 1;
                        res["message"] = "Role created successfully.";
                    }
                }
                else
                {
                    if (db.Roles.Any((x => x.RoleName == role.RoleName && x.RoleID != role.RoleID && x.IsDeleted == false)))
                    {
                        res["success"] = 0;
                        res["message"] = "Role already exist.";
                    }
                    else
                    {
                        Role objrole = db.Roles.FirstOrDefault(x => x.RoleID == role.RoleID);
                        if (objrole.IsDefault == false)
                        {
                            res["success"] = 0;
                            res["message"] = "Module can not update.";
                        }
                        else
                        {
                            objrole.RoleID           = role.RoleID;
                            objrole.Description      = role.Description;
                            objrole.DisplayName      = role.DisplayName;
                            objrole.RoleName         = role.RoleName;
                            objrole.IsDeleted        = false;
                            objrole.LastModifiedDate = DateTime.Now;
                            objrole.IsDeleted        = false;

                            db.Entry(objrole).State = EntityState.Modified;
                            db.SaveChanges();

                            //Delete all User Permission
                            List <Entities.UserPermission> listPermission = db.UserPermissions.Where(x => x.RoleID == role.RoleID).ToList();
                            foreach (var objpermission in listPermission)
                            {
                                db.UserPermissions.Remove(objpermission);
                            }
                            db.SaveChanges();

                            //Create New Permission of users
                            for (var i = 0; i < role.UserPermission.Count(); i++)
                            {
                                role.UserPermission[i].RoleID = objrole.RoleID;
                                db.UserPermissions.Add(role.UserPermission[i]);
                                db.SaveChanges();
                            }

                            res["success"] = 1;
                            res["message"] = "Role updated Successfully.";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                res["success"] = 0;
                res["message"] = ex.Message.ToString();
            }

            return(Ok(res));
        }