Beispiel #1
0
 public void AddState(string state)
 {
     _buotDb.State.Add(new State {
         Value = state
     });
     _buotDb.SaveChanges();
 }
Beispiel #2
0
        public void AddNotice(NoticeViewModel notice, string userName)
        {
            var categoriesIds = GetCategoriesIds(notice.CategoriesIds);

            var categories = _buotDb.Category
                             .Join(inner: categoriesIds,
                                   outerKeySelector: c => c.Id,
                                   innerKeySelector: id => id,
                                   resultSelector: (c, id) => c)
                             .OrderByDescending(c => c.Id)
                             .ToList();

            var categoriesToRemove = new List <Category>();

            categories.ForEach(cat =>
            {
                if (cat.ParentId > 0)
                {
                    var parent = categories.FirstOrDefault(c => c.Id == cat.ParentId);
                    if (categories.Contains(parent))
                    {
                        categoriesToRemove.Add(parent);
                    }
                }
            });

            categoriesToRemove.ForEach(c =>
            {
                categories.Remove(c);
            });

            _buotDb.Notice.Add(new Notice
            {
                Title       = notice.Title,
                ImageUrl    = notice.ImageUrl,
                Description = notice.Description,
                Created     = DateTime.Now,
                Category    = categories,
                Content     = notice.Content,
                UserName    = userName
            });
            _buotDb.SaveChanges();
        }