Exemple #1
0
 public void Insert(FavoritesBindingModel model)
 {
     using (var context = new AdBoardDatabase())
     {
         context.Favorites.Add(CreateModel(model, new Favorites()));
         context.SaveChanges();
     }
 }
Exemple #2
0
        public void Delete(FavoritesBindingModel model)
        {
            var element = _favoriteStorage.GetElement(new FavoritesBindingModel
            {
                Id = model.Id
            });

            if (element == null)
            {
                throw new Exception("Избранное не найдено");
            }
            _favoriteStorage.Delete(model);
        }
Exemple #3
0
 public void Update(FavoritesBindingModel model)
 {
     using (var context = new AdBoardDatabase())
     {
         var element = context.Favorites.FirstOrDefault(rec => rec.Id ==
                                                        model.Id);
         if (element == null)
         {
             throw new Exception("Сообщение не найдено");
         }
         CreateModel(model, element);
         context.SaveChanges();
     }
 }
Exemple #4
0
 public List <FavoritesViewModel> Read(FavoritesBindingModel model)
 {
     if (model == null)
     {
         return(_favoriteStorage.GetFullList());
     }
     if (model.Id.HasValue)
     {
         return(new List <FavoritesViewModel> {
             _favoriteStorage.GetElement(model)
         });
     }
     return(_favoriteStorage.GetFilteredList(model));
 }
Exemple #5
0
 public void Delete(FavoritesBindingModel model)
 {
     using (var context = new AdBoardDatabase())
     {
         Favorites element = context.Favorites.FirstOrDefault(rec => rec.Id ==
                                                              model.Id);
         if (element != null)
         {
             context.Favorites.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Сообщение не найдено");
         }
     }
 }
Exemple #6
0
        public void CreateOrUpdate(FavoritesBindingModel model)
        {
            var element = _favoriteStorage.GetElement(new FavoritesBindingModel
            {
                Link = model.Link
            });

            if (element != null && element.Id != model.Id)
            {
                throw new Exception("Уже есть такое избранное");
            }
            if (model.Id.HasValue)
            {
                _favoriteStorage.Update(model);
            }
            else
            {
                _favoriteStorage.Insert(model);
            }
        }
Exemple #7
0
 public FavoritesViewModel GetElement(FavoritesBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new AdBoardDatabase())
     {
         var message = context.Favorites
                       .FirstOrDefault(rec => rec.Id == model.Id);
         return(message != null ?
                new FavoritesViewModel
         {
             Id = rec.Id,
             Username = context.Favorites.Include(x => x.User).FirstOrDefault(x => x.UserId == rec.UserId).User.Username,
             UserId = rec.UserId,
             AdName = rec.AdName,
             Link = rec.Link
         } :
                null);
     }
 }
Exemple #8
0
 public List <FavoritesViewModel> GetFilteredList(FavoritesBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new AdBoardDatabase())
     {
         return(context.Favorites
                .Include(rec => rec.User)
                .Where(rec => rec.UserId == model.UserId)
                .Select(rec => new FavoritesViewModel
         {
             Id = rec.Id,
             Username = context.Favorites.Include(x => x.User).FirstOrDefault(x => x.UserId == rec.UserId).User.Username,
             UserId = rec.UserId,
             AdName = rec.AdName,
             Link = rec.Link
         })
                .ToList());
     }
 }
Exemple #9
0
 private Favorites CreateModel(FavoritesBindingModel model, Favorites favorite)
 {
     favorite.Link   = model.Link;
     favorite.AdName = model.AdName;
     return(favorite);
 }