Beispiel #1
0
        public async Task <IActionResult> Create(MenuCreateViewModel model)
        {
            string fileName = string.Empty;

            try
            {
                if (ModelState.IsValid)
                {
                    if (model.ImageFile != null)
                    {
                        fileName = await funcs.UploadFileAsync(model.ImageFile, uploadDirectory);

                        if (!string.IsNullOrEmpty(fileName))
                        {
                            await funcs.UploadFileToS3Async(uploadDirectory, fileName);
                        }
                    }

                    var menu = new Menu
                    {
                        Name         = model.Name,
                        Description  = model.Description,
                        Available    = model.Available,
                        Category     = model.Category,
                        Price        = model.Price,
                        Special      = model.Special,
                        SpecialPrice = model.SpecialPrice,
                        Image        = fileName,
                        DateCreated  = DateTime.Now,
                        DateUpdated  = DateTime.Now
                    };

                    await repository.AddSync(menu);

                    await unitOfWork.CompleteAsync();

                    // Invoke PUT Api function
                    // Update normal price to special price before calling Api cause only price column in DynamoDB
                    if (menu.Special)
                    {
                        menu.Price = menu.SpecialPrice;
                    }

                    await funcs.PostMenuToAWSAsync(menu);

                    return(RedirectToAction("details", new { id = menu.Id }));
                }
                return(View());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }