Exemple #1
0
        public async Task <IActionResult> Edit(int id, ItemModel item)
        {
            // Check if logged in user is still in the system, if not then sign user out and return to homepage
            if (!await _applicationUser.UserExistsById(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                await _signInManager.SignOutAsync();

                return(RedirectToAction("Index", "Home", new { area = "" }));
            }

            if (id != item.Id)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                // This all should probably be in the Service logic class, not doing it right now because can't find a way to return NotFound() 404 from the Service class.
                // Try and Catch for database concurrency, what if someone else was already editing and saved changes, we would just overwrite his changes
                // doing this to prevent that
                // Might not actually work if we don't throw concurrency exception by ourselves.
                try {
                    ICollection <ItemImage> ItemImages = new List <ItemImage>();
                    List <int> ItemImagesToRemoveIds   = new List <int>();


                    // Check if user has chosen to remove all the images
                    if (item.Images != null && item.Images.Any())
                    {
                        // Since we are using ItemModel which doesn't know about the Image list that every item has,
                        //  we'll add them to an Image list and after create the Item to be inserted to the database
                        foreach (var modelImage in item.Images)
                        {
                            ItemImages.Add(new ItemImage {
                                Id            = modelImage.Id,
                                ImageFileName = modelImage.ImageFileName
                            });

                            // Check if user selected the image to be removed
                            if (modelImage.RemoveImage)
                            {
                                ItemImagesToRemoveIds.Add(modelImage.Id);
                            }
                        }
                    }


                    // Create Item for database insertion from the ItemEditModel class
                    Item EditedItem = new Item {
                        Id          = item.Id,
                        Name        = item.Name,
                        Description = item.Description,
                        Images      = ItemImages,
                        Price       = item.Price
                    };

                    // Check if new added images are the right type.
                    if (item.ImagesToAdd != null && item.ImagesToAdd.Any())
                    {
                        foreach (var img in item.ImagesToAdd)
                        {
                            if (!img.ContentType.Contains("image"))
                            {
                                return(StatusCode(StatusCodes.Status415UnsupportedMediaType));
                            }
                        }
                    }

                    await _leiunurk.EditItem(EditedItem, ItemImagesToRemoveIds, item.ImagesToAdd, imgUploadPath);

                    StatusMessage = "The Item has been edited!";
                }
                catch (DbUpdateConcurrencyException) {
                    if (!(await _leiunurk.ItemExists(item.Id)))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            // show form again if model not valid
            return(View(item));
        }