private async Task <string> UploadImageAsync(WhiskeyInputModel whiskeyInputModel) { string fileName = null; if (whiskeyInputModel.Image != null) { string uploadDirectory = Path.Combine(this.webHostEnvironment.WebRootPath, "images", "whiskeyImages"); fileName = Guid.NewGuid().ToString() + "-" + whiskeyInputModel.Image.FileName; string filePath = Path.Combine(uploadDirectory, fileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await whiskeyInputModel.Image.CopyToAsync(fileStream); } } return(fileName); }
public async Task AddWhiskeyAsync(WhiskeyInputModel whiskeyInputModel) { var whiskey = this.context .Whiskeys .Where(x => x.Brand == whiskeyInputModel.Brand) .FirstOrDefault(); if (whiskey == null) { string image = await this.UploadImageAsync(whiskeyInputModel); var whiskeyToAdd = new Whiskey() { Image = image, Brand = whiskeyInputModel.Brand, Year = whiskeyInputModel.Year, Quantity = whiskeyInputModel.Quantity, }; await this.context.Whiskeys.AddAsync(whiskeyToAdd); await this.context.SaveChangesAsync(); } }