public MyRemoteServices.addBookingUnitRequest GetBookingUnitRequest(AddNewBookingUnitViewModel anbuVM, long curentUserId, List <byte[]> myImages) { using (var ctx = new ApplicationDbContext()) { long cityId = (long)ctx.Cities.FirstOrDefault(x => x.Id.ToString() == anbuVM.CityId).MainServerId; long accCatId = (long)ctx.AccomodationCategories.FirstOrDefault(x => x.Id.ToString() == anbuVM.AccomodationCategoryId).MainServerId; long accTypeId = (long)ctx.AccomodationTypes.FirstOrDefault(x => x.Id.ToString() == anbuVM.AccomodationTypeId).MainServerId; long agentId = (long)ctx.Users.FirstOrDefault(x => x.Id == curentUserId).MainServerId; long[] bonusFeaturesIds; if (anbuVM.BonusFeatures != null) { int bfCnt = anbuVM.BonusFeatures.Count; bonusFeaturesIds = new long[bfCnt]; for (int i = 0; i < bfCnt; i++) { var bonusFeature = anbuVM.BonusFeatures[i]; if (bonusFeature.IsSelected) { long currentId = bonusFeature.Id; long bfId = (long)ctx.BonusFeatures.FirstOrDefault(x => x.Id == currentId).MainServerId; bonusFeaturesIds[i] = bfId; } } } else { bonusFeaturesIds = new long[1]; bonusFeaturesIds[0] = -1; } int imgCnt = anbuVM.Images.Length; MyRemoteServices.hMapStringStringElement[] base64ImagesList = new MyRemoteServices.hMapStringStringElement[imgCnt]; for (int i = 0; i < imgCnt; i++) { var file = anbuVM.Images[i]; string imgName = file.FileName; string thePictureDataAsString = Convert.ToBase64String(myImages[i]); base64ImagesList[i] = new MyRemoteServices.hMapStringStringElement { key = imgName, value = thePictureDataAsString }; } MyRemoteServices.BookingUnit unit = new MyRemoteServices.BookingUnit { cityMainServerId = cityId, accCategoryMainServerId = accCatId, accTypeMainServerId = accTypeId, address = anbuVM.Address, agentMainServerId = agentId, bonusFeaturesMainServerIds = bonusFeaturesIds, description = anbuVM.Description, name = anbuVM.Name, peopleNo = anbuVM.PeopleNo, base64ImagesList = base64ImagesList }; MyRemoteServices.addBookingUnitRequest retObj = new MyRemoteServices.addBookingUnitRequest { bookingUnit = unit }; return(retObj); } }
public ActionResult AddBookingUnit(AddNewBookingUnitViewModel anbuVM) { if (!Request.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } else { if (ModelState.IsValid) { DTOHelper dtoHlp = new DTOHelper(); bool imgValidation = dtoHlp.ValidateImageTypes(anbuVM.Images); if (imgValidation) { List <byte[]> imagesCopy = new List <byte[]>(); foreach (var img in anbuVM.Images) { MemoryStream target = new MemoryStream(); img.InputStream.CopyTo(target); byte[] data = target.ToArray(); imagesCopy.Add(data); } long curentUserId = User.Identity.GetUserId <long>(); //send a request on the main server and await for the response MyRemoteServices.AgentEndpointPortClient aepc = new MyRemoteServices.AgentEndpointPortClient(); MyRemoteServices.addBookingUnitRequest abuRequest = dtoHlp.GetBookingUnitRequest(anbuVM, curentUserId, imagesCopy); MyRemoteServices.addBookingUnitResponse abuResponse = aepc.addBookingUnit(abuRequest); if (abuResponse.responseWrapper.success) { //save localy using (var ctx = new ApplicationDbContext()) { //create a new booking unit with all the prerequisits City myCity = ctx.Cities.FirstOrDefault(x => x.Id.ToString() == anbuVM.CityId); ApplicationUser agentUsr = ctx.Users.FirstOrDefault(x => x.Id == curentUserId); AccomodationType myAccType = ctx.AccomodationTypes.FirstOrDefault(x => x.Id.ToString() == anbuVM.AccomodationTypeId); AccomodationCategory myAccCat = ctx.AccomodationCategories.FirstOrDefault(x => x.Id.ToString() == anbuVM.AccomodationCategoryId); ICollection <BonusFeatures> myBonusFeatures = new List <BonusFeatures>(); if (anbuVM.BonusFeatures != null) { foreach (var bfvm in anbuVM.BonusFeatures) { if (bfvm.IsSelected) { var myBFeature = ctx.BonusFeatures.FirstOrDefault(x => x.Id == bfvm.Id); if (myBFeature != null) { myBonusFeatures.Add(myBFeature); } } } } BookingUnit newUnit = new BookingUnit { Name = anbuVM.Name, Address = anbuVM.Address, City = myCity, Description = anbuVM.Description, PeopleNo = anbuVM.PeopleNo, Agent = agentUsr, AccomodationType = myAccType, AccomodationCategory = myAccCat, BonusFeatures = myBonusFeatures, MainServerId = (long?)abuResponse.responseWrapper.responseBody }; //add the new unit to the DBContext ctx.BookingUnits.Add(newUnit); //get uploaded images and save them on the server var uploadDir = "~/uploadedImages"; var cnt = 0; foreach (var imgUpl in anbuVM.Images) { string newFileName = string.Concat(Path.GetFileNameWithoutExtension(imgUpl.FileName) , DateTime.Now.ToString("_yyyy_MM_dd_HH_mm_ss") , Path.GetExtension(imgUpl.FileName) ); var imagePath = Path.Combine(Server.MapPath(uploadDir), newFileName); System.IO.File.WriteAllBytes(imagePath, imagesCopy[cnt].ToArray()); var imageUrl = Path.Combine(uploadDir, newFileName); BookingUnitPicture newPicture = new BookingUnitPicture { BookingUnit = newUnit, Value = imageUrl }; ctx.Pictures.Add(newPicture); cnt++; } //save changes ctx.SaveChanges(); } } else { //some error happened, retry TempData["error"] = abuResponse.responseWrapper.message; return(RedirectToAction("AddNewBookingUnit", "Agent")); } TempData["success"] = "Successfully added a new booking unit"; return(RedirectToAction("AgentPage", "Agent")); } else { //image type exception TempData["error"] = "Wrong image type, please try again"; return(RedirectToAction("AddNewBookingUnit", "Agent")); } } else { //invalid VM exception TempData["error"] = "Some form atributes are incorrect"; return(RedirectToAction("AddNewBookingUnit", "Agent")); } } }