public void Insert(StaffBindingModel model)
 {
     using (var context = new HotelContext())
     {
         context.Staff.Add(CreateModel(model, new Staff(), context));
         context.SaveChanges();
     }
 }
 private Staff CreateModel(StaffBindingModel model, Staff staff, HotelContext database)
 {
     staff.Name        = model.Name;
     staff.Surname     = model.Surname;
     staff.Middlename  = model.Middlename;
     staff.Postid      = model.Postid;
     staff.Birthday    = model.Birthday;
     staff.Employement = model.Employement;
     return(staff);
 }
 public void CreateOrUpdate(StaffBindingModel model)
 {
     if (model.Id.HasValue)
     {
         _clientStorage.Update(model);
     }
     else
     {
         _clientStorage.Insert(model);
     }
 }
 public void Update(StaffBindingModel model)
 {
     using (var context = new HotelContext())
     {
         var element = context.Staff.FirstOrDefault(rec => rec.Id == model.Id);
         if (element == null)
         {
             throw new Exception("Клиент не найден");
         }
         CreateModel(model, element, context);
         context.SaveChanges();
     }
 }
        public void Delete(StaffBindingModel model)
        {
            var element = _clientStorage.GetElement(new StaffBindingModel
            {
                Id = model.Id
            });

            if (element == null)
            {
                throw new Exception("Элемент не найден");
            }
            _clientStorage.Delete(model);
        }
 public StaffViewModel GetElement(StaffBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new HotelContext())
     {
         var accounting = context.Staff.Include(x => x.Post)
                          .FirstOrDefault(rec => rec.Id == model.Id);
         return(accounting != null?CreateModel(accounting) :
                    null);
     }
 }
 public List <StaffViewModel> GetFilteredList(StaffBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new HotelContext())
     {
         return(context.Staff.Include(x => x.Post)
                .Where(rec => rec.Employement.Date == model.Employement.Date)
                .Select(CreateModel)
                .ToList());
     }
 }
 public List <StaffViewModel> Read(StaffBindingModel model)
 {
     if (model == null)
     {
         return(_clientStorage.GetFullList());
     }
     if (model.Id.HasValue)
     {
         return(new List <StaffViewModel> {
             _clientStorage.GetElement(model)
         });
     }
     return(_clientStorage.GetFilteredList(model));
 }
 public void Delete(StaffBindingModel model)
 {
     using (var context = new HotelContext())
     {
         Staff element = context.Staff.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Staff.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Клиент не найден");
         }
     }
 }
 private void buttonSave_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrEmpty(textBoxName.Text))
     {
         MessageBox.Show("Заполните Имя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     if (string.IsNullOrEmpty(textBoxSurname.Text))
     {
         MessageBox.Show("Заполните Фамилию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     if (string.IsNullOrEmpty(comboBox.Text))
     {
         MessageBox.Show("Заполните должность", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     if ((dateTimePickerEmployment.Value.Year - dateTimePickerBirthday.Value.Date.Year) < 17)
     {
         MessageBox.Show("Куда такой молодой", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     try
     {
         StaffBindingModel model = new StaffBindingModel
         {
             Name        = textBoxName.Text,
             Surname     = textBoxSurname.Text,
             Middlename  = textBoxMiddlename.Text,
             Postid      = Convert.ToInt32(comboBox.SelectedValue),
             Birthday    = dateTimePickerBirthday.Value,
             Employement = dateTimePickerEmployment.Value
         };
         if (Id.HasValue)
         {
             model.Id = Id.Value;
         }
         logic.CreateOrUpdate(model);
         Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message + '\n' + ex.InnerException?.Message, "Ошибка",
                         MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }