public void Insert(FavoritesBindingModel model) { using (var context = new AdBoardDatabase()) { context.Favorites.Add(CreateModel(model, new Favorites())); context.SaveChanges(); } }
public void Delete(FavoritesBindingModel model) { var element = _favoriteStorage.GetElement(new FavoritesBindingModel { Id = model.Id }); if (element == null) { throw new Exception("Избранное не найдено"); } _favoriteStorage.Delete(model); }
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(); } }
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)); }
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("Сообщение не найдено"); } } }
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); } }
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); } }
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()); } }
private Favorites CreateModel(FavoritesBindingModel model, Favorites favorite) { favorite.Link = model.Link; favorite.AdName = model.AdName; return(favorite); }