public async Task <IActionResult> Index([Bind] OrderAndPaymentMethods model) { if (ModelState.IsValid) { // Include customers Email. It will be used as userId in the API model.OrderViewModel.UserEmail = User.Identity.Name; // Get cart id var cartId = Guid.Parse(HttpContext.Session.GetString(_cartSessionCookie)); // Send order to API var token = await webAPIToken.New(); var apiResult = await webAPI.PostAsync(model.OrderViewModel, ApiURL.ORDERS + cartId, token); if (apiResult.Status.IsSuccessStatusCode) { return(RedirectToAction(nameof(ThankYou))); } else { TempData["OrderError"] = "Oops det här var pinsamt! Kunde inte skapa din order. Något sket sig, eh he hee...."; } } TempData["PaymentMethodError"] = "Vänligen välj ett betalsätt"; return(RedirectToAction("Index")); }
public async Task <IActionResult> Index(AllProductsViewModel model) { Rating rating = model.NewRating; // Register rating with current date rating.RateDate = DateTime.UtcNow; rating.UserEmail = User.Identity.Name; rating.UserId = User.UserId(); // Request new token and store rating var token = await webAPIToken.New(); var apiResponse = await webAPI.PostAsync <Rating>(rating, ApiURL.RATINGS_POST, token); // Was new rating saved successfully? if (apiResponse.Status.IsSuccessStatusCode) { TempData["NewRatingSaved"] = true; } else { TempData["NewRatingFailed"] = true; } return(RedirectToAction("ProductDetail", "Product", new { id = rating.ProductId })); }
public async Task <ActionResult> Login([Bind] LoginModel model) { if (ModelState.IsValid) { var apiResult = await webAPI.PostAsync(model, ApiURL.USERS_LOGIN); if (apiResult.Status.IsSuccessStatusCode) { await SetAuthCookie(apiResult.APIPayload, model.RememberUser); return(RedirectToAction("Index", "Home")); } } ViewBag.LoginResult = "Felaktiga inloggningsuppgifter! Försök igen!"; return(View(model)); }
public async Task <IActionResult> AddToCart(int id) { // Generate a unique id Guid guid = Guid.NewGuid(); // Does session cookie exist? If not, bake one! if (HttpContext.Session.GetString(_cartSessionCookie) == null) { HttpContext.Session.SetString(_cartSessionCookie, guid.ToString()); } ShoppingCart shoppingCart = new ShoppingCart() { CartId = Guid.Parse(HttpContext.Session.GetString(_cartSessionCookie)), ProductId = id, Amount = 1 }; await webAPI.PostAsync <ShoppingCart>(shoppingCart, "https://localhost:44305/api/carts"); return(Ok()); }
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)); } }
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)); } }
public async Task <IActionResult> AddArticle([Bind] News model) { if (ModelState.IsValid) { // Add current date to article model.NewsDate = DateTime.UtcNow; var token = await webAPIToken.New(); var result = await webAPI.PostAsync(model, ApiURL.NEWS, token); TempData["Article"] = "Ny artikel har skapats"; return(RedirectToAction("Index")); } else { return(View(model)); } }
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")); } }