public async Task <bool> Create(AuctionHouseCreateInputModel inputModel) { City cityFromDb = context .Cities .FirstOrDefault(city => city.Name == inputModel.City); if (cityFromDb == null) { throw new ArgumentNullException(nameof(cityFromDb)); } AuctionHouse auctionHouse = new AuctionHouse { Name = inputModel.Name, Address = inputModel.Address, Description = inputModel.Description, }; auctionHouse.City = cityFromDb; context.AuctionHouses.Add(auctionHouse); int result = await context .SaveChangesAsync(); return(result > 0); }
public async Task Create_WithNotExistingCity_ShouldReturnSuccessfullyCreate() { var context = AuctionDbContextInMemory.InitializeContext(); await SeedData(context); this.auctionHouseService = new AuctionHouseService(context); var testAuctionHouse = new AuctionHouseCreateInputModel { Name = "Lucky", Address = "Hope street", Description = "We belive", City = "Yeet" }; await Assert.ThrowsAsync <ArgumentNullException>(() => this.auctionHouseService.Create(testAuctionHouse)); }
public async Task<IActionResult> Create(AuctionHouseCreateInputModel inputModel) { if (!this.ModelState.IsValid) { var allCities = await this.cityService.GetAllCities().ToListAsync(); this.ViewData["cities"] = allCities.Select(cities => new AuctionHouseCreateCityViewModel { Name = cities.Name }) .ToList(); return this.View(); } await this.auctionHouseService.Create(inputModel); return RedirectToHome(); }
public async Task Create_WithCorrectData_ShouldReturnSuccessfullyCreate() { string errorMessagePrefix = "AuctionService Create() does not work properly."; var context = AuctionDbContextInMemory.InitializeContext(); await SeedData(context); this.auctionHouseService = new AuctionHouseService(context); var testAuctionHouse = new AuctionHouseCreateInputModel { Name = "Lucky", Address = "Hope street", Description = "We belive", City = "Richmond" }; bool actualResult = await this.auctionHouseService.Create(testAuctionHouse); Assert.True(actualResult, errorMessagePrefix); }