public IActionResult ChangeSaleStatus(string saleId) { if (this.saleService.SaleExists(saleId) == false) { return(this.NotFound()); } if (this.TempData[GlobalConstants.ErrorsFromPOSTRequest] != null) { ModelStateHelper.MergeModelStates(this.TempData, this.ModelState); } var viewModel = this.saleService.GetAllSaleStatuses(); var inputModel = new ChangeSaleStatusInputModel { SaleId = saleId }; if (this.TempData["NewSaleStatusId"] != null) { inputModel.NewSaleStatusId = this.TempData["NewSaleStatusId"].ToString(); } var complexModel = new ComplexModel <ChangeSaleStatusInputModel, List <SaleStatus> > { ViewModel = viewModel, InputModel = inputModel }; return(this.View(complexModel)); }
public async Task <IActionResult> ProductPage(string productId, string toReplyComment, int commentsPage, int commentsOrderingOption) { //Check if the product in question exists if (this.productService.ProductExistsById(productId) == false) { return(this.NotFound()); } var product = this.productService.GetProductById(productId, true); var viewModel = mapper.Map <ProductInfoViewModel>(product); viewModel.ProductCategories = this.categoryService.GetCategoriesForProduct(productId); //Sanitize commentsPage commentsPage = int.Parse(this.htmlEncoder.Encode(commentsPage.ToString())); var currentUserId = this.userService.GetUserId(this.User.Identity.Name); viewModel.CurrentUserId = currentUserId; //Short description viewModel.ShortDescription = this.productService.GetShordDescription(viewModel.Description, 40); //Overall product rating viewModel.ProductRating = new ProductRatingViewModel(this.productService.GetAverageRating(viewModel.ProductRatings)); await FillProductInfoViewModel(viewModel, productId, commentsPage, toReplyComment, commentsOrderingOption); //Add each model state error from the last action to this one if (TempData[GlobalConstants.ErrorsFromPOSTRequest] != null) { ModelStateHelper.MergeModelStates(TempData, this.ModelState); } object complexModel = null; var typeOfInputModel = TempData[GlobalConstants.InputModelFromPOSTRequestType]; //If input model is for adding review if (typeOfInputModel?.ToString() == nameof(AddReviewInputModel)) { complexModel = AssignViewAndInputModels <AddReviewInputModel, ProductInfoViewModel>(viewModel); } //If input model is for replying to a comment else if (typeOfInputModel?.ToString() == nameof(ReplyCommentInputModel)) { var replyCommentInputModelsJSON = TempData[GlobalConstants.InputModelFromPOSTRequest]?.ToString(); var replyCommentInputModel = JsonSerializer.Deserialize <ReplyCommentInputModel>(replyCommentInputModelsJSON); viewModel.ReplyCommentInputModel = replyCommentInputModel; complexModel = AssignViewAndInputModels <AddReviewInputModel, ProductInfoViewModel>(viewModel, true); } //If there isn't an input model else { complexModel = AssignViewAndInputModels <AddReviewInputModel, ProductInfoViewModel>(viewModel, true); } return(this.View("ProductPage", complexModel)); }
public IActionResult Create() { if (TempData[GlobalConstants.ErrorsFromPOSTRequest] != null) { ModelStateHelper.MergeModelStates(TempData, this.ModelState); var inputModel = JsonSerializer.Deserialize <AddCategoryInputModel>(TempData[GlobalConstants.InputModelFromPOSTRequest].ToString()); } return(this.View()); }
public async Task <IActionResult> Edit(string productId, string errorReturnUrl) { //Check if product with this id exists. MAYBE I WILL REPLACE THIS LATER WILL HAVE TO SWITCH TO DEVELOPMENT AND SEED if (this.productService.ProductExistsById(productId) == false) { return(this.NotFound()); //return this.View("/Views/Shared/Error.cshtml"); } AddProductInputModel inputModel = null; //Add each model state error from the last action to this one. Fill the input model with he values from the last post action if (TempData[GlobalConstants.ErrorsFromPOSTRequest] != null && TempData[GlobalConstants.InputModelFromPOSTRequestType]?.ToString() == nameof(AddProductInputModel)) { ModelStateHelper.MergeModelStates(TempData, this.ModelState); var inputModelJSON = TempData[GlobalConstants.InputModelFromPOSTRequest]?.ToString(); inputModel = JsonSerializer.Deserialize <AddProductInputModel>(inputModelJSON); var product = this.productService.GetProductById(productId, true); //Get the categories for the product inputModel.CategoriesIds = this.categoryService.GetCategoriesForProduct(product.Id).Select(x => x.Id).ToList(); } //If there wasn't an error with the edit form prior to this, just fill the inputModel like normal else { var product = this.productService.GetProductById(productId, true); inputModel = this.mapper.Map <AddProductInputModel>(product); //Get the categories for the product inputModel.CategoriesIds = this.categoryService.GetCategoriesForProduct(product.Id).Select(x => x.Id).ToList(); //Set the correct additional images paths for (int i = 0; i < product.AdditionalImages.Count; i++) { var image = product.AdditionalImages.ToList()[i]; inputModel.AdditionalImages[i] = image.Image; } } //Set the input model mode to edit inputModel.IsAdding = false; //Set input model short description inputModel.ShortDescription = this.productService.GetShordDescription(inputModel.Description, 40); return(this.View("/Views/Products/Add.cshtml", inputModel)); }
public async Task <IActionResult> Add() { AddProductInputModel inputModel = null; //Add each model state error from the last action to this one if (TempData[GlobalConstants.ErrorsFromPOSTRequest] != null) { ModelStateHelper.MergeModelStates(TempData, ModelState); var inputModelJSON = TempData[GlobalConstants.InputModelFromPOSTRequest]?.ToString(); inputModel = JsonSerializer.Deserialize <AddProductInputModel>(inputModelJSON); } return(this.View(inputModel)); }
public async Task <IActionResult> Checkout() { var currentUserId = this.userService.GetUserId(this.User.Identity.Name); var cardProducts = this.cartService.GetAllProductsForCheckoutViewModel(currentUserId); //Check if the user has any products in their cart if (cardProducts.Count == 0) { //Set notification NotificationHelper.SetNotification(TempData, NotificationType.Error, "You do not have any products in your cart"); return(this.RedirectToAction("All", "Carts")); } var checkoutViewModel = new CheckoutViewModel { ProductsInfo = cardProducts, TotalPrice = cardProducts.Sum(x => x.SinglePrice * x.Quantity), Countries = this.countryService.GetAllCountries(), PaymentMethods = this.paymentMethodService.GetAllPaymentMethods() }; var complexModel = new ComplexModel <CheckoutInputModel, CheckoutViewModel> { ViewModel = checkoutViewModel }; //TODO: Add input model on error from post request if (this.TempData[GlobalConstants.ErrorsFromPOSTRequest] != null) { ModelStateHelper.MergeModelStates(this.TempData, this.ModelState); } if (this.TempData[GlobalConstants.InputModelFromPOSTRequestType]?.ToString() == nameof(CheckoutInputModel)) { complexModel.InputModel = JsonSerializer.Deserialize <CheckoutInputModel>(this.TempData[GlobalConstants.InputModelFromPOSTRequest]?.ToString()); } return(this.View(complexModel)); }
public IActionResult Add() { AddProductInputModel inputModel = null; //Add each model state error from the last action to this one if (TempData[GlobalConstants.ErrorsFromPOSTRequest] != null) { ModelStateHelper.MergeModelStates(TempData, ModelState); var inputModelJSON = TempData[GlobalConstants.InputModelFromPOSTRequest]?.ToString(); inputModel = JsonSerializer.Deserialize <AddProductInputModel>(inputModelJSON); //Add categories names inputModel.CategoriesNames = new List <string>(); var categoriesNames = this.categoryService.GetCategoryNamesFromIds(inputModel.CategoriesIds); inputModel.CategoriesNames.AddRange(categoriesNames); } return(this.View(inputModel)); }
public IActionResult All() { var currentUserId = this.userService.GetUserId(this.User.Identity.Name); var productsInCart = this.cartService.GetAllProductsForCartViewModel(currentUserId); var complexModel = new ComplexModel <List <BuyProductInputModel>, List <ProductCartViewModel> > { ViewModel = productsInCart }; if (TempData.ContainsKey(GlobalConstants.ErrorsFromPOSTRequest)) { //Merge model states ModelStateHelper.MergeModelStates(TempData, this.ModelState); } if (this.TempData[GlobalConstants.InputModelFromPOSTRequestType]?.ToString() == $"List<{nameof(BuyProductInputModel)}>") { complexModel.InputModel = JsonSerializer.Deserialize <List <BuyProductInputModel> >(this.TempData[GlobalConstants.InputModelFromPOSTRequest]?.ToString()); } complexModel.ViewModel .OrderBy(x => x.Id) .ToList(); //Validate if quantity for one of the products exceeds the quantity in stock for (int i = 0; i < complexModel.ViewModel.Count; i++) { var productViewModel = complexModel.ViewModel[i]; if (productViewModel.QuantityInStock < productViewModel.Quantity) { this.ModelState.AddModelError($"InputModel[{i}].Quantity", "This product's selected quantity is more than the available quantity in stock."); if (this.ViewData["CartBuyButtonErrorForQuantity"] == null) { this.ViewData["CartBuyButtonErrorForQuantity"] = true; } } } return(this.View(complexModel)); }
public IActionResult Edit(string categoryId) { EditCategoryInputModel inputModel; if (TempData[GlobalConstants.ErrorsFromPOSTRequest] != null) { ModelStateHelper.MergeModelStates(TempData, this.ModelState); inputModel = JsonSerializer.Deserialize <EditCategoryInputModel>(TempData[GlobalConstants.InputModelFromPOSTRequest].ToString()); } else { //Check if category exists if (this.categoryService.CategoryExists(categoryId) == false) { return(this.NotFound()); } var category = this.categoryService.GetCategoryById(categoryId); inputModel = this.mapper.Map <EditCategoryInputModel>(category); } return(this.View(inputModel)); }
public async Task <IActionResult> All() { var currentUserId = this.userService.GetUserId(this.User.Identity.Name); var productsInCart = this.cartService.GetAllProductsForCartViewModel(currentUserId); var complexModel = new ComplexModel <List <BuyProductInputModel>, List <ProductCartViewModel> > { ViewModel = productsInCart }; if (TempData.ContainsKey(GlobalConstants.ErrorsFromPOSTRequest)) { //Merge model states ModelStateHelper.MergeModelStates(TempData, this.ModelState); } if (this.TempData[GlobalConstants.InputModelFromPOSTRequestType]?.ToString() == $"List<{nameof(BuyProductInputModel)}>") { complexModel.InputModel = JsonSerializer.Deserialize <List <BuyProductInputModel> >(this.TempData[GlobalConstants.InputModelFromPOSTRequest]?.ToString()); } return(this.View(complexModel)); }
public IActionResult Edit(string productId) { if (this.productService.ProductExistsById(productId) == false) { NotificationHelper.SetNotification(this.TempData, NotificationType.Error, "The product doesn't exist"); return(this.RedirectToAction("ProductPage", "Products", new { area = "", productId = productId })); } EditProductInputModel inputModel = null; //Add each model state error from the last action to this one. Fill the input model with he values from the last post action if (TempData[GlobalConstants.ErrorsFromPOSTRequest] != null && TempData[GlobalConstants.InputModelFromPOSTRequestType]?.ToString() == nameof(EditProductInputModel)) { ModelStateHelper.MergeModelStates(TempData, this.ModelState); var inputModelJSON = TempData[GlobalConstants.InputModelFromPOSTRequest]?.ToString(); inputModel = JsonSerializer.Deserialize <EditProductInputModel>(inputModelJSON); var product = this.productService.GetProductById(productId, true); //Add categories names inputModel.CategoriesNames = new List <string>(); var categoriesNames = this.categoryService.GetCategoryNamesFromIds(inputModel.CategoriesIds); inputModel.CategoriesNames.AddRange(categoriesNames); //Set the image path for all of the modifiedImages if (inputModel.MainImageUploadInfo.ModifiedImage.Id != null) { inputModel.MainImageUploadInfo.ModifiedImage.Path = this.productImageService.GetImageById(inputModel.MainImageUploadInfo.ModifiedImage.Id).Image; } //Set isBeingModified param for mainImageUpload to false inputModel.MainImageUploadInfo.IsBeingModified = false; foreach (var additionalImageUploadInfo in inputModel.AdditionalImagesUploadsInfo) { var modifiedImage = additionalImageUploadInfo.ModifiedImage; if (modifiedImage.Id != null) { modifiedImage.Path = this.productImageService.GetImageById(modifiedImage.Id).Image; } //Set isBeingModified param for additionalImageUpload to false additionalImageUploadInfo.IsBeingModified = false; } } //If there wasn't an error with the edit form prior to this, just fill the inputModel like normal else { var product = this.productService.GetProductById(productId, true); inputModel = this.mapper.Map <EditProductInputModel>(product); //Get the categories for the product var productCategories = this.categoryService.GetCategoriesForProduct(product.Id).ToList(); inputModel.CategoriesIds = productCategories.Select(x => x.Id).ToList(); inputModel.CategoriesNames = productCategories.Select(x => x.Name).ToList(); //Set image mode inputModel.ImagesAsFileUploads = false; //Get the main image var mainImage = this.productImageService.GetMainImage(productId); //Set main image path inputModel.MainImage = mainImage.Image; //Set main image upload info inputModel.MainImageUploadInfo = new EditProductImageUploadInputModel { ImageUpload = null, ModifiedImage = new ImageIdAndPathInputModel { Id = mainImage.Id, Path = mainImage.Image } }; //Get additional images var additionalImages = this.productImageService.GetAdditionalImages(productId); inputModel.AdditionalImages = new List <string>(); inputModel.AdditionalImagesUploadsInfo = new List <EditProductImageUploadInputModel>(); //Sed additional images uploads and paths for (int i = 0; i < additionalImages.Count; i++) { var image = additionalImages[i]; inputModel.AdditionalImages.Add(image.Image); inputModel.AdditionalImagesUploadsInfo.Add(new EditProductImageUploadInputModel { ImageUpload = null, ModifiedImage = new ImageIdAndPathInputModel { Id = image.Id, Path = image.Image } }); } } //Set input model short description inputModel.ShortDescription = this.productService.GetShordDescription(inputModel.Description, 40); return(this.View(inputModel)); }