Esempio n. 1
0
        /// <summary>
        /// Replaces a single Product with a new full set of values
        /// </summary>
        /// <param name="replacedProduct">The new data for the Product you wish to update</param>
        /// <returns>Returns a result indicating if the operation succeeded</returns>
        public async Task <Result> ReplaceVenueProduct(UpdatedProduct replacedProduct)
        {
            try
            {
                using var con = new Npgsql.NpgsqlConnection(settings.Connection.DatabaseConnectionString);
                await con.ExecuteAsync(
                    @"UPDATE ""Product""
            SET
            productTypeId = @ProductTypeId,
            productName = @ProductName,
            productDescription = @ProductDescription,
            price = @Price,
            image = @Image,
            ageRestricted = @AgeRestricted,
            parentProductId = @ParentProductId
            WHERE venueId = @VenueId AND productId = @ProductId",
                    replacedProduct
                    ).ConfigureAwait(false);

                return(Result.Ok());
            }
            catch (Exception ex)
            {
                return(Result.Fail(ex.ToString()));
            }
        }
Esempio n. 2
0
 public string UpdateProduct(UpdatedProduct product, int productId)
 {
     if (ModelState.IsValid)
     {
         Product productSelectedForUpdate = dbContext.Products.Include(x => x.Vendor).FirstOrDefault(u => u.ProductId == productId);
         productSelectedForUpdate.Name        = product.name;
         productSelectedForUpdate.Quantity    = product.quantity;
         productSelectedForUpdate.Description = product.description;
         productSelectedForUpdate.Category    = product.category;
         productSelectedForUpdate.Price       = product.price;
         dbContext.SaveChanges();
         var message = new { message = "Product successfully updated!" };
         return(JsonConvert.SerializeObject(message));
     }
     else
     {
         var error = new { error = "Error Model State is not valid!" };
         return(JsonConvert.SerializeObject(error));
     }
 }
Esempio n. 3
0
        public async Task <IActionResult> ReplaceVenueProduct([FromRoute(Name = "venueId")] int venueId, [FromBody] DtoReplaceProductRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var product = new UpdatedProduct
            {
                VenueId            = venueId,
                ProductId          = request.ProductId,
                ProductTypeId      = request.ProductTypeId,
                ProductName        = request.ProductName,
                ProductDescription = request.ProductDescription,
                Price           = request.Price,
                Image           = request.Image,
                AgeRestricted   = request.AgeRestricted,
                ParentProductId = request.ParentProductId,
            };

            return(await products.ReplaceVenueProduct(product)
                   .OnBoth(p => p.IsFailure ? StatusCode(500) : StatusCode(200))
                   .ConfigureAwait(false));
        }
Esempio n. 4
0
        public void UpdateProductButton(object sender, RoutedEventArgs e)
        {
            if (nameProduct.Text.Length == 0)
            {
                MessageBox.Show("норм вводи");
                return;
            }
            else if (!CheckName(nameProduct.Text))
            {
                MessageBox.Show("продукта такого нет");
                return;
            }
            else
            {
                name = nameProduct.Text;
            }

            if (newNameProduct.Text.Length == 0 && newPriceProduct.Text.Length == 0)
            {
                MessageBox.Show("что то одно введи");
                return;
            }
            else if (newPriceProduct.Text.Length != 0 && newNameProduct.Text.Length == 0)
            {
                newPrice = int.Parse(newPriceProduct.Text);
                choice   = 1;
            }
            else if (newPriceProduct.Text.Length == 0 && newNameProduct.Text.Length != 0)
            {
                newName = newNameProduct.Text;
                choice  = 2;
            }
            else
            {
                newName  = newNameProduct.Text;
                newPrice = int.Parse(newPriceProduct.Text);
                choice   = 3;
            }


            using (var context = new ProductContext())
            {
                var            product = context.Products.Where(prod => prod.NameProduct.Contains(name)).FirstOrDefault();
                UpdatedProduct updatedProduct;
                switch (choice)
                {
                case 1:
                    updatedProduct = new UpdatedProduct
                    {
                        NameProduct     = product.NameProduct,
                        PriceProduct    = newPrice,
                        OldPriceProduct = product.PriceProduct
                    };
                    context.UpdatedProducts.Add(updatedProduct);
                    break;

                case 2:
                    updatedProduct = new UpdatedProduct
                    {
                        NameProduct    = newName,
                        PriceProduct   = product.PriceProduct,
                        OldNameProduct = product.NameProduct
                    };
                    context.UpdatedProducts.Add(updatedProduct);
                    break;

                case 3:
                    updatedProduct = new UpdatedProduct
                    {
                        PriceProduct    = newPrice,
                        OldPriceProduct = product.PriceProduct,
                        NameProduct     = newName,
                        OldNameProduct  = product.NameProduct
                    };
                    context.UpdatedProducts.Add(updatedProduct);
                    break;
                }

                context.SaveChanges();

                MessageBox.Show("продукт изменен");
            }
            Close();
        }