Beispiel #1
0
        public static Business_Response ValidateUpdateProduct(ProductEntity oProduct)
        {
            using var db = new InventoryContext();
            var products = db.Products.ToList();
            Business_Response response = new Business_Response();
            ProductEntity     cProduct = (ProductEntity)oProduct.Clone();

            //Nombre del producto.
            if (products.Where(p => (p.ProductId != cProduct.ProductId) && (p.ProductName == cProduct.ProductName)).Any())
            {
                response.Error = true;
                response.ErrorMessages.Add($"El nombre del producto {cProduct.ProductName} ya existe.");
            }

            if (cProduct.ProductName == null || cProduct.ProductName.Length < 2)
            {
                cProduct.ProductName = "";
                response.Error       = true;
                response.ErrorMessages.Add("El nombre del producto debe ser mayor a 1 caracter.");
            }

            if (cProduct.ProductName.Length > 100)
            {
                response.Error = true;
                response.ErrorMessages.Add("El nombre del producto debe ser menor a 100 caracteres.");
            }

            //Descripción del producto.
            if (cProduct.ProductDescription == null)
            {
                cProduct.ProductDescription = "";
            }

            if (cProduct.ProductDescription.Length > 600)
            {
                response.Error = true;
                response.ErrorMessages.Add("La descripción del producto debe ser menor a 600 caracteres.");
            }

            if (!db.Categories.Where(c => c.CategoryId == cProduct.CategoryId).Any())
            {
                response.Error = true;
                response.ErrorMessages.Add($"La referencia de la categoría {cProduct.CategoryId} no existe.");
            }

            return(response);
        }