コード例 #1
0
        public IHttpActionResult AddProduct([FromBody] ProductForCreation productForCreation)
        {
            try
            {
                if (productForCreation == null)
                {
                    return(BadRequest());
                }

                var product = _mapper.Map <Product>(productForCreation);
                product.ImageUrl = new CreatePicture().Create(productForCreation.Picture);

                if (product.ImageUrl == null)
                {
                    return(BadRequest());
                }

                var result = ProductManager.AddProduct(product);

                if (result.Status == Persistance.RepositoryActionStatus.Created)
                {
                    return(Created(Request.RequestUri + "/" + result.Entity.Id.ToString(),
                                   _mapper.Map <ProductDto>(result.Entity)));
                }

                return(BadRequest());
            }
            catch (Exception)
            {
                return(InternalServerError());
            }
        }
コード例 #2
0
        public async Task <IActionResult> CreateProduct(ProductForCreation prodCreateDto)
        {
            /*
             * if(!User.FindFirst(ClaimTypes.Role).Value.Equals("admin")){
             *  return Unauthorized();
             * }
             */

            Product prod = new Product()
            {
                ProductName = prodCreateDto.ProductName,
                Details     = prodCreateDto.Details,
                UnitPrice   = prodCreateDto.UnitPrice,
                IsActive    = true
            };

            //mapper.Map(prodCreateDto,prod);
            repo.Add(prod);

            if (await repo.SaveAll())
            {
                return(Ok(prod.ProductId));
            }

            throw new Exception($"Updating user  failed to save");
        }
コード例 #3
0
        public async Task <IActionResult> CreateProduct(ProductForCreation productToCreationDto)
        {
            var productModel = _mapper.Map <Products>(productToCreationDto);
            await _onlineShoppingRepo.CreateProduct(productModel);

            var productToReturn = _mapper.Map <ProductToReturnDetails>(productModel);

            return(CreatedAtRoute(nameof(GetProduct), new { id = productToReturn.Id }, productToReturn));

            //    return Ok(productToReturn);
        }
コード例 #4
0
        public async Task <IActionResult> UpdateProduct(int id, ProductForCreation productDto)
        {
            try
            {
                var productToReturn = await _productServices.UpdateProduct(id, productDto);

                if (productToReturn == 0)
                {
                    return(new BadRequestObjectResult(new { Message = "Có lỗi khi cập nhật sản phẩm" }));
                }
                return(Ok(new { Message = "Cập nhật sản phẩm thành công!" }));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(new { Message = ex.Message.ToString() }));
            }
        }
コード例 #5
0
        public async Task <IActionResult> CreateProduct(ProductForCreation productDto)
        {
            try
            {
                var product = await _productServices.CreateProduct(productDto);

                if (product == null)
                {
                    return(new BadRequestObjectResult(new { Message = "Tạo sản phẩm thất bại" }));
                }
                return(Ok(product));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(new { Message = ex.Message.ToString() }));
            }
        }
コード例 #6
0
        public async Task <ProductForDetail> CreateProduct(ProductForCreation productDto)
        {
            var checkUrlSeoExist = _db.Products.Where(p => p.UrlSeo == productDto.UrlSeo);

            if (checkUrlSeoExist == null)
            {
                //add product in db
                Product productToReturn = new Product
                {
                    Id             = productDto.Id,
                    ProductName    = productDto.ProductName,
                    Price          = productDto.Price,
                    CategoryId     = productDto.CategoryId,
                    PhotoUrl       = productDto.PhotoUrl,
                    Description    = productDto.Description,
                    ProductDetails = productDto.ProductDetails,
                    Weight         = productDto.Weight,
                    Company        = productDto.Company,
                    UrlSeo         = productDto.UrlSeo
                };
                _db.Add(productToReturn);
                await _db.SaveChangesAsync();

                return(new ProductForDetail
                {
                    Id = productToReturn.Id,
                    ProductName = productToReturn.ProductName,
                    CategoryId = productToReturn.CategoryId,
                    Price = productToReturn.Price,
                    Description = productToReturn.Description,
                    ProductDetails = productToReturn.ProductDetails,
                    Weight = productToReturn.Weight,
                    Company = productToReturn.Company,
                    UrlSeo = productToReturn.UrlSeo,
                    Photos = null
                });
            }
            return(null);
        }
コード例 #7
0
        public async Task <int> UpdateProduct(int id, ProductForCreation productDto)
        {
            var product = await _db.Products
                          .Include(p => p.Photos)
                          .FirstOrDefaultAsync(u => u.Id == id);       // 1

            if (product == null)
            {
                return(0);
            }
            if (productDto != null)
            {
                product.ProductName    = productDto.ProductName;
                product.CategoryId     = productDto.CategoryId;
                product.Price          = productDto.Price;
                product.Description    = productDto.Description;
                product.ProductDetails = productDto.ProductDetails;
                product.PhotoUrl       = productDto.PhotoUrl;
                _db.Products.Update(product);
                await _db.SaveChangesAsync();
            }
            return(1);
        }