public async Task <IActionResult> EditImage(int id, [Bind("CategoryId,CategoryName,PictureFile")] CategoriesEditImageViewModel model)
        {
            if (id != model.CategoryId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    await _categoriesService.UpdateFromCategoriesEditImageViewModel(model);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (await CategoriesExists(model.CategoryId) == false)
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
        public async Task <CategoriesEditImageViewModel> GetCategoriesEditImageViewModelByIdAsync(int id)
        {
            var category = await _categoriesRepository.GetByIdAsync(id);

            var model = new CategoriesEditImageViewModel()
            {
                CategoryId   = category.CategoryId,
                CategoryName = category.CategoryName,
                PictureFile  = new FormFile(new MemoryStream(category.Picture), 0, category.Picture.Length, category.CategoryName, "image.bmp")
            };

            return(model);
        }
        public async Task <IActionResult> Update(int id, [FromForm] CategoriesEditImageViewModel model)
        {
            try
            {
                if (id != model.CategoryId)
                {
                    _logger.LogError("Id from Url and model doesn't match.");
                    return(BadRequest("Id from Url and model doesn't match."));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid owner object sent from client.");
                    return(BadRequest("Invalid model object"));
                }
                try
                {
                    await _categoriesService.UpdateFromCategoriesEditImageViewModel(model);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (await CategoriesExists(model.CategoryId) == false)
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside Update action: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
        public async Task UpdateFromCategoriesEditImageViewModel(CategoriesEditImageViewModel model)
        {
            if (model == null || model.CategoryId == 0)
            {
                return;
            }
            var categories = await GetByIdAsync(model.CategoryId);

            if (!string.IsNullOrEmpty(model.CategoryName))
            {
                categories.CategoryName = model.CategoryName;
            }
            if (model != null && model.PictureFile != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await model.PictureFile.CopyToAsync(memoryStream);

                    categories.Picture = memoryStream.ToArray();
                }
            }
            await Update(categories);
        }