コード例 #1
0
        public IActionResult Put([FromRoute] int key, ProductModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (key != model.Product.Id)
            {
                return(BadRequest());
            }

            var product = _productService.GetById(key);

            if (product == null)
            {
                return(BadRequest(new ResponseModel()
                {
                    Message = "Product Not Found",
                    Result = false,
                    Status = 400
                }));
            }

            if (!String.IsNullOrEmpty(model.file))
            {
                string oldPath = product.ImageUrl;

                string imagePath = UploudFile.Base64ToImage(model.file.Split(",")[1], _env);
                model.Product.ImageUrl = imagePath;

                if (System.IO.File.Exists(_env.WebRootPath + "/img/" + oldPath))
                {
                    System.IO.File.Delete(_env.WebRootPath + "/img/" + oldPath);
                }
            }

            List <int> categoryIds = new List <int>();

            foreach (var item in model.Product?.ProductCategories)
            {
                categoryIds.Add(item.CategoryId);
            }

            _productService.Update(model.Product, categoryIds);
            return(Ok());
        }
コード例 #2
0
        public async Task <IActionResult> Post([FromBody] ProductModel entity)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            if (!String.IsNullOrEmpty(entity.file))
            {
                string imagePath = UploudFile.Base64ToImage(entity.file.Split(",")[1], _env);
                entity.Product.ImageUrl = imagePath;
            }


            await _productService.CreateAsync(entity.Product);

            return(Ok(entity));
        }