public ServiceResultModel <HotelDefinitionVM> GetHotel(int id) { HotelDefinitionVM hotel = null; using (EFBookingContext context = new EFBookingContext()) { //context.HotelDefinitions.Include("HotelAttribute"); var hotelEntity = context.HotelDefinitions.FirstOrDefault(p => p.Id == id); hotel = hotelEntity.MapProperties <HotelDefinitionVM>(); hotel.HotelAttributes = hotelEntity.HotelAttributes.Select(p => p.MapProperties <HotelAttributeVM>()).ToList(); hotel.HotelRooms = hotelEntity.HotelRooms.Select(p => p.MapProperties <HotelRoomVM>()).ToList(); } return(ServiceResultModel <HotelDefinitionVM> .OK(hotel)); }
public JsonResult SaveHotelDefinition(HotelDefinitionVM model) { if (!ModelState.IsValid) { return(base.JSonModelStateHandle()); } ServiceResultModel <bool> serviceResult = _hotelService.SaveHotel(model); return(Json(base.UIResponse = new UIResponse { Message = string.Format("Operation Is Completed"), ResultType = serviceResult.ResultType, Data = serviceResult.Data })); }
public ServiceResultModel <bool> SaveHotel(HotelDefinitionVM model) { using (EFBookingContext context = new EFBookingContext()) { using (var transaction = context.Database.BeginTransaction()) { try { var deletedAttributes = context.HotelAttributes.Where(p => p.HotelId == model.Id); context.HotelAttributes.RemoveRange(deletedAttributes); HotelDefinition hotel = new HotelDefinition(); hotel = model.MapProperties <HotelDefinition>(); context.HotelDefinitions.Add(hotel); var savedHotel = context.SaveChanges(); foreach (var item in model.Attributes.Where(p => p.IsSelected)) { context.HotelAttributes.Add(new HotelAttribute { AttributeId = item.Id, HotelId = hotel.Id, IsActive = true, IsDeleted = false, CreateDate = DateTime.Now }); // throw new Exception(); } context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw; } } } return(ServiceResultModel <bool> .OK(true)); }
public ActionResult HotelDefinitionAddEdit(int?hotelId) { //HotelDefinitionVM hotelDefinition = new HotelDefinitionVM(); //if (hotelId.HasValue) // hotelDefinition = _hotelDefinitionService.GetHotel(hotelId.Value).Data; //var allCitiesData = _definitionService.GetCities().Data; //hotelDefinition.Cities = allCitiesData.Select(p => new BSelectListItem //{ // Disabled = false, // Text = p.Name, // Value = p.Id.ToString(), // Selected = hotelDefinition?.CityId > 0 ? p.Id == hotelDefinition.CityId : false //}); //hotelDefinition.Districts = allCitiesData.SelectMany(p => p.Districts) // .Select(p => new BSelectListItem // { // Text = p.Name, // Value = p.Id.ToString(), // ParentValue = p.CityId.ToString(), // Disabled = false, // Selected = hotelDefinition?.DistrictId > 0 // ? p.Id == hotelDefinition.DistrictId // : false // }); //hotelDefinition.HotelTypes = _hotelTypeService.GetAllHotelTypes(new HotelTypeFilter()).Data; HotelDefinitionVM hotelDefinition = new HotelDefinitionVM(); if (hotelId.HasValue) { hotelDefinition = _hotelService.GetHotel(hotelId.Value).Data; } ICacheManager cache = new MemCacheManager(); var data = cache.GetFromCache <List <CityDefinitionVM> >("Cities", () => _definitionService.GetCities().Data, null); var allCitiesData = _definitionService.GetCities().Data; hotelDefinition.Cities = allCitiesData.Select(p => new BSelectListItem { ParentValue = "0", Value = p.Id.ToString(), Text = p.Name, Selected = hotelId.HasValue ? hotelDefinition.CityId == p.Id : false }).AsEnumerable(); hotelDefinition.Districts = Enumerable.Empty <BSelectListItem>(); if (hotelId.HasValue && hotelDefinition?.CityId > 0 && hotelDefinition?.DistrictId > 0) { hotelDefinition.Districts = allCitiesData.SelectMany(p => p.Districts) .Where(p => p.CityId == hotelDefinition.CityId) .Select(c => new BSelectListItem { ParentValue = c.CityId.ToString(), Value = c.Id.ToString(), Text = c.Name, Selected = hotelId.HasValue ? hotelDefinition.DistrictId == c.Id : false }).AsEnumerable(); } hotelDefinition.HotelTypes = _hotelTypeService.GetAllHotelTypes(new HotelTypeFilter()).Data; List <CheckBoxListTemplate> attributes = _attributeService.GetAllAttributeList(new AttributeFilter { AttributeType = (int)AttributeType.Hotel }).Data .Select(p => new CheckBoxListTemplate { Id = p.Id, Text = p.Name, IsSelected = hotelDefinition.Id > 0 && hotelDefinition.HotelAttributes.Any(c => c.AttributeId == p.Id) }).ToList(); hotelDefinition.Attributes = attributes; return(View(hotelDefinition)); }