public void TestAddNullObject() { // Arrange var options = BuildDbContextOptions(); ListingInputModel testObject = null; // Act using (var context = new ApplicationDbContext(options)) { context.Database.EnsureCreated(); var repository = new ListingRepository(context); var service = new ListingService(repository, null, null, null, null, null); service.AddListingAsync(testObject); } // Assert using (var context = new ApplicationDbContext(options)) { var result = context.Listing.ToList(); Assert.Equal(6, result.Count); context.Database.EnsureDeleted(); } }
public async Task TestGetEditIdValid() { // Arrange var inputModel = new ListingInputModel { Id = 666, Description = "test" }; var mockService = new Mock <IListingService>(); mockService .Setup(x => x.GetListingInputModelByIdAsync(It.IsAny <int>())) .ReturnsAsync(inputModel) .Verifiable(); var controller = new ListingController(mockService.Object); // Act var result = await controller.Edit(1); // Assert var viewResult = Assert.IsAssignableFrom <ViewResult>(result); var modelResult = Assert.IsAssignableFrom <ListingInputModel>(viewResult.Model); Assert.Equal("test", modelResult.Description); mockService.Verify(x => x.GetListingInputModelByIdAsync(It.IsAny <int>()), Times.Once); }
public async Task <IActionResult> Edit(int id, ListingInputModel inputModel) { if (inputModel == null) { return(BadRequest()); } if (id != inputModel.Id) { return(NotFound()); } if (ModelState.IsValid) { var editSuccess = await _listingService.UpdateListingAsync(inputModel); if (editSuccess) { return(RedirectToAction("Index", "Home")); } TempData["EditError"] = "There was a problem updating the information. Please ensure the data entered is valid, and try again."; return(View(inputModel)); } return(View(inputModel)); }
public async Task <bool> UpdateListingAsync(ListingInputModel source) { if (source == null) { return(false); } try { var entity = await GetListingByIdAsync(source.Id); if (entity == null) { return(false); } await MapListingValues(source, entity); _carService.MapCarValues(source.Car, entity.Car); _repairJobService.MapRepairJobValues(source.RepairJob, entity.RepairJob); _mediaService.UpdateMediaCollection(source.ImgNames, entity); entity.DateLastUpdated = DateTime.Today; _listingRepository.UpdateListing(entity); return(true); } catch (Exception e) { Console.WriteLine(e); return(false); } }
public async Task <IActionResult> Create(ListingInputModel inputModel) { if (inputModel == null) { return(RedirectToAction(nameof(Create), "Listing")); } if (!ModelState.IsValid) { return(View(inputModel)); } await _listingService.AddListingAsync(inputModel); return(RedirectToAction(nameof(Index), "Home")); }
public ActionResult Create(ListingInputModel model) { //code for creating if (model != null && this.ModelState.IsValid) { var l = new Listing() { Address = model.StreetAddress, forSale = model.ForSale }; db.Listings.Add(l); this.db.SaveChanges(); return(this.RedirectToAction("Conf")); } return(this.View(model)); }
private async Task MapListingValues(ListingInputModel inputModel, Listing entity) { if (entity == null) { throw new Exception("Listing entity not found"); } var status = await _statusService.GetStatusByIdAsync(inputModel.Status); if (status == null) { throw new Exception("Status entity not found."); } var listingEntityEntry = _listingRepository.GetListingEntityEntry(entity); listingEntityEntry.CurrentValues.SetValues(inputModel); entity.Status = status; }
public async Task TestGetEditInputModelNull() { // Arrange ListingInputModel inputModel = null; var mockService = new Mock <IListingService>(); mockService .Setup(x => x.GetListingInputModelByIdAsync(It.IsAny <int>())) .ReturnsAsync(inputModel) .Verifiable(); var controller = new ListingController(mockService.Object); // Act var result = await controller.Edit(1); // Assert Assert.IsAssignableFrom <NotFoundResult>(result); mockService.Verify(x => x.GetListingInputModelByIdAsync(It.IsAny <int>()), Times.Once); }
public async Task AddListingAsync(ListingInputModel inputModel) { if (inputModel != null) { var listing = new Listing { Car = new Car(), RepairJob = new RepairJob() }; _listingRepository.TrackListing(listing); await MapListingValues(inputModel, listing); _carService.MapCarValues(inputModel.Car, listing.Car); _repairJobService.MapRepairJobValues(inputModel.RepairJob, listing.RepairJob); _mediaService.UpdateMediaCollection(inputModel.ImgNames, listing); listing.DateCreated = DateTime.Today; listing.DateLastUpdated = DateTime.Today; _listingRepository.AddListing(listing); } }
public async Task TestUpdateListingAsyncSourceValid() { // Arrange var options = BuildDbContextOptions(); var inputModel = new ListingInputModel { Id = 1, Description = "updated listing" }; var mockRepairJobService = new Mock <IRepairJobService>(); mockRepairJobService .Setup(x => x.MapRepairJobValues( It.IsAny <RepairJobInputModel>(), It.IsAny <RepairJob>())) .Verifiable(); var mockCarService = new Mock <ICarService>(); mockCarService .Setup(x => x.MapCarValues( It.IsAny <CarInputModel>(), It.IsAny <Car>())) .Verifiable(); var mockMediaService = new Mock <IMediaService>(); mockMediaService .Setup(x => x.UpdateMediaCollection( It.IsAny <IEnumerable <string> >(), It.IsAny <Listing>())) .Verifiable(); var mockStatusService = new Mock <IStatusService>(); bool result; await using (var context = new ApplicationDbContext(options)) { context.Database.EnsureCreated(); var status = context.Status.FirstOrDefault(s => s.Id == 1); mockStatusService .Setup(x => x.GetStatusByIdAsync(It.IsAny <int>())) .ReturnsAsync(status) .Verifiable(); var repository = new ListingRepository(context); var service = new ListingService(repository, mockRepairJobService.Object, mockCarService.Object, mockStatusService.Object, mockMediaService.Object, _mapper); // Act result = await service.UpdateListingAsync(inputModel); } await using (var context = new ApplicationDbContext(options)) { // Assert var listings = context.Listing.ToList(); Assert.True(result); Assert.Equal("updated listing", listings.FirstOrDefault(l => l.Id == 1).Description); mockRepairJobService .Verify(x => x.MapRepairJobValues( It.IsAny <RepairJobInputModel>(), It.IsAny <RepairJob>()), Times.Once); mockCarService .Verify(x => x.MapCarValues( It.IsAny <CarInputModel>(), It.IsAny <Car>()), Times.Once); mockStatusService .Verify(x => x.GetStatusByIdAsync(It.IsAny <int>()), Times.Once); mockMediaService .Verify(x => x.UpdateMediaCollection( It.IsAny <IEnumerable <string> >(), It.IsAny <Listing>()), Times.Once); context.Database.EnsureDeleted(); } }
public async Task TestUpdateListingAsyncSourceNull() { // Arrange var options = BuildDbContextOptions(); ListingInputModel testInputModel = null; var mockRepairJobService = new Mock <IRepairJobService>(); mockRepairJobService .Setup(x => x.MapRepairJobValues( It.IsAny <RepairJobInputModel>(), It.IsAny <RepairJob>())) .Verifiable(); var mockCarService = new Mock <ICarService>(); mockCarService .Setup(x => x.MapCarValues( It.IsAny <CarInputModel>(), It.IsAny <Car>())) .Verifiable(); var mockMediaService = new Mock <IMediaService>(); mockMediaService .Setup(x => x.UpdateMediaCollection( It.IsAny <IEnumerable <string> >(), It.IsAny <Listing>())) .Verifiable(); var mockStatusService = new Mock <IStatusService>(); using (var context = new ApplicationDbContext(options)) { context.Database.EnsureCreated(); var status = context.Status.FirstOrDefault(s => s.Id == 1); mockStatusService .Setup(x => x.GetStatusByIdAsync(It.IsAny <int>())) .ReturnsAsync(status) .Verifiable(); var repository = new ListingRepository(context); var service = new ListingService(repository, mockRepairJobService.Object, mockCarService.Object, mockStatusService.Object, mockMediaService.Object, _mapper); Assert.Equal(6, context.Listing.Count()); // Act await service.AddListingAsync(testInputModel); } await using (var context = new ApplicationDbContext(options)) { // Assert var listings = context.Listing.ToList(); Assert.Equal(6, listings.Count); mockRepairJobService .Verify(x => x.MapRepairJobValues( It.IsAny <RepairJobInputModel>(), It.IsAny <RepairJob>()), Times.Never); mockCarService .Verify(x => x.MapCarValues( It.IsAny <CarInputModel>(), It.IsAny <Car>()), Times.Never); mockStatusService .Verify(x => x.GetStatusByIdAsync(It.IsAny <int>()), Times.Never); mockMediaService .Verify(x => x.UpdateMediaCollection( It.IsAny <IEnumerable <string> >(), It.IsAny <Listing>()), Times.Never); context.Database.EnsureDeleted(); } }