public async Task <IActionResult> CreateProduct(ProductForCreateDto productForCreateDto)
        {
            try
            {
                var product = new Product
                {
                    CreatedAt            = productForCreateDto.CreatedAt,
                    Descirption          = productForCreateDto.Descirption,
                    HasDiscount          = productForCreateDto.HasDiscount,
                    PercentageOfDiscount = productForCreateDto.PercentageOfDiscount,
                    Photos      = productForCreateDto.Photos,
                    ProductName = productForCreateDto.ProductName,
                    Price       = productForCreateDto.Price,
                    CategoryId  = productForCreateDto.CategoryId
                };

                await _productRepo.Create(product);

                return(StatusCode(201));
            }
            catch (Exception)
            {
                return(BadRequest("can not create the product"));
            }
        }
        public async Task <IActionResult> UpdateProduct(int productId, ProductForCreateDto productForCreateDto)
        {
            try
            {
                var product = new Product
                {
                    CategoryId           = productForCreateDto.CategoryId,
                    OrderId              = productForCreateDto.OrderId,
                    CreatedAt            = productForCreateDto.CreatedAt,
                    HasDiscount          = productForCreateDto.HasDiscount,
                    PercentageOfDiscount = productForCreateDto.PercentageOfDiscount,
                    Photos      = productForCreateDto.Photos,
                    Price       = productForCreateDto.Price,
                    ProductName = productForCreateDto.ProductName,
                    Descirption = productForCreateDto.Descirption,
                };
                await _productRepo.Update(product, productId);

                return(Ok(product));
            }
            catch
            {
                return(BadRequest("can not update the product"));
            }
        }
        public async Task <IActionResult> Create([FromForm] ProductForCreateDto product)
        {
            if (product == null)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(HttpContext.User);

                try
                {
                    var productEntity = new Product(product.ProductName, product.Description, product.Style,
                                                    product.Brand,
                                                    product.Url, product.ProductType, product.ShippingPriceCents, product.Note, user);

                    productEntity = await _productRepository.AddProductAsync(productEntity);

                    var success = await _productRepository.SaveChanges();

                    await _cachingProductStore.InvalidateCache(productEntity.Id, user.Id);

                    _flashMessage.Info($"Successfully saved product: {productEntity.ProductName}");
                    return(RedirectToAction(nameof(GetProductsForUser)));
                }
                catch (DbUpdateException)
                {
                    ModelState.AddModelError("", "Unable to save changes.");
                }
            }
            return(View());
        }
Exemple #4
0
        public async Task <ActionResult <ProductDto> > CreateProduct([FromBody] ProductForCreateDto product)
        {
            _orm.OpenConn();
            var productFromDB = _mapper.Map <Product>(product);

            productFromDB = await _orm.CreateProduct(productFromDB);

            var productDto = _mapper.Map <ProductDto>(productFromDB);
            await _orm.CloseConn();

            return(CreatedAtRoute("GetProductByID", new { productID = productDto.Id }, productDto));
        }
        public async Task <IActionResult> AddProduct([FromBody] ProductForCreateDto productForCreateDto)
        {
            var product = _mapper.Map <Product>(productForCreateDto);
            var brand   = await _repo.GetBrandById(productForCreateDto.BrandId);

            brand.Products.Add(product);
            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetProductById", new { Controller = "Product", id = product.Id }, product));
            }
            return(BadRequest("Could not add new product"));
        }
Exemple #6
0
        public async Task <IActionResult> AddProduct([FromBody] ProductForCreateDto productForCreateDto)
        {
            var product = _mapper.Map <Product>(productForCreateDto);
            await _productRepositroy.AddProduct(product);

            await _productRepositroy.SaveAsync();

            var productResult = _mapper.Map <ProductDto>(product);

            return(CreatedAtRoute("GetProductById",
                                  new { ProductId = productResult.Id },
                                  productResult));
        }
Exemple #7
0
        public async Task <IActionResult> Create(ProductForCreateDto productForCreateDto)
        {
            //validate request
            if (await _productRepo.ProductExists(productForCreateDto.Name))
            {
                return(BadRequest("Product already exists"));
            }

            var productToCreate = _mapper.Map <Product>(productForCreateDto);
            var createdProduct  = await _productRepo.Add(productToCreate);

            await _hub.Clients.All.SendAsync("transferproductdata", ProductStore.Instance.Products);

            return(StatusCode(201));
        }
Exemple #8
0
        public async Task <IActionResult> CreateProduct([FromBody] ProductForCreateDto product)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogError($"Invalid model state for the ProductForCreateDto object");
                return(UnprocessableEntity(ModelState));
            }
            if (product == null)
            {
                _logger.LogError("ProductForCreateDto object sent from client is null. ");
                return(BadRequest("ProductForCreateDto object is null"));
            }
            var productEntity = _mapper.Map <Product>(product);
            await _serviceManager.Product.CreateProductAsync(productEntity);

            await _serviceManager.SaveAsync();

            var productToReturn = _mapper.Map <ProductDto>(productEntity);

            return(CreatedAtRoute("ProductById", new { id = productToReturn.Id }, productToReturn));
        }