public Property CreateProperty(PropertyBindingModel model, string userId) { var property = this.Mapper.Map <Property>(model); property.NumberOfBathrooms = model.NumberOfBathrooms; property.UserId = userId; using (DbContext) { DbContext.Properties.Add(property); this.DbContext.SaveChanges(); if (model.SelectedFeatures.Any()) { foreach (var featureId in model.SelectedFeatures) { AddFeaturesToProperty(property, featureId); } } if (model.Images.Any()) { AddImagesToProperty(model, property); } this.DbContext.SaveChanges(); } return(property); }
public void DeletePost_Should_Return_ErrorPage_When_Model_IsNull() { // Arrange PropertyBindingModel model = null; // Act RedirectToActionResult result = (RedirectToActionResult)this.propertiesController.Delete(model); // Assert Assert.AreEqual(NotFoundPage, result.ActionName); }
public IActionResult Delete(PropertyBindingModel model) { try { this.propertyService.DeletePropertyById(model.Id); } catch (Exception) { return(RedirectToAction("NotFound", "Error", new { area = "" })); } return(RedirectToAction("Index", "Home")); }
private void AddImagesToProperty(PropertyBindingModel model, Property property) { foreach (var img in model.Images) { if (img.ContentType == "image/jpeg") { var imgName = img.FileName; var image = new Image() { Name = imgName, }; property.ImageNames.Add(image); } } this.DbContext.SaveChanges(); }
public IActionResult Create(PropertyBindingModel model) { if (!ModelState.IsValid) { return(RedirectToAction("Create", model)); } var userId = this.userManager.GetUserId(HttpContext.User); var property = this.propertyService.CreateProperty(model, userId); if (userId == null || property == null) { return(RedirectToAction("NotFound", "Error", new { area = "" })); } SaveImagesToRoot(model, property); return(RedirectToAction("Index", "Home")); }
private void SaveImagesToRoot(PropertyBindingModel model, Property property) { foreach (var image in model.Images.Take(3)) { if (image.ContentType == "image/jpeg") { var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images", $"{property.Id}"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } var fileStream = new FileStream(Path.Combine(filePath, image.FileName), FileMode.Create); image.CopyTo(fileStream); fileStream.Close(); } } }
public void EditProperty(int id, PropertyBindingModel model) { var property = this.GetPropertyById(id); property.Title = model.Title; property.Price = model.Price; property.NumberOfBathrooms = model.NumberOfBathrooms; property.NumberOfBedrooms = model.NumberOfBedrooms; property.Description = model.Description; property.IndoorArea = model.IndoorArea; property.Floor = model.Floor; property.Currency = model.Currency; property.AddressId = model.AddressId; property.PropertyType = model.PropertyType; //To avoid disposing the current Db connection if (model.SelectedFeatures.Any()) { property.Features.Clear(); this.DbContext.SaveChanges(); foreach (var featureId in model.SelectedFeatures) { AddFeaturesToProperty(property, featureId); } } if (model.Images.Any()) { DeletePropertyImages(property.Id); property.ImageNames.Clear(); DbContext.SaveChanges(); AddImagesToProperty(model, property); } this.DbContext.SaveChanges(); }
public IActionResult Create() { var loggedInUser = HttpContext.User.Identity.Name; var user = this.userService.GetUserByEmail(loggedInUser); if (user == null || loggedInUser == null) { return(RedirectToAction("NotFound", "Error", new { area = "" })); } if (!user.EmailConfirmed) { return(RedirectToAction("Index", "Identity/Account/Manage")); } var model = new PropertyBindingModel { Features = propertyService.GetAllFeatures().ToDictionary(x => x.Id, x => x.Name), Addresses = propertyService.GetAllAddresses().ToList() }; return(View(model)); }
public IActionResult Edit(PropertyBindingModel model) { if (model == null) { return(RedirectToAction("NotFound", "Error", new { area = "" })); } if (!this.ModelState.IsValid) { model.Features = propertyService.GetAllFeatures().ToDictionary(x => x.Id, x => x.Name); model.Addresses = propertyService.GetAllAddresses().ToList(); model.SelectedFeatures = propertyService.GetAllSelectedFeatures(model.Id).ToList(); return(View(model)); } this.propertyService.EditProperty(model.Id, model); var property = this.propertyService.GetPropertyById(model.Id); if (model.Images.Any()) { SaveImagesToRoot(model, property); } return(RedirectToAction("Details", new { id = model.Id })); }