public async Task <IActionResult> Create(AdminCategoryCreateInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            var imageUploadModel = new ImageUploadInputModel();

            if (inputModel.ImageModel.ImageFile != null)
            {
                imageUploadModel = new ImageUploadInputModel
                {
                    ImageFile = inputModel.ImageModel.ImageFile,
                };
                await this.UploadProductImage(imageUploadModel);
            }

            var category = new Category
            {
                Name             = inputModel.Name,
                Description      = inputModel.Description,
                ImageUrl         = imageUploadModel.ImageUrl,
                ImageStorageName = imageUploadModel.ImageStorageName,
            };

            await this.categoriesService.CreateAsync(category);

            this.TempData["Message"]     = CATEGORY_CREATE_SUCCESS;
            this.TempData["MessageType"] = AlertMessageTypes.Success;

            return(this.RedirectToAction("View", new { categoryId = category.Id }));
        }
        private async Task UploadProductImage(ImageUploadInputModel imageModel)
        {
            string fileNameForStorage = imageModel.ImageFile.FileName;

            imageModel.ImageUrl = await this.googleCloudStorage.UploadFileAsync(imageModel.ImageFile, fileNameForStorage);

            imageModel.ImageStorageName = fileNameForStorage;
        }
        public async Task <IActionResult> Edit(AdminProductEditInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                // Pass back the categories and Sizes list because POST Requests lose Collections
                var categories =
                    await this.categoriesService.GetAll <CategoryDropdownViewModel>();

                inputModel.Categories = categories;
                return(this.View(inputModel));
            }

            var product = await this.productsService.GetBaseById(inputModel.Id);

            product.Name        = inputModel.Name;
            product.Description = inputModel.Description;
            product.CategoryId  = inputModel.CategoryId;

            if (inputModel.IsNewImage)
            {
                var imageUploadModel = new ImageUploadInputModel();
                if (inputModel.ImageModel.ImageFile != null)
                {
                    imageUploadModel = new ImageUploadInputModel
                    {
                        ImageFile = inputModel.ImageModel.ImageFile,
                    };
                    await this.UploadProductImage(imageUploadModel);

                    product.ImageUrl         = imageUploadModel.ImageUrl;
                    product.ImageStorageName = imageUploadModel.ImageStorageName;
                }
            }

            var filteredSizesInputs = inputModel.Sizes
                                      .Where(s => !string.IsNullOrEmpty(s.Name) && s.Price >= 0M).ToList();

            List <ProductSize> sizes = this.mapper.Map <List <ProductSize> >(filteredSizesInputs);

            product.Sizes = sizes;

            // Delete old sizes
            await this.productSizeService.DeleteProductSizes(product.Id);

            await this.productsService.UpdateAsync(product);

            this.TempData["Message"]     = PRODUCT_DELETED;
            this.TempData["MessageType"] = AlertMessageTypes.Success;

            // Custom route redirect because it matches the normal ProductsController action
            return(this.RedirectToRoute(new
            {
                area = "Administration",
                controller = "Products",
                action = "View",
                productId = product.Id,
            }));
        }
        public async Task <IActionResult> Create(AdminProductCreateInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                // Pass back the categories list because POST Requests lose Collections
                var categories =
                    await this.categoriesService.GetAll <CategoryDropdownViewModel>();

                inputModel.Categories = categories;
                return(this.View(inputModel));
            }

            var imageUploadModel = new ImageUploadInputModel();

            if (inputModel.ImageModel.ImageFile != null)
            {
                imageUploadModel = new ImageUploadInputModel
                {
                    ImageFile = inputModel.ImageModel.ImageFile,
                };
                await this.UploadProductImage(imageUploadModel);
            }

            var filteredSizesInputs = inputModel.Sizes
                                      .Where(s => !string.IsNullOrEmpty(s.Name) && s.Price >= 0M).ToList();

            List <ProductSize> sizes = this.mapper.Map <List <ProductSize> >(filteredSizesInputs);

            var product = new Product
            {
                Name             = inputModel.Name,
                Description      = inputModel.Description,
                CategoryId       = inputModel.CategoryId,
                Sizes            = sizes,
                ImageUrl         = imageUploadModel.ImageUrl,
                ImageStorageName = imageUploadModel.ImageStorageName,
            };

            await this.productsService.CreateAsync(product);

            return(this.RedirectToAction("View", new { id = product.Id }));
        }
        public async Task <IActionResult> Edit(AdminCategoryEditInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            var category = await this.categoriesService.GetBaseById(inputModel.Id);

            if (inputModel.IsNewImage)
            {
                var imageUploadModel = new ImageUploadInputModel();
                if (inputModel.ImageModel.ImageFile != null)
                {
                    imageUploadModel = new ImageUploadInputModel
                    {
                        ImageFile = inputModel.ImageModel.ImageFile,
                    };
                    await this.UploadProductImage(imageUploadModel);

                    category.ImageUrl         = imageUploadModel.ImageUrl;
                    category.ImageStorageName = imageUploadModel.ImageStorageName;
                }
            }

            await this.categoriesService.UpdateAsync(category);

            this.TempData["Message"]     = CATEGORY_EDIT_SUCCESS;
            this.TempData["MessageType"] = AlertMessageTypes.Success;

            // Custom route redirect because it matches the normal ProductsController action
            return(this.RedirectToRoute(new
            {
                area = "Administration",
                controller = "Categories",
                action = "View",
                categoryId = category.Id,
            }));
        }