public async Task <IActionResult> Create(ListingManagementDTO listingManagementDTO) { if (ModelState.IsValid) { try { var mapped = _mapper.Map <Listing>(listingManagementDTO); mapped.Image = new Photo() { ImageLink = _imageUploadWrapper.Upload(listingManagementDTO.File, _hostEnvironment) }; _repositoryWrapper.Listing.CreateListing(mapped); await _repositoryWrapper.SaveAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { _logger.LogError($"Error creating listing: {ex}"); } } ViewData["AddressId"] = await GetAddresses(null); return(View(nameof(Create), listingManagementDTO)); }
// GET: Admin/Listing/Details/5 public async Task <IActionResult> Details(int?id) { if (id == null || id <= 0) { _logger.LogError($"Invalid id past to {nameof(Details)}"); return(RedirectToAction(nameof(Index))); } ListingManagementDTO listing = _mapper.Map <ListingManagementDTO>(await _repositoryWrapper.Listing.GetListingByIdAsync(id)); return(View(nameof(Details), listing)); }
public EditShould() { fixture = new SetupFixture(); env = new Mock <IHostEnvironment>(); imageUploadWrapper = new Mock <IImageUploadWrapper>(); env.Setup(m => m.EnvironmentName).Returns("Hosting:UnitTestEnvironment"); sut = new ListingController(fixture.Logger.Object, fixture.repositoryWrapper.Object, fixture.mapper.Object, env.Object, imageUploadWrapper.Object); address = new Address() { FirstLine = "TestFirstLine", Number = "23", TownCity = "TestCity", Postcode = "Xf343xs", Id = 1 }; address2 = new Address() { FirstLine = "FirstLine", Number = "33", TownCity = "TestCity", Postcode = "Xf343xs", Id = 1 }; fixture.repositoryWrapper .Setup(x => x.Listing.GetListingByIdAsync(It.IsAny <int>())) .ReturnsAsync(It.IsAny <Listing>); fixture.repositoryWrapper.Setup(x => x.Address.GetAllAddressesNotInUseAsync()).ReturnsAsync(new List <Address>() { address }); fixture.repositoryWrapper.Setup(x => x.Address.GetAddressByIdAsync(It.IsAny <int>())).ReturnsAsync(address2); fixture.mapper.Setup(x => x.Map <ListingManagementDTO>(It.IsAny <Listing>())).Returns(new ListingManagementDTO() { Address = address }); imageUploadWrapper.Setup(x => x.Upload(It.IsAny <IFormFile>(), It.IsAny <IHostEnvironment>())) .Returns("imageurl"); fixture.mapper.Setup(x => x.Map <Listing>(It.IsAny <ListingManagementDTO>())).Returns(new Listing() { Id = 1 }); listingManagementDTO = new ListingManagementDTO() { Id = 1, Address = new Address() { Id = 1 }, File = new Mock <IFormFile>().Object }; }
public async Task <IActionResult> Edit(int id, ListingManagementDTO listingManagementDTO) { if (id != listingManagementDTO.Id) { _logger.LogError($"Id did not match for to {nameof(Edit)}. Expected:{id}, Actual{listingManagementDTO.Id}"); return(RedirectToAction(nameof(Index))); } if (ModelState.IsValid) { try { var mapped = _mapper.Map <Listing>(listingManagementDTO); if (listingManagementDTO.File != null) { mapped.Image = new Photo() { ImageLink = _imageUploadWrapper.Upload(listingManagementDTO.File, _hostEnvironment) }; } else { var listing = await _repositoryWrapper.Listing.GetListingByIdAsync(id); mapped.Image = listing.Image; mapped.ImageId = listing.ImageId; } mapped.Address = await _repositoryWrapper.Address.GetAddressByIdAsync(listingManagementDTO.AddressId); _repositoryWrapper.Listing.UpdateListing(mapped); await _repositoryWrapper.SaveAsync(); } catch (Exception ex) { _logger.LogError($"Error updating listing: {ex}"); } return(RedirectToAction(nameof(Index))); } ViewData["AddressId"] = await GetAddresses(listingManagementDTO.Address.Id); return(View(nameof(Edit), listingManagementDTO)); }