public ActionResult Edit(int id, [Bind("Id, Description, Address, PostCode, PricePerMonth, NoOfBedrooms, NoOfBaths, OffStreetParking, LaundryAvailable, Files")] ApartmentListing apartmentListing) { if (id != apartmentListing.Id) { return(NotFound()); } // Check if the model is Valid if (ModelState.IsValid) { // Add More Images to the listing // Check if Files were selected for the Listing if (apartmentListing.Files != null) { // Check if files are all images i.e. jpeg, jpg, png, bmp if (_listingService.AllFilesAreImages(apartmentListing.Files)) { foreach (var formFile in apartmentListing.Files) { // Create a random file name for the Profile Image string randomFileName = Guid.NewGuid().ToString(); // Get the extension of the filename string imageExtension = _listingService.GetImageExtension($"{formFile.FileName}"); // Create the URL to the image string imageUrl = $"/images/listings/{randomFileName}{imageExtension}"; // Create an Image listing object var image = new ListingImage { ApartmentListing = apartmentListing, ImageUrl = imageUrl }; // Add the Image to the Listing's Images apartmentListing.Images.Append(image); // Copy the Image into the wwwroot/images/listings folder _listingService.UploadListingImage(randomFileName, imageExtension, formFile); } // Update and Save the listing _listingService.UpdateListing(apartmentListing); _listingService.SaveChanges(); // Alert that Listing has been Updated TempData["Success"] = "Listing Updated Successfully"; return(RedirectToAction("Edit", new { Id = id })); } // Alert Upload Images TempData["Message"] = "Accepted Extensions are .jpeg .jpg .png .bmp"; return(View(apartmentListing)); } if (apartmentListing.Files == null) { _listingService.UpdateListing(apartmentListing); } } return(View(apartmentListing)); }
public void UpdateListing(ApartmentListing editListing) { _context.Update(editListing); }
public void AddListing(ApartmentListing newListing) { _context.Add(newListing); }
public async Task <ActionResult> Create([Bind("Id, Description, Address, PostCode, PricePerMonth, NoOfBedrooms, NoOfBaths, OffStreetParking, LaundryAvailable, Files")] ApartmentListing apartmentListing) { // Check if the model is Valid if (ModelState.IsValid) { // Check if Files were selected for the Listing if (apartmentListing.Files != null) { // Check if files are all images i.e. jpeg, jpg, png, bmp if (_listingService.AllFilesAreImages(apartmentListing.Files)) { // Create an empty IEnumerable of Listing Images IEnumerable <ListingImage> Images = Enumerable.Empty <ListingImage>(); foreach (var formFile in apartmentListing.Files) { // Create a random file name for the Profile Image string randomFileName = Guid.NewGuid().ToString(); // Get the extension of the filename string imageExtension = _listingService.GetImageExtension($"{formFile.FileName}"); // Create the URL to the image string imageUrl = $"/images/listings/{randomFileName}{imageExtension}"; // Create an Image listing object var image = new ListingImage { ApartmentListing = apartmentListing, ImageUrl = imageUrl }; // Add the Image to IEnumerator of Listing Images Images.Append(image); // Copy the Image into the wwwroot/images/listings folder _listingService.UploadListingImage(randomFileName, imageExtension, formFile); } // Assign Images apartmentListing.Images = Images; // Get and Assign User var currentUser = await _userManager.GetUserAsync(User); apartmentListing.User = currentUser; // Get and Assign Current time var currentTime = DateTime.Now; apartmentListing.DateCreated = currentTime; // Get the Category // Add and Save the listing _listingService.AddListing(apartmentListing); _listingService.SaveChanges(); // Alert that Appartment Listing has been created // Redirect to the index of Listing Controller return(RedirectToAction(nameof(Index))); } // Alert Upload Images TempData["Message"] = "Accepted Extensions are .jpeg .jpg .png .bmp"; return(View(apartmentListing)); } // Alert Upload Images TempData["Message"] = "Select Photo(s) for your Listing "; return(View(apartmentListing)); } return(View(apartmentListing)); }