Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(MenuEditViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var menu = await repository.GetMenuAsync(model.Id);

                    menu.Name         = model.Name;
                    menu.Description  = model.Description;
                    menu.Category     = model.Category;
                    menu.Price        = model.Price;
                    menu.Available    = model.Available;
                    menu.Special      = model.Special;
                    menu.SpecialPrice = model.SpecialPrice;
                    menu.DateUpdated  = DateTime.Now;

                    if (model.ImageFile != null)
                    {
                        if (model.Image != null)
                        {
                            // delete the file in S3 Bucket
                            await funcs.DeleteObjectInS3Async(model.Image);

                            // delete the file in images folder
                            funcs.DeleteFile(Path.Combine(uploadDirectory, model.Image));
                        }

                        // upload a new image
                        string fileName = await funcs.UploadFileAsync(model.ImageFile, uploadDirectory);

                        if (!string.IsNullOrEmpty(fileName))
                        {
                            menu.Image = fileName;

                            // upload the new file to S3
                            await funcs.UploadFileToS3Async(uploadDirectory, fileName);
                        }
                    }

                    repository.Update(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.PutMenuToAWSAsync(menu);

                    return(RedirectToAction(nameof(Details), new { id = menu.Id }));
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                // need to implement logging
                throw new Exception(ex.Message);
            }
        }