private Numberofhotel CreateModel(NumberofhotelBindingModel model, Numberofhotel numberofhotel, TravelAgencyContext context) { numberofhotel.Typeofnumberid = model.Typeofnumberid; numberofhotel.Viewnumber = model.Viewnumber; numberofhotel.Price = model.Price; if (model.Id.HasValue) { var roomsHotel = context.HotelNumberofhotel.Where(rec => rec.Numberofhotelid == model.Id.Value).ToList(); // удалили те, которых нет в модели context.HotelNumberofhotel.RemoveRange(roomsHotel.Where(rec => !model.HotelNumberofhotel.ContainsKey(rec.Hotelid)).ToList()); context.SaveChanges(); // обновили количество у существующих записей } // добавили новые foreach (var pc in model.HotelNumberofhotel) { context.HotelNumberofhotel.Add(new HotelNumberofhotel { Numberofhotelid = numberofhotel.Numberofhotelid, Hotelid = pc.Key }); try { context.SaveChanges(); } catch (DbUpdateException e) { throw; } } return(numberofhotel); }
public void Update(NumberofhotelBindingModel model) { using (var context = new TravelAgencyContext()) { using (var transaction = context.Database.BeginTransaction()) { try { var element = context.Numberofhotel.FirstOrDefault(rec => rec.Numberofhotelid == model.Id); if (element == null) { throw new Exception("Номер не найден"); } CreateModel(model, element, context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
private Numberofhotel CreateModel(NumberofhotelBindingModel model, Numberofhotel numberofhotel) { numberofhotel.Typeofnumberid = model.Typeofnumberid; numberofhotel.Viewnumber = model.Viewnumber; numberofhotel.Price = model.Price; return(numberofhotel); }
public void CreateOrUpdate(NumberofhotelBindingModel model) { if (model.Id.HasValue) { _numberofhotelStorage.Update(model); } else { _numberofhotelStorage.Insert(model); } }
public void Delete(NumberofhotelBindingModel model) { var element = _numberofhotelStorage.GetElement(new NumberofhotelBindingModel { Id = model.Id }); if (element == null) { throw new Exception("Элемент не найден"); } _numberofhotelStorage.Delete(model); }
public List <NumberofhotelViewModel> Read(NumberofhotelBindingModel model) { if (model == null) { return(_numberofhotelStorage.GetFullList()); } if (model.Id.HasValue) { return(new List <NumberofhotelViewModel> { _numberofhotelStorage.GetElement(model) }); } return(_numberofhotelStorage.GetFilteredList(model)); }
public NumberofhotelViewModel GetElement(NumberofhotelBindingModel model) { if (model == null) { return(null); } using (var context = new TravelAgencyContext()) { var numberofhotel = context.Numberofhotel.Include(x => x.HotelNumberofhotel) .ThenInclude(x => x.Hotel).Include(x => x.Typeofnumber) .FirstOrDefault(rec => rec.Numberofhotelid == model.Id); return(numberofhotel != null?CreateModel(numberofhotel) : null); } }
public List <NumberofhotelViewModel> GetFilteredList(NumberofhotelBindingModel model) { if (model == null) { return(null); } using (var context = new TravelAgencyContext()) { return(context.Numberofhotel.Include(x => x.Typeofnumber).Include(x => x.HotelNumberofhotel) .ThenInclude(x => x.Hotel) .Where(rec => rec.Viewnumber == model.Viewnumber) .Select(CreateModel) .ToList()); } }
public void Delete(NumberofhotelBindingModel model) { using (var context = new TravelAgencyContext()) { Numberofhotel element = context.Numberofhotel.FirstOrDefault(rec => rec.Numberofhotelid == model.Id); if (element != null) { context.Numberofhotel.Remove(element); context.SaveChanges(); } else { throw new Exception("Номер не найден"); } } }
public void Insert(NumberofhotelBindingModel model) { using (var context = new TravelAgencyContext()) { using (var transaction = context.Database.BeginTransaction()) { try { Numberofhotel numberofhotel = CreateModel(model, new Numberofhotel()); context.Numberofhotel.Add(numberofhotel); context.SaveChanges(); numberofhotel = CreateModel(model, numberofhotel, context); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }