public void Create(Product product)
 {
     if (product == null || product.Name == null)
     {
         string errMessage = product == null ? "Product cannot be null." : "Product name cannot be null.";
         var    response   = HttpResponseFactory.ConstructResponse(HttpStatusCode.BadRequest, errMessage);
         throw new HttpResponseException(response);
     }
     _productsService.CreateProduct(product);
 }
Example #2
0
 public ActionResult PostProduct(ProductDto product)
 {
     try
     {
         product = _productConverter.FromDO(_productsService.CreateProduct(_productConverter.ToDO(product)));
         return(CreatedAtAction("GetProduct", new { id = product.Id }, product));
     } catch (InvalidArgumentException e)
     {
         return(BadRequest(e.Message));
     }
 }
        public IActionResult Create(ProductCreateViewModel model)
        {
            if (!this.AdminAuthorization())
            {
                return(RedirectToAction("/"));
            }

            Product product = service.CreateProduct(model);

            return(this.RedirectToAction("/Products/Details?=id=" + product.Id));
        }
Example #4
0
 public ActionResult CreateProduct([FromBody] Product products)
 {
     try
     {
         _productsService.CreateProduct(products);
         return(Ok(true));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #5
0
        public async Task <IActionResult> CreateProduct([FromBody] ProductTO product)
        {
            try
            {
                await _productsService.CreateProduct(product);

                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode(500, e));
            }
        }
        public IActionResult PostProduct([FromBody] Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (_productsService.CreateProduct(product))
            {
                return(Created($"/api/Products/{product.ProductID}", product));
            }

            return(BadRequest());
        }
        private async Task CreateProduct()
        {
            try
            {
                CreatedProduct = await productsService.CreateProduct(this);

                Window.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(JsonConvert.SerializeObject(ex), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                Log.Error(ex);
            }
        }
        public ActionResult AddProduct(ProductVM productvm, HttpPostedFileBase file)
        {
            int id;

            if (!ModelState.IsValid)
            {
                productvm.Categories = new SelectList(_categoryservice.GetAllCategories(), "Id", "Name");
                return(View(productvm));
            }
            else
            {
                Product product = new Product();
                product.Name        = productvm.Name;
                product.Description = productvm.Description;
                product.Price       = productvm.Price;

                Category selectedcategory = _categoryservice.GetAllCategories().Where(x => x.Id == productvm.CategoryId).FirstOrDefault();
                product.category     = selectedcategory;
                product.CategoryId   = selectedcategory.Id;
                product.CategoryName = selectedcategory.Name;
                if (file != null)
                {
                    product.ImageName = file.FileName;
                }
                _productservice.CreateProduct(product);

                TempData["SM"] = "You have added a product";

                id = product.Id;
                // Check if a file was uploaded
                if (file != null && file.ContentLength > 0)
                {
                    _imageProcessing.GenerateDirectoriesAndSaveImages(file, id);
                }
                else
                {
                    Category _selectedcategory = _categoryservice.GetAllCategories().Where(x => x.Id == productvm.CategoryId).FirstOrDefault();
                    productvm.Categories   = new SelectList(_categoryservice.GetAllCategories(), "Id", "Name");
                    productvm.category     = _selectedcategory;
                    productvm.CategoryId   = _selectedcategory.Id;
                    productvm.CategoryName = _selectedcategory.Name;

                    //ModelState.AddModelError("", "No image was not uploaded - something went wrong");
                    return(this.RedirectToAction <ShopController>(c => c.Products(1, null)));
                }
            }

            return(this.RedirectToAction <ShopController>(c => c.Products(1, null)));
        }
Example #9
0
        public async Task <IActionResult> CreateProduct(ProductModel model)
        {
            _productBusinessLogic.ApplyBusinessRules(model);

            try
            {
                var results = _productsService.CreateProduct(model);

                return(new JsonResult(results));
            }
            catch (NotFoundException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Example #10
0
        public IHttpResponse Create(ProductInputModel productInput)
        {
            if (GetUser().Role != UserRole.Admin)
            {
                return(this.Redirect("/"));
            }

            if (productInput.Barcode.Length != 12)
            {
                return(this.Error("Barcode shoud be 12 digits long!"));
            }

            productsService.CreateProduct(productInput);

            return(this.Redirect("/products/all"));
        }
        public async Task <ActionResult <ViewProduct> > CreateProduct(CreateProduct createProduct)
        {
            try
            {
                var product = await _productsService.CreateProduct(createProduct);

                return(Ok(product));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(StatusCode(500, new ApiResponse {
                    Result = 1, Message = ex.Message
                }));
            }
        }
Example #12
0
        public async Task <IActionResult> Create([FromBody] ProductViewModel productViewModel, CancellationToken cancellationToken)
        {
            try
            {
                var validator = new ProductValidator();
                await validator.ValidateAndThrowAsync(productViewModel, cancellationToken : cancellationToken);

                var product = _mapper.Map <Product>(productViewModel);

                await _productsService.CreateProduct(product, cancellationToken);
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }

            return(new StatusCodeResult(StatusCodes.Status201Created));
        }
Example #13
0
 public IActionResult CreateProduct(long foodId, [FromBody] ProductModel newProduct) //ActionResult<ProductModel>
 {
     try
     {
         if (!ModelState.IsValid)//Validates if Model restrictions are fullfiled
         {
             return(BadRequest(ModelState));
         }
         var createdProduct = _productService.CreateProduct(foodId, newProduct);
         return(Created($"api/foods/{foodId}/{createdProduct.Id}", createdProduct));
     }
     catch (NotFoundItemException ex)
     {
         return(BadRequest(ex.Message));
     }
     catch (Exception)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, "Something unexpected happened."));
     }
 }
Example #14
0
 public void Create(Product product)
 {
     _productsService.CreateProduct(product);
 }
Example #15
0
 public async Task <bool> CreateProduct(ProductDto product)
 {
     return(await _iProductsService.CreateProduct(product));
 }
Example #16
0
        public async Task <IActionResult> Post([FromBody] CreateAndUpdateProductDto product)
        {
            var id = await _service.CreateProduct(product);

            return(Ok(id));
        }
Example #17
0
 public async Task <ActionResult <ResponseMessage> > Post([FromBody] Product product)
 {
     return(await _productsService.CreateProduct(new CreateProductRequest { Product = product }));
 }
 public Task Post(Product product)
 {
     return(_productsService.CreateProduct(product));
 }