Exemple #1
0
        public async Task <IActionResult> Create([Bind("Id,Name,Description,Price,ImagesToAdd")] ItemModel formItem)
        {
            // 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 (ModelState.IsValid)
            {
                // Check if it's images that are being uploaded
                if (formItem.ImagesToAdd != null && formItem.ImagesToAdd.Count > 0)
                {
                    foreach (var img in formItem.ImagesToAdd)
                    {
                        if (!img.ContentType.Contains("image"))
                        {
                            return(StatusCode(StatusCodes.Status415UnsupportedMediaType));
                        }
                    }
                }

                Item newItem = new Item {
                    Name        = formItem.Name,
                    Description = formItem.Description,
                    Price       = formItem.Price
                };
                await _leiunurk.AddItem(newItem, formItem.ImagesToAdd, imgUploadPath);

                StatusMessage = "The Item has been created!";
                return(RedirectToAction(nameof(Index)));
            }

            return(View(formItem));
        }