Exemple #1
0
        public bool UpdateMenu(CmsMenuItemModel menu)
        {
            using (var db = new CMSdb(_context))
            {
                using (var tr = db.BeginTransaction())
                {
                    InsertLog(new LogModel
                    {
                        PageId   = menu.Id,
                        PageName = menu.Name,
                        Section  = LogModule.Menu,
                        Action   = LogAction.update
                    });
                    var q = db.core_menu.Where(w => w.id == menu.Id);
                    if (q.Any())
                    {
                        //влияние n_sort при смене/назначении группы
                        if (q.Single().f_parent != menu.Pid)
                        {
                            if (q.Single().f_parent != null)
                            {
                                db.core_menu.Where(w => w.f_parent == q.Single().f_parent&& w.n_sort > q.Single().n_sort)
                                .Set(p => p.n_sort, p => p.n_sort - 1)
                                .Update();
                            }
                        }

                        //определим новый парметр сортровки для текущего значения
                        int newsort = 0;
                        var q2      = db.core_menu.Where(w => w.f_parent == menu.Pid);
                        if (q2.Any())
                        {
                            newsort = (int)q2.Select(s => s.n_sort).Max();
                        }
                        newsort++;

                        bool result = db.core_menu
                                      .Where(w => w.id == menu.Id)
                                      .Set(s => s.c_title, menu.Name)
                                      .Set(s => s.c_alias, menu.Alias)
                                      .Set(s => s.c_class, menu.Icon)
                                      .Set(s => s.f_parent, menu.Pid)
                                      .Set(s => s.n_sort, (q.Single().f_parent == null)? q.Single().n_sort : newsort)
                                      .Update() > 0;
                        tr.Commit();
                        return(true);
                    }
                    return(false);
                }
            }
        }
Exemple #2
0
        public bool InsertMenu(CmsMenuItemModel menu)
        {
            using (var db = new CMSdb(_context))
            {
                using (var tr = db.BeginTransaction())
                {
                    InsertLog(new LogModel
                    {
                        PageId   = menu.Id,
                        PageName = menu.Name,
                        Section  = LogModule.Menu,
                        Action   = LogAction.insert
                    });

                    int sort = 1;
                    if (menu.Pid != null)
                    {
                        var q = db.core_menu.Where(w => w.f_parent == menu.Pid);
                        if (q.Any())
                        {
                            sort = (int)q.Select(s => s.n_sort).Max() + 1;
                        }
                    }

                    bool result = db.core_menu
                                  .Insert(
                        () => new core_menu {
                        id       = menu.Id,
                        c_title  = menu.Name,
                        c_alias  = menu.Alias,
                        c_class  = menu.Icon,
                        n_sort   = sort,
                        f_parent = (menu.Pid != null)? menu.Pid:null
                    }) > 0;
                    tr.Commit();
                    return(result);
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Единичный элемент cms menu
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public CmsMenuItemModel GetCmsMenuItem(Guid id)
 {
     using (var db = new CMSdb(_context))
     {
         var query = db.core_menu.Where(w => w.id == id);
         if (query.Any())
         {
             var s    = query.Single();
             var data = new CmsMenuItemModel
             {
                 Id    = s.id,
                 Alias = s.c_alias,
                 Icon  = s.c_class,
                 Name  = s.c_title
             };
             if (s.f_parent != null)
             {
                 data.Pid = (Guid)s.f_parent;
             }
             return(data);
         }
         return(null);
     }
 }