public ActionResult BeAHost() { if ((string)Session["userCode"] == "tourist") { int TouristId = (int)Session["TouristId"]; var d = (from c in entity.Tourists where c.TouristId == TouristId select c).FirstOrDefault(); BeAHostViewModel aovm = new BeAHostViewModel(); if (d.TouristAddress != null) { aovm.HostAddress = d.TouristAddress; } aovm.HostPhnNo = d.TouristPhnNo; aovm.TouristId = Convert.ToInt32(d.TouristId); aovm.CountryList = new SelectList(entity.Countries, "CountryId", "CountryName"); aovm.HostTypeList = new SelectList(entity.HostTypes, "HostTypeId", "HostTypeName"); return(View(aovm)); } else { Session["message"] = "please log in to add your offer."; return(RedirectToAction("Index", "Home")); } }
public ActionResult BeAHost(BeAHostViewModel aovm, HttpPostedFileBase AddPicture) { if (ModelState.IsValid) { try { bool result = ol.CheckTouristIdInHost(aovm.TouristId); if (result == true) { result = ol.CheckAddressNTypeId(aovm.TouristId, aovm.HostAddress, aovm.HostTypeId); if (result == true) { result = ol.CheckOverlap(aovm.HostStartTime, aovm.HostStopTime, aovm.TouristId, aovm.HostAddress, aovm.HostTypeId); if (result == true) { TempData["Success"] = "there is a time overlap"; aovm.CountryList = new SelectList(entity.Countries, "CountryId", "CountryName"); aovm.HostTypeList = new SelectList(entity.HostTypes, "HostTypeId", "HostTypeName"); return(View(aovm)); } } } Host offer = new Host(); offer.TouristId = aovm.TouristId; var d = (from c in entity.Tourists where c.TouristId == aovm.TouristId select c).FirstOrDefault(); if (aovm.CountryId == null) { aovm.CountryId = Convert.ToInt32(d.CountryId); } if (aovm.CityId == null) { aovm.CityId = Convert.ToInt32(d.CityId); } if (aovm.HostAddress == "Not given yet") { TempData["Success"] = "Address must be given."; aovm.CountryList = new SelectList(entity.Countries, "CountryId", "CountryName"); aovm.HostTypeList = new SelectList(entity.HostTypes, "HostTypeId", "HostTypeName"); return(View(aovm)); } offer.HostingCountryId = aovm.CountryId; offer.HostingCityId = aovm.CityId; offer.HostTypeId = aovm.HostTypeId; offer.Rent = aovm.HostPrice; offer.HostingPlaceAddress = aovm.HostAddress; offer.HostingPhnNo = aovm.HostPhnNo; offer.HostingStartTime = Convert.ToDateTime(aovm.HostStartTime); offer.HostingStopTime = Convert.ToDateTime(aovm.HostStopTime); if (aovm.HostDetail != null) { offer.HostingDetail = aovm.HostDetail; } else { offer.HostingDetail = "Not given yet."; } offer.HostingStatus = true; offer.HostAdminsPermit = true; offer.HostDateOfAccountCreation = DateTime.Now; entity.Hosts.Add(offer); Tourist tour = (from c in entity.Tourists where c.TouristId == aovm.TouristId select c).FirstOrDefault(); tour.TouristsHostStatus = true; if (entity.SaveChanges() > 0) { var p = (from c in entity.Hosts where c.TouristId == aovm.TouristId && c.HostingCountryId == aovm.CountryId && c.HostingCityId == aovm.CityId && c.HostingPlaceAddress == aovm.HostAddress && c.HostTypeId == aovm.HostTypeId && c.HostingStartTime == aovm.HostStartTime select new { c.HostId, c.HostingCountryId, c.HostingCityId }).FirstOrDefault(); if (AddPicture != null) { HostAlbum pa = new HostAlbum(); pa.HostAlbumName = "Profile Picture"; pa.HostId = p.HostId; pa.AlbumTypeId = 1; pa.CityId = p.HostingCityId; pa.CountryId = p.HostingCountryId; pa.HostAlbumAdminPermit = true; pa.HostAlbumPrivacy = true; pa.TouristId = aovm.TouristId; pa.HostAlbumDateOfAdded = DateTime.Now; entity.HostAlbums.Add(pa); entity.SaveChanges(); HostPicture pp = new HostPicture(); pp.HostPictureData = new byte[AddPicture.ContentLength]; AddPicture.InputStream.Read(pp.HostPictureData, 0, AddPicture.ContentLength); pp.HostId = p.HostId; var albumId = (from c in entity.HostAlbums where c.HostId == p.HostId && c.AlbumTypeId == 1 select c.HostAlbumId).Single(); int AlbumId = Convert.ToInt32(albumId); pp.HostAlbumId = AlbumId; pp.AlbumTypeId = 1; pp.HostPictureAdminPermit = true; pp.CountryId = p.HostingCountryId; pp.CityId = Convert.ToInt32(p.HostingCityId); pp.TouristId = aovm.TouristId; pp.HostPicturePrivacy = true; pp.HostPictureDateOfAdded = DateTime.Now; entity.HostPictures.Add(pp); if (entity.SaveChanges() > 0) { Host o = (from c in entity.Hosts where c.HostId == p.HostId select c).FirstOrDefault(); int pictureId = (from c in entity.HostPictures where c.HostId == p.HostId && c.AlbumTypeId == 1 orderby c.HostPictureId descending select c.HostPictureId).First(); o.HostProfilePicId = pictureId; entity.SaveChanges(); } } ModelState.Clear(); TempData["Success"] = "Saved"; int TouristId = (int)Session["TouristId"]; d = (from c in entity.Tourists where c.TouristId == TouristId select c).FirstOrDefault(); if (d.TouristAddress != null) { aovm.HostAddress = d.TouristAddress; } aovm.HostPhnNo = d.TouristPhnNo; aovm.TouristId = Convert.ToInt32(d.TouristId); aovm.CountryList = new SelectList(entity.Countries, "CountryId", "CountryName"); aovm.HostTypeList = new SelectList(entity.HostTypes, "HostTypeId", "HostTypeName"); return(View(aovm)); } else { TempData["Success"] = "Error not Saved"; aovm.CountryList = new SelectList(entity.Countries, "CountryId", "CountryName"); aovm.HostTypeList = new SelectList(entity.HostTypes, "HostTypeId", "HostTypeName"); return(View(aovm)); } } catch (Exception ex) { return(View("Error", new HandleErrorInfo(ex, "Host", "BeAHost"))); } } else { aovm.CountryList = new SelectList(entity.Countries, "CountryId", "CountryName"); aovm.HostTypeList = new SelectList(entity.HostTypes, "HostTypeId", "HostTypeName"); return(View(aovm)); } }