public async Task <IActionResult> Create(CreateProductInputModel productModel) { if (!this.ModelState.IsValid) { var allCategories = await this.productService.GetAllCategories().ToListAsync(); this.ViewData["categories"] = allCategories.Select(category => new CreateCategoryInputModel { Name = category.Name, }) .ToList(); return(this.View()); } string pictureUrl = await this.cLoudinaryService.UploadPicture(productModel.Picture, productModel.Name); var product = new CreateProductServiceModel { Name = productModel.Name, Weight = productModel.Weight, Price = productModel.Price, Description = productModel.Description, Category = productModel.Category, Picture = pictureUrl, Brand = productModel.Brand, }; await this.productService.Create(product); return(this.Redirect("/")); }
public async Task <IActionResult> Create(CreateProductInputModel input) { if (!this.ModelState.IsValid) { input.CategoriesItems = this.categoriesService.GetAllAsKeyValuePairs(); return(this.View(input)); } // var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; var user = await this.userManager.GetUserAsync(this.User); try { await this.productsService.CreateAsync(input, user.Id, $"{this.environment.WebRootPath}/images"); } catch (Exception e) { this.ModelState.AddModelError(string.Empty, e.Message); input.CategoriesItems = this.categoriesService.GetAllAsKeyValuePairs(); return(this.View(input)); } this.TempData["Message"] = "Product added successfully."; // return this.Json(input); return(this.RedirectToAction(nameof(this.All), "Products", new { area = string.Empty })); }
public IActionResult CreateProduct() { var viewModel = new CreateProductInputModel(); viewModel.Categories = this.getCategoriesService.GetAsStringEnumerable(); return(this.View(viewModel)); }
public IActionResult Create() { var viewModel = new CreateProductInputModel(); viewModel.CategoriesItems = this.categoriesService.GetAllAsKeyValuePairs(); return(this.View(viewModel)); }
public async Task <bool> CreateProductAsync(CreateProductInputModel input) { var product = new Product() { Name = input.Name, Description = input.Description, Price = input.Price, CategoryName = input.CategoryName, CategoryType = input.CategoryType, Sizes = input.Sizes, Colors = input.Colors, CreatedOn = DateTime.Now, IsDeleted = false }; if (product != null && product.Price > 0) { var pics = this.pictureRepository.All().Where(x => x.ProductId == product.Id).ToList(); product.Pictures = pics; this.productRepository.Add(product); await this.productRepository.SaveChangesAsync(); return(true); } throw new InvalidOperationException(GlobalConstants.CreateProductError); }
public async Task CreateAsync(CreateProductInputModel input) { var uploadedImage = this.cloudinaryService.UploadAsync(input.ImageFile); var imageUrl = uploadedImage.Result; var image = new Image { Url = imageUrl, }; var product = new Product { Name = input.Name.ToUpper(), Image = image, Description = input.Description, Price = input.Price, CategoryProductId = input.CategoryProductId, }; await this.imageRepository.AddAsync(image); await this.imageRepository.SaveChangesAsync(); await this.productsRepository.AddAsync(product); await this.productsRepository.SaveChangesAsync(); }
public IActionResult Create(CreateProductInputModel model) { if (!this.ModelState.IsValid) { return(this.Redirect("/Products/Create")); } this.productService.CreateProduct(model.Name, model.Price); return(this.Redirect("/")); }
public async Task <ActionResult> CreateProduct([FromBody] CreateProductInputModel inputModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var product = await this.productServices.CreateProductAsync(inputModel); return(CreatedAtAction(nameof(GetProductById), new { id = product.Id }, product)); }
public async Task <IActionResult> Create(CreateProductInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var id = await this.storeService.Create(input.CategoryId, input.Content, input.Title, input.Price, input.ImgUrl); return(RedirectToAction("Details", new { id = id })); }
public async Task <IActionResult> CreateProduct(CreateProductInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var id = await this.productsService.CreateProductAsync(input); return(this.Redirect($"/Administration/Img/ImgUpload?id={id}")); }
public async Task CreateAsync(CreateProductInputModel input, string userId, string imagePath) { var product = new Product { CategoryId = input.CategoryId, Description = input.Description, Name = input.Name, Price = input.Price, Availability = input.Availability, AddedByUserId = userId, }; foreach (var inputIngredient in input.Materials) { var material = this.materialsRepository.All().FirstOrDefault(x => x.Name == inputIngredient.MaterialName); if (material == null) { material = new Material { Name = inputIngredient.MaterialName }; } product.Materials.Add(new ProductMaterial { Material = material, }); } // /wwwroot/images/products/jhdsi-343g3h453-=g34g.jpg Directory.CreateDirectory($"{imagePath}/products/"); foreach (var image in input.Images) { var extension = Path.GetExtension(image.FileName).TrimStart('.'); if (!this.allowedExtensions.Any(x => extension.EndsWith(x))) { throw new Exception($"Invalid image extension {extension}"); } var dbImage = new Image { AddedByUserId = userId, Extension = extension, }; product.Images.Add(dbImage); var physicalPath = $"{imagePath}/products/{dbImage.Id}.{extension}"; using Stream fileStream = new FileStream(physicalPath, FileMode.Create); await image.CopyToAsync(fileStream); } await this.productsRepository.AddAsync(product); await this.productsRepository.SaveChangesAsync(); }
public async Task <IActionResult> CreateProduct(CreateProductInputModel input) { if (!this.ModelState.IsValid) { input.Categories = this.getCategoriesService.GetAsStringEnumerable(); return(this.View(input)); } await this.productService.CreateAsync(input); return(this.Redirect("/")); }
public IActionResult Create(CreateProductInputModel createProduct) { if (!this.ModelState.IsValid) { // TODO: SAVE FORM RESULT return(this.View()); } this.productsService.CreateProduct(createProduct.Name, createProduct.Price); return(this.Redirect("All")); }
public IActionResult Create(CreateProductInputModel model) { if (!ModelState.IsValid) { return(this.RedirectToAction("Error", "Home")); } AddProductInputServiceModel serviceModel = this.mapper.Map <AddProductInputServiceModel>(model); this.productService.AddProduct(serviceModel); return(this.RedirectToAction("All")); }
public async Task<IActionResult> Create(CreateProductInputModel input) { if (!this.ModelState.IsValid) { input.CategoriesItems = this.categoryService.GetAllAsKeyValuePairs(); return this.View(input); } await this.productService.CreateAsync(input); return this.RedirectToAction(nameof(this.Index)); }
public async Task <IActionResult> Create(CreateProductInputModel model) { string pictureUrl = await this.cloudinaryService.UploadPictureAsync(model.Picture, model.Name); ProductServiceModel serviceModel = Mapper.Map <ProductServiceModel>(model); serviceModel.Picture = pictureUrl; await this.productService.Create(serviceModel); return(this.Redirect("/")); }
public async Task <IActionResult> Create(CreateProductInputModel model, List <IFormFile> files) { if (!this.ModelState.IsValid) { return(this.View(model)); } var id = await this.productsService.Create(model.CategoryId, model.Name, model.Description, model.Price); this.imagesService.UploadImagesToProduct(id, files); return(this.RedirectToAction("Details", new { id = id })); }
public IHttpResponse Create(CreateProductInputModel model) { var product = new Product { Name = model.Name, Price = model.Price, Picture = model.Picture, Barcode = model.Barcode }; this.Db.Products.Add(product); this.Db.SaveChanges(); return(this.Redirect("/Products/Details?id=" + product.Id)); }
public async Task <IActionResult> Create(CreateProductInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var id = await this.productsService.Create(input.CategoryId, input.Description, input.ShortDescription, input.BrandId, input.Model, input.ImagePath, input.ProductInformation, input.Stock, input.Name, input.Price); return(this.RedirectToAction("Details", new { id = id })); }
private CreateProductInputModel PrepareCreateProductInputModel() { var allCategories = this.categoryService.GetAllCategories <ProductCategoryViewModel>().ToList(); var categories = allCategories.Select(x => new SelectListItem() { Text = x.Name }).ToList(); var inputModel = new CreateProductInputModel() { Categories = categories }; return(inputModel); }
public async Task <bool> CreateAsync(CreateProductInputModel model) { var file = model.PictFormFiles; var productPictureId = string.Format(GlobalConstants.ProductPicture, model.Name); var product = this._mapper.Map <Product>(model); await this._dbContext.Products.AddAsync(product); await this._dbContext.SaveChangesAsync(); await this._pictureService.UploadPicturesAsync(model.PictFormFiles, product.GetType(), productPictureId, product.Id); return(true); }
public IActionResult Create([FromBody] CreateProductInputModel input) { var product = new Product { Name = input.Name, Price = input.Price, Type = (ProductType)Enum.Parse(typeof(ProductType), input.Type), ProductOffers = new List <ProductOffer>(), Duration = input.Duration }; _repository.Add(product); _repository.SaveChanges(); return(Ok(product.Id)); }
public async Task <Product> CreateProductAsync(CreateProductInputModel inputModel) { var product = new Product() { Name = inputModel.Name, Step = inputModel.Step, MaxPrincipal = inputModel.MaxPrincipal, MinPrincipal = inputModel.MinPrincipal, ProductTypeId = inputModel.ProductTypeId, }; await this.dbContext.AddAsync(product); await this.dbContext.SaveChangesAsync(); return(product); }
public IActionResult Edit(int id, [FromBody] CreateProductInputModel input) { var product = _repository.GetByID(id); if (product == null) { return(NotFound()); } product.Name = input.Name; product.Price = input.Price; product.Type = (ProductType)Enum.Parse(typeof(ProductType), input.Type); _repository.Update(product); _repository.SaveChanges(); return(Ok(product.Id)); }
public async Task <string> CreateProductAsync(CreateProductInputModel input) { var product = new Product { ImgPath = GlobalConstants.DefaultImgProduct, Price = input.Price, Title = input.Title, Category = input.Category, SubCategory = input.SubCategory, Description = input.Description, }; await this.productsRepository.AddAsync(product); await this.productsRepository.SaveChangesAsync(); return(product.Id); }
public async Task <IActionResult> Create(CreateProductInputModel model) { if (!ModelState.IsValid) { var categoryList = this._categoryService .GetAll(); ViewBag.ListOfCategories = categoryList; return(this.View(model)); } var product = await this._productService.CreateAsync(model); TempData[GlobalConstants.TempDataSuccessMessageKey] = GlobalConstants .ProductMessage .CreateProductSuccess; return(Redirect("/")); }
public IHttpResponse Create(CreateProductInputModel model) { if (!Enum.TryParse(model.Type, out ProductType type)) { return(this.BadRequestErrorWithView("Invalid type.")); } var product = new Product() { Description = model.Description, Name = model.Name, Price = model.Price, Type = type }; this.Db.Products.Add(product); this.Db.SaveChanges(); return(this.Redirect("/Products/Details?id=" + product.Id)); }
public async Task <IActionResult> Create(CreateProductInputModel inputModel) { if (!ModelState.IsValid) { inputModel = PrepareCreateProductInputModel(); return(this.View(inputModel)); } var user = await this.userManager.GetUserAsync(HttpContext.User); var product = new Product() { Name = inputModel.Name, Quantity = inputModel.Quantity, Price = inputModel.Price, Description = inputModel.Description, PublishDate = DateTime.UtcNow, Color = inputModel.Color, MarketplaceUserId = user.Id, }; var isProductAdded = await this.productService.AddProduct(product); if (!isProductAdded) { inputModel = PrepareCreateProductInputModel(); return(this.View(inputModel)); } var category = await this.categoryService.GetCategoryByName(inputModel.CategoryName); await this.productService.AddCategory(product.Id, category); var picturePath = await this.pictureService .SavePicture(product.Id, inputModel.Picture, GlobalConstants.DefaultPicturesPath); await this.productService.AddPicturePath(product.Id, picturePath); return(this.RedirectToAction(nameof(My))); }
public async Task <IActionResult> Post(CreateProductInputModel input) { //IFormFileCollection files = this.HttpContext.Request.Form.Files; if (!this.ModelState.IsValid) { return(this.Ok(this.ModelState.Values)); } //input.Photos = files; var result = await this.productService.CreateProductAsync(input); if (result) { return(this.Ok(result)); } return(this.BadRequest("Failed to create product")); }
public async Task <IActionResult> CreateAsync(CreateProductInputModel input) { if (!this.ModelState.IsValid) { input.CategoriesItems = this.categoriesService.GetAllAsKeyValuePairs(); return(this.View(input)); } try { await this.productsService.CreateAsync(input, $"{this.environment.WebRootPath}/images/Products"); } catch (Exception ex) { this.ModelState.AddModelError(string.Empty, ex.Message); return(this.View(input)); } return(this.Redirect("/")); }