public async Task <ListingViewModel> GetDefaultViewModelEntry() { var viewModel = new ListingViewModel(); var heatingDbList = _listingRepo.GetAllHeating(); var propFeaturesDbList = _listingRepo.GetAllFeatures(); var customersDbList = _customerRepo.GetAll(); var agentsDbList = _userRepo.GetAllByRole(RoleNames.AGENT); await Task.WhenAll(heatingDbList, propFeaturesDbList, customersDbList, agentsDbList); viewModel.HeatingListToSelect = ListingViewHelperUtilites. GetHeatingTypesToListItems(heatingDbList.Result); viewModel.FeaturesListToSelect = ListingViewHelperUtilites. GetPropertyFeaturesToListItems(propFeaturesDbList.Result); viewModel.Agents = agentsDbList.Result; viewModel.Customers = customersDbList.Result; return(viewModel); }
public async Task <ActionResult> Edit(int?id) { try { if (id.HasValue) { var listingRes = await _listingRepo.Get(id.Value); if (listingRes != null) { model = await GetDefaultViewModelEntry(); model.ExistingListingImages = ListingViewHelperUtilites.SetImagesToListItems (listingRes.ImagesContent.ToList()); model.FeaturesListToSelect .SetFeaturesTypesToListItems(listingRes.Features); model.HeatingListToSelect .SetHeatingTypesToListItems(listingRes.Heating); model.InputModel = listingRes; return(View(model)); } else { return(HttpNotFound()); } } else { return(HttpNotFound()); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); return(View(model)); } }
public async Task <ActionResult> SearchListing (int?ViewingId, int?page, int?listingRequestedDetails, SearchBoxViewModel searchModel, string orderBy = null) { var model = ListingViewHelperUtilites.GetDefaultSearchParams(orderBy); if (searchModel != null) { searchModel.PriceRange = ListingViewHelperUtilites .GetPairPriceRange(searchModel.PriceRangeString); } var sortedList = await GetsortedListing (searchModel ?? new SearchBoxViewModel(), orderBy, page); if (ViewingId.HasValue) { model.ViewingID = ViewingId;//We know it is in edit mode } model.CurrentSort = orderBy; model.Listings = sortedList; model.SearchModel = searchModel; if (listingRequestedDetails.HasValue) { model.DisplayOnlyModel = await _listingRepo .Get(listingRequestedDetails.Value); } if (model.DisplayOnlyModel == null) { model.DisplayOnlyModel = new Listing(); } return(View(model)); }
public async Task <ActionResult> SearchListing (int?ViewingId, int?page, int?listingRequestedDetails, int?numOfBeds, int?numOfBaths, int?numOfStories, string municipality = null, string priceRangeString = null, string orderBy = null) { var model = ListingViewHelperUtilites.GetDefaultSearchParams(orderBy); model.SearchModel = ListingViewHelperUtilites.GetSearchBoxModel (numOfBeds, numOfBaths, numOfStories, municipality, priceRangeString); var sortedList = await GetsortedListing (model.SearchModel ?? new SearchBoxViewModel(), orderBy, page); if (ViewingId.HasValue) { model.ViewingID = ViewingId;//We know it is in editing viewing } model.CurrentSort = orderBy; model.Listings = sortedList; if (listingRequestedDetails.HasValue) { model.DisplayOnlyModel = await _listingRepo .Get(listingRequestedDetails.Value); } if (model.DisplayOnlyModel == null) { model.DisplayOnlyModel = new Listing(); } return(View(model)); }
public async Task <ActionResult> Edit(ListingViewModel model) { try { if (ModelState.IsValid) { var listingFromDb = await _listingRepo.Get(model.InputModel.ID); model.InputModel.ImagesContent = listingFromDb.ImagesContent; var modelToUpdate = model.InputModel; if (!_imageHelper.IsPostedFilesListEmpty(model.ListingImageFiles)) { if (_imageHelper.HasImageListExceededSizeLimit (model.ListingImageFiles)) { ModelState.AddModelError (string.Empty, "File upload limit exceeded (more than 7 files)"); model = await GetDefaultViewModelEntry(); model.InputModel = modelToUpdate; model.ExistingListingImages = ListingViewHelperUtilites.SetImagesToListItems (modelToUpdate.ImagesContent.ToList()); return(View(model)); } if (_imageHelper.isValidFileUpload(model.ListingImageFiles)) { var imagesList = model.ListingImageFiles; _imageHelper.SetModelImages (modelToUpdate, imagesList); } else { ModelState.AddModelError (string.Empty, "One or more File uploads was not valid"); model = await GetDefaultViewModelEntry(); model.InputModel = modelToUpdate; return(View(model)); } } _imageHelper.ArchiveCheckedImages (model.ExistingListingImages, modelToUpdate.ImagesContent.ToList()); if (modelToUpdate.ImagesContent .Count(i => !i.IsArchived) > 7) { ModelState.AddModelError (string.Empty, "A listing can't have more than 7 Images"); model = await GetDefaultViewModelEntry(); model.InputModel = modelToUpdate; var imagesContentList = modelToUpdate.ImagesContent. Where(i => i.ID > 0).ToList(); model.ExistingListingImages = ListingViewHelperUtilites.SetImagesToListItems (imagesContentList); return(View(model)); } var currentLoggedUserId = User.Identity.GetUserId(); var currentLoggedUserRes = await UserIdentityManager .GetUserById(currentLoggedUserId); var tmpModel = ListingViewHelperUtilites .MapHeatingAndFeaturesToListingModel(model); modelToUpdate.Heating = tmpModel.Heating; modelToUpdate.Features = tmpModel.Features; modelToUpdate.DateUpdated = DateTime.Now; modelToUpdate.UserUpdatorId = currentLoggedUserRes.registeredUser.ID; await _listingRepo.Update(modelToUpdate); model.InputModel = await _listingRepo.Get(modelToUpdate.ID); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); var inputModel = model.InputModel; model = await GetDefaultViewModelEntry(); model.InputModel = inputModel; model.FeaturesListToSelect. SetFeaturesTypesToListItems(model.InputModel.Features); model.HeatingListToSelect. SetHeatingTypesToListItems(model.InputModel.Heating); return(View(model)); } return(View("Details", model)); }
public async Task <ActionResult> Create(ListingViewModel model) { try { if (ModelState.IsValid) { var modelToInsert = model.InputModel; var currentLoggedUserId = User.Identity.GetUserId(); var currentLoggedUserRes = await UserIdentityManager.GetUserById(currentLoggedUserId); modelToInsert = ListingViewHelperUtilites .MapHeatingAndFeaturesToListingModel(model); modelToInsert.DateCreated = DateTime.Now; modelToInsert.DateUpdated = DateTime.Now; modelToInsert.UserCreatorId = currentLoggedUserRes.registeredUser.ID; modelToInsert.UserUpdatorId = currentLoggedUserRes.registeredUser.ID; modelToInsert.IsActive = true; if (!_imageHelper.IsPostedFilesListEmpty(model.ListingImageFiles)) { if (_imageHelper.HasImageListExceededSizeLimit(model.ListingImageFiles)) { ModelState.AddModelError (string.Empty, "File upload limit exceeded (more than 7 files)"); model = await GetDefaultViewModelEntry(); model.InputModel = modelToInsert; return(View(model)); } if (_imageHelper.isValidFileUpload(model.ListingImageFiles)) { var imagesList = model.ListingImageFiles; _imageHelper.SetModelImages (modelToInsert, imagesList); } else { ModelState.AddModelError (string.Empty, "One or more File uploads was not valid"); model = await GetDefaultViewModelEntry(); model.InputModel = modelToInsert; return(View(model)); } } int res = await _listingRepo.Create(modelToInsert); return(RedirectToAction("Details", new { id = modelToInsert.ID })); } else { var tmpModel = model.InputModel; model = await GetDefaultViewModelEntry(); model.InputModel = tmpModel; return(View(model)); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); model = await GetDefaultViewModelEntry(); return(View(model)); } }