示例#1
0
        public async Task <IActionResult> PostProduct([FromBody] ShopCommon.Models.Product product)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(ModelState));
            }

            //here send the user for create the new product:
            var user = await this.userHelper.GetUserByEmailAsync(product.User.UserName);

            if (user == null)
            {
                return(this.BadRequest("Invalid user"));
            }

            //Aqui subo la imagen al servidor:
            var imageUrl = string.Empty;

            if (product.ImageArray != null && product.ImageArray.Length > 0)
            {
                var stream   = new MemoryStream(product.ImageArray);
                var guid     = Guid.NewGuid().ToString();
                var file     = $"{guid}.jpg";
                var folder   = "wwwroot\\images\\Products";
                var fullPath = $"~/images/Products/{file}";
                var response = FileHelper.UpaloadPhoto(stream, folder, file);

                if (response)
                {
                    imageUrl = fullPath;
                }
            }


            var entityProduct = new Product
            {
                IsAvailable  = product.IsAvailable,
                LastPurchase = product.LastPurchase,
                LastSale     = product.LastSale,
                Name         = product.Name,
                Price        = product.Price,
                Stock        = product.Stock,
                User         = user,
                ImageUrl     = imageUrl,
            };

            var newProduct = await this.productRepository.CreateAsync(entityProduct);

            return(Ok(newProduct));
        }
示例#2
0
        public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] ShopCommon.Models.Product product)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(ModelState));
            }

            if (id != product.Id)
            {
                return(BadRequest());
            }

            var oldProduct = await this.productRepository.GetByIdAsync(id);

            if (oldProduct == null)
            {
                return(this.BadRequest("Product Id don't exists."));
            }

            //TODO: Upload Images


            //here update the oldPorduct with the new product from the product  mobile:
            oldProduct.IsAvailable  = product.IsAvailable;
            oldProduct.LastPurchase = product.LastPurchase;
            oldProduct.LastSale     = product.LastSale;
            oldProduct.Name         = product.Name;
            oldProduct.Price        = product.Price;
            oldProduct.Stock        = product.Stock;

            var updateProduct = await this.productRepository.UpdateAsync(oldProduct);

            //here return the product like this in the database:

            return(Ok(updateProduct));
        }