Пример #1
0
 public void Insert(MessagesBindingModel model)
 {
     using (var context = new AdBoardDatabase())
     {
         context.Messages.Add(CreateModel(model, new Messages()));
         context.SaveChanges();
     }
 }
Пример #2
0
 public void CreateOrUpdate(MessagesBindingModel model)
 {
     if (model.Id.HasValue)
     {
         _messageStorage.Update(model);
     }
     else
     {
         _messageStorage.Insert(model);
     }
 }
Пример #3
0
        public void Delete(MessagesBindingModel model)
        {
            var element = _messageStorage.GetElement(new MessagesBindingModel
            {
                Id = model.Id
            });

            if (element == null)
            {
                throw new Exception("Сообщение не найден");
            }
            _messageStorage.Delete(model);
        }
Пример #4
0
 public void Update(MessagesBindingModel model)
 {
     using (var context = new AdBoardDatabase())
     {
         var element = context.Messages.FirstOrDefault(rec => rec.Id ==
                                                       model.Id);
         if (element == null)
         {
             throw new Exception("Сообщение не найдено");
         }
         CreateModel(model, element);
         context.SaveChanges();
     }
 }
Пример #5
0
 public List <MessagesViewModel> Read(MessagesBindingModel model)
 {
     if (model == null)
     {
         return(_messageStorage.GetFullList());
     }
     if (model.Id.HasValue)
     {
         return(new List <MessagesViewModel> {
             _messageStorage.GetElement(model)
         });
     }
     return(_messageStorage.GetFilteredList(model));
 }
Пример #6
0
 public void Delete(MessagesBindingModel model)
 {
     using (var context = new AdBoardDatabase())
     {
         Messages element = context.Messages.FirstOrDefault(rec => rec.Id ==
                                                            model.Id);
         if (element != null)
         {
             context.Messages.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Сообщение не найдено");
         }
     }
 }
Пример #7
0
 public MessagesViewModel GetElement(MessagesBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new AdBoardDatabase())
     {
         var message = context.Messages
                       .FirstOrDefault(rec => rec.Id == model.Id);
         return(message != null ?
                new MessagesViewModel
         {
             Id = message.Id,
             Username = context.Messages.Include(x => x.User).FirstOrDefault(x => x.UserId == x.UserId).User.Username,
             UserId = message.UserId,
             Text = message.Text,
             Date = message.Date
         } :
                null);
     }
 }
Пример #8
0
 public List <MessagesViewModel> GetFilteredList(MessagesBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new AdBoardDatabase())
     {
         return(context.Messages
                .Include(rec => rec.User)
                .Where(rec => rec.UserId == model.UserId)
                .Select(rec => new MessagesViewModel
         {
             Id = rec.Id,
             Username = rec.User.Username,
             UserId = rec.UserId,
             Text = rec.Text,
             Date = rec.Date
         })
                .ToList());
     }
 }
Пример #9
0
 private Messages CreateModel(MessagesBindingModel model, Messages message)
 {
     message.Text = model.Text;
     message.Date = model.Date;
     return(message);
 }