public async Task <IActionResult> Create(RestaurantCreateInputModel restaurantCreateInputModel) { var fileType = restaurantCreateInputModel.Image.ContentType.Split('/')[1]; if (!this.IsImageTypeValid(fileType)) { return(this.View(restaurantCreateInputModel)); } var restaurant = await this.restaurantsService.CreateAsync(restaurantCreateInputModel); return(this.RedirectToAction("Details", "Restaurants", new { area = "", id = restaurant.Id })); }
public async Task CreateAsyncThrowsArgumentExceptionIdRestaurantTypeInvalid() { await this.AddTestingDestinationToDb(); var invalidRestaurantCreateInputModel = new RestaurantCreateInputModel { Name = TestRestaurantName, DestinationId = TestDestinationId, Type = InvalidRestaurantType, }; var exception = await Assert.ThrowsAsync <ArgumentException>(() => this.RestaurantsServiceMock.CreateAsync(invalidRestaurantCreateInputModel)); Assert.Equal(string.Format(ServicesDataConstants.InvalidRestaurantType, invalidRestaurantCreateInputModel.Type), exception.Message); }
public async Task CreateAsyncAddsRestaurantToDbContext() { await this.AddTestingDestinationToDb(); RestaurantDetailsViewModel restaurantDetailsViewModel; using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; var restaurantCreateInputModel = new RestaurantCreateInputModel { Name = TestRestaurantName, DestinationId = TestDestinationId, Type = TestRestaurantType, Image = file, Address = TestRestaurantAddress, Seats = TestRestaurantSeats, }; restaurantDetailsViewModel = await this.RestaurantsServiceMock.CreateAsync(restaurantCreateInputModel); } ApplicationCloudinary.DeleteImage(ServiceProvider.GetRequiredService <Cloudinary>(), restaurantDetailsViewModel.Name); var restaurantsDbSet = this.DbContext.Restaurants.OrderBy(r => r.CreatedOn); Assert.Collection(restaurantsDbSet, elem1 => { Assert.Equal(restaurantsDbSet.Last().Id, restaurantDetailsViewModel.Id); Assert.Equal(restaurantsDbSet.Last().Name, restaurantDetailsViewModel.Name); Assert.Equal(restaurantsDbSet.Last().DestinationId, restaurantDetailsViewModel.DestinationId); Assert.Equal(restaurantsDbSet.Last().Destination.Name, restaurantDetailsViewModel.DestinationName); Assert.Equal(restaurantsDbSet.Last().Address, restaurantDetailsViewModel.Address); Assert.Equal(restaurantsDbSet.Last().Type.ToString(), restaurantDetailsViewModel.Type); Assert.Equal(restaurantsDbSet.Last().ImageUrl, restaurantDetailsViewModel.ImageUrl); }); }
public async Task <RestaurantDetailsViewModel> CreateAsync(RestaurantCreateInputModel restaurantCreateInputModel) { if (!Enum.TryParse(restaurantCreateInputModel.Type, true, out RestaurantType typeEnum)) { throw new ArgumentException(string.Format(ServicesDataConstants.InvalidRestaurantType, restaurantCreateInputModel.Type)); } // If destination exists return existing view model var restaurantExists = this.restaurantsRepository.All().Any( r => r.Name == restaurantCreateInputModel.Name && r.DestinationId == restaurantCreateInputModel.DestinationId); if (restaurantExists) { return(AutoMapper.Mapper .Map <RestaurantDetailsViewModel>(this.restaurantsRepository.All() .First(r => r.Name == restaurantCreateInputModel.Name && r.DestinationId == restaurantCreateInputModel.DestinationId))); } var imageUrl = await ApplicationCloudinary.UploadImage(this.cloudinary, restaurantCreateInputModel.Image, restaurantCreateInputModel.Name); var restaurant = new Restaurant() { Name = restaurantCreateInputModel.Name, Address = restaurantCreateInputModel.Address, DestinationId = restaurantCreateInputModel.DestinationId, ImageUrl = imageUrl, Type = typeEnum, Seats = restaurantCreateInputModel.Seats, }; this.restaurantsRepository.Add(restaurant); await this.restaurantsRepository.SaveChangesAsync(); var restaurantDetailsViewModel = AutoMap.Mapper.Map <RestaurantDetailsViewModel>(restaurant); return(restaurantDetailsViewModel); }