Esempio n. 1
0
        public async Task <ActionResult> CreateProduct(IFormFile file, [Bind] AllProductsViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (!IsUploadedFileImage(file))
                    {
                        ModelState.AddModelError("Photo", "Filen är ogiltig!");
                        TempData["Errors"] = "Filen är ogiltig!";
                        model.Categories   = await webAPI.GetAllAsync <Category>(ApiURL.CATEGORIES);

                        model.Brands = await webAPI.GetAllAsync <Brand>(ApiURL.BRANDS);

                        return(View(model));
                    }

                    // Instantiate new product
                    Product newProduct = new Product()
                    {
                        Name            = model.Name,
                        Price           = model.Price,
                        Quantity        = model.Quantity,
                        CategoryId      = model.CategoryId,
                        BrandId         = model.BrandId,
                        Description     = model.Description,
                        FullDescription = model.FullDescription,
                        Specification   = model.Specification,
                        Discount        = model.Discount,
                        ActiveProduct   = model.ActiveProduct
                    };

                    // Request token
                    var token = await webAPIToken.New();

                    // Store product
                    var apiResonse = await webAPI.PostAsync(newProduct, ApiURL.PRODUCTS, token);

                    // Deserialize API response content and get ID of newly created product
                    var newProductId = webAPI.DeserializeJSON <AllProductsViewModel>(apiResonse.ResponseContent).Id;
                    newProduct.Id = newProductId;

                    // Store image in www root folder with unique product Id
                    if (file != null)
                    {
                        // Set category folder name
                        var folderName = await GetCategoryName(model.CategoryId);

                        // Store new image
                        ProductImage productImage = new ProductImage(environment.WebRootPath, folderName, file);
                        newProduct.Photo = productImage.StoreImage(newProduct.Id);
                    }

                    // Update product with image
                    var response = webAPI.UpdateAsync <Product>(newProduct, ApiURL.PRODUCTS + newProduct.Id, token);
                }

                else
                {
                    model.Categories = await webAPI.GetAllAsync <Category>(ApiURL.CATEGORIES);

                    model.Brands = await webAPI.GetAllAsync <Brand>(ApiURL.BRANDS);

                    TempData["Errors"] = "Fyll i formuläret ordentligt";
                    return(View(model));
                }

                TempData["Succesmsg"] = $"Great!! {model.Name} skapad i databasen";
                return(RedirectToAction("AllProducts", "Product"));
            }
            catch
            {
                TempData["Database error"] = "Sorry!! Något gick fel när du lägger Data till databasen";
                return(RedirectToAction("CreateProduct", "Product"));
            }
        }
Esempio n. 2
0
        public async Task <ActionResult> EditProduct(IFormFile file, [Bind] AllProductsViewModel model)
        {
            try
            {
                // Get product by id from API
                var productInDB = await webAPI.GetOneAsync <AllProductsViewModel>(ApiURL.PRODUCTS + model.Id);

                if (ModelState.IsValid)
                {
                    if (!IsUploadedFileImage(file))
                    {
                        ModelState.AddModelError("Photo", "Filen är ogiltig!");
                        TempData["Errors"] = "Filen är ogiltig!";
                        model.Categories   = await webAPI.GetAllAsync <Category>(ApiURL.CATEGORIES);

                        model.Brands = await webAPI.GetAllAsync <Brand>(ApiURL.BRANDS);

                        return(View(model));
                    }

                    Product editproduct = new Product()
                    {
                        Id              = model.Id,
                        Name            = model.Name,
                        Price           = Convert.ToDecimal(model.Price.ToString().Replace('.', ',')),
                        Quantity        = model.Quantity,
                        CategoryId      = model.CategoryId,
                        BrandId         = model.BrandId,
                        Description     = model.Description,
                        Photo           = model.Photo,
                        FullDescription = model.FullDescription,
                        Specification   = model.Specification,
                        Discount        = Convert.ToSingle(model.Discount.ToString().Replace('.', ',')),
                        ActiveProduct   = model.ActiveProduct
                    };

                    // Set category folder name
                    var folderName = await GetCategoryName(model.CategoryId);

                    // Update image
                    if (file != null)
                    {
                        // Remove old image and store new image
                        ProductImage productImage = new ProductImage(environment.WebRootPath, folderName, file);
                        productImage.DeleteImage(editproduct.Photo);
                        editproduct.Photo = productImage.StoreImage(editproduct.Id);
                    }

                    // If category was changed, move existing product photo to selected folder
                    else if (productInDB.CategoryId != model.CategoryId)
                    {
                        // Move image to new location
                        ProductImage productImage = new ProductImage(environment.WebRootPath, folderName, editproduct.Photo);
                        editproduct.Photo = productImage.MoveImage();
                    }

                    // Send to API and update product
                    var token = await webAPIToken.New();

                    var apiResonse = await webAPI.UpdateAsync(editproduct, ApiURL.PRODUCTS + editproduct.Id, token);
                }

                else
                {
                    // TODO: Separate this to its own method?
                    model.Categories = await webAPI.GetAllAsync <Category>(ApiURL.CATEGORIES);

                    model.Brands = await webAPI.GetAllAsync <Brand>(ApiURL.BRANDS);

                    return(View(model));
                }

                TempData["EditSuccesmsg"] = $"{model.Name} har uppdaterats!";
                return(RedirectToAction("AllProducts", "Product"));
            }
            catch
            {
                TempData["EditDatabase error"] = "Oops! Något gick fel. Försök igen eller kontakta support om problemet kvarstår.";
                return(RedirectToAction("EditProduct", "Product"));
            }
        }