Esempio n. 1
0
        public async Task <IActionResult> Update([Bind] News model)
        {
            var token = await webAPIToken.New();

            var result = await webAPI.UpdateAsync <News>(model, ApiURL.NEWS + model.Id, token);

            TempData["Article"] = "Nyhetsartikeln har uppdaterats";

            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public async Task <ActionResult> UpdateLogin([Bind] UpdateUserPasswordModel model)
        {
            if (ModelState.IsValid)
            {
                var apiResult = await webAPI.UpdateAsync <UpdateUserPasswordModel>(model, ApiURL.USERS_LOGIN_UPDATE + User.Identity.Name);

                if (apiResult.Status.IsSuccessStatusCode)
                {
                    await SetAuthCookie(apiResult.APIPayload);

                    TempData["PasswordSuccess"] = "Lösenordet har uppdaterats!";
                    return(RedirectToAction(nameof(UpdateLogin)));
                }
                else
                {
                    ModelState.AddModelError("CurrentPassword", "Felaktigt lösenord");
                }
            }

            return(View(model));
        }
Esempio n. 3
0
        public async Task <IActionResult> EditStatus([Bind] OrderViewModel model, int statusId)
        {
            var token = await webAPIToken.New();

            var order = await webAPI.GetOneAsync <Order>(ApiURL.ORDERS + model.Id, token);

            // Update status
            order.StatusId = statusId;

            // Save changes
            var apiResult = await webAPI.UpdateAsync(order, ApiURL.ORDERS + model.Id, token);

            TempData["StatusUpdate"] = true;
            return(RedirectToAction("EditStatus"));
        }
Esempio n. 4
0
        public async Task <IActionResult> EditCategory([Bind] Category model)
        {
            model.categoryCollection = await GetAllCategories();

            if (ModelState.IsValid)
            {
                // If category contains an Id, update it, else create new category!
                if (model.Id > 0)
                {
                    var result = model.categoryCollection.Where(x => x.Id == model.Id).FirstOrDefault();
                    result.Name = model.Name;

                    var token = await webAPIToken.New();

                    var response = await webAPI.UpdateAsync(result, ApiURL.CATEGORIES + result.Id, token);

                    TempData["CategoryUpdate"] = "Kategorin har uppdaterats!";
                }
                else
                {
                    // Does category already exist?
                    if (model.categoryCollection.Any(x => x.Name == model.Name))
                    {
                        ModelState.AddModelError("Name", "Kategorin finns redan registrerad!");
                        return(View("EditCategory", model));
                    }

                    // Create new category
                    var category = new Category()
                    {
                        Name = model.Name
                    };

                    // Post to API
                    var token = await webAPIToken.New();

                    var response = await webAPI.PostAsync <Category>(category, ApiURL.CATEGORIES, token);

                    TempData["NewCategory"] = "Ny kategori har skapats!";
                }

                return(RedirectToAction("EditCategory", "Category", new { id = "" }));
            }
            else
            {
                return(View("EditCategory", model));
            }
        }
Esempio n. 5
0
        public async Task <IActionResult> Edit([Bind] Brand model)
        {
            model.BrandsCollection = await GetAllBrands();

            if (ModelState.IsValid)
            {
                if (model.Id > 0)
                {
                    var brand = model.BrandsCollection.Where(x => x.Id == model.Id).FirstOrDefault();
                    brand.Name = model.Name;

                    var token = await webAPIToken.New();

                    var response = await webAPI.UpdateAsync(brand, ApiURL.BRANDS, token);
                }
                else
                {
                    // Does brand already exist?
                    if (model.BrandsCollection.Any(x => x.Name == model.Name))
                    {
                        ModelState.AddModelError("Name", "Tillverkaren finns redan registrerad!");
                        return(View("index", model));
                    }

                    // Create new brand
                    var brand = new Brand()
                    {
                        Name = model.Name
                    };

                    // Post to API
                    var token = await webAPIToken.New();

                    var response = await webAPI.PostAsync(brand, ApiURL.BRANDS, token);

                    TempData["NewBrand"] = "Ny tillverkare har skapats!";
                }

                return(RedirectToAction("index", "Brand"));
            }
            else
            {
                return(View("index", model));
            }
        }
Esempio n. 6
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"));
            }
        }