private void buttonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxCity.Text) || string.IsNullOrEmpty(textBoxHotelName.Text)) { MessageBox.Show("Заполните все поля", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { HotelBindingModel model = new HotelBindingModel { City = textBoxCity.Text, Hotelname = textBoxHotelName.Text }; if (Id.HasValue) { model.Id = Id; } _logicH.CreateOrUpdate(model); MessageBox.Show("Успешно", "Сохранено", MessageBoxButtons.OK, MessageBoxIcon.Information); Close(); } catch (Exception ex) { MessageBox.Show(ex.InnerException?.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
public void Insert(HotelBindingModel model) { using (var context = new TravelAgencyContext()) { context.Hotel.Add(CreateModel(model, new Hotel(), context)); context.SaveChanges(); } }
public void CreateOrUpdate(HotelBindingModel model) { if (model.Id.HasValue) { _hotelStorage.Update(model); } else { _hotelStorage.Insert(model); } }
public void Update(HotelBindingModel model) { using (var context = new TravelAgencyContext()) { var element = context.Hotel.FirstOrDefault(rec => rec.Hotelid == model.Id); if (element == null) { throw new Exception("Отель не найден"); } CreateModel(model, element, context); context.SaveChanges(); } }
public void Delete(HotelBindingModel model) { var element = _hotelStorage.GetElement(new HotelBindingModel { Id = model.Id }); if (element == null) { throw new Exception("Элемент не найден"); } _hotelStorage.Delete(model); }
public async Task <IEnumerable <Hotel> > AddHotel([FromBody][Required] HotelBindingModel hotel) { HotelInfo hotelInfo = new HotelInfo() { Name = hotel.Name, Phone = hotel.Phone, Address = hotel.Address, City = hotel.City, State = hotel.State, Zip = hotel.Zip, }; return(await hotelDAO.AddHotelAsync(hotelInfo)); }
public HotelViewModel GetElement(HotelBindingModel model) { if (model == null) { return(null); } using (var context = new TravelAgencyContext()) { var hotel = context.Hotel.Include(x => x.HotelNumberofhotel).Include(x => x.Contract) .FirstOrDefault(rec => rec.Hotelid == model.Id); return(hotel != null?CreateModel(hotel) : null); } }
public List <HotelViewModel> GetFilteredList(HotelBindingModel model) { if (model == null) { return(null); } using (var context = new TravelAgencyContext()) { return(context.Hotel.Include(x => x.HotelNumberofhotel).Include(x => x.Contract) .Where(rec => rec.Hotelname == model.Hotelname) .Select(CreateModel) .ToList()); } }
public List <HotelViewModel> Read(HotelBindingModel model) { if (model == null) { return(_hotelStorage.GetFullList()); } if (model.Id.HasValue) { return(new List <HotelViewModel> { _hotelStorage.GetElement(model) }); } return(_hotelStorage.GetFilteredList(model)); }
public void Delete(HotelBindingModel model) { using (var context = new TravelAgencyContext()) { Hotel element = context.Hotel.FirstOrDefault(rec => rec.Hotelid == model.Id); if (element != null) { context.Hotel.Remove(element); context.SaveChanges(); } else { throw new Exception("Отель не найден"); } } }
public async Task <HotelBindingModel> AddHotelAsync([FromBody][Required] HotelBindingModel hotel) { IEnumerable <Hotel> hotelsByCity = await UnitOfWork.Hotel.GetByCityAsync(hotel.City); if (hotelsByCity.Any(h => h.name == hotel.Name)) { throw new Exception("Hotel already exists"); } Hotel hotelInfo = new Hotel() { //id = Guid.NewGuid(), name = hotel.Name, phone = hotel.Phone, address = hotel.Address, city = hotel.City, state = hotel.State, zip = hotel.Zip, }; await UnitOfWork.Hotel.AddAsync(hotelInfo); hotel.HotelId = hotelInfo.id; return(hotel); }
private Hotel CreateModel(HotelBindingModel model, Hotel hotel, TravelAgencyContext database) { hotel.City = model.City; hotel.Hotelname = model.Hotelname; return(hotel); }