Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 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;
             }
         }
     }
 }
Ejemplo n.º 3
0
 private Numberofhotel CreateModel(NumberofhotelBindingModel model, Numberofhotel numberofhotel)
 {
     numberofhotel.Typeofnumberid = model.Typeofnumberid;
     numberofhotel.Viewnumber     = model.Viewnumber;
     numberofhotel.Price          = model.Price;
     return(numberofhotel);
 }
Ejemplo n.º 4
0
 public void CreateOrUpdate(NumberofhotelBindingModel model)
 {
     if (model.Id.HasValue)
     {
         _numberofhotelStorage.Update(model);
     }
     else
     {
         _numberofhotelStorage.Insert(model);
     }
 }
Ejemplo n.º 5
0
        public void Delete(NumberofhotelBindingModel model)
        {
            var element = _numberofhotelStorage.GetElement(new NumberofhotelBindingModel
            {
                Id = model.Id
            });

            if (element == null)
            {
                throw new Exception("Элемент не найден");
            }
            _numberofhotelStorage.Delete(model);
        }
Ejemplo n.º 6
0
 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));
 }
Ejemplo n.º 7
0
 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);
     }
 }
Ejemplo n.º 8
0
 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());
     }
 }
Ejemplo n.º 9
0
 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("Номер не найден");
         }
     }
 }
Ejemplo n.º 10
0
 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;
             }
         }
     }
 }