예제 #1
0
        public void CreateProduct(Product p)
        {
            // TODO check if varenr og navn eksisterer i forvejen
            // if check here


            if (p.Supplier == null)
            {
                // Supplier or any of suppliers properties are null
                throw new ArgumentNullException("Some supplier information is missing");
            }

            if (p.Supplier.Id != 0)
            {
                // If the supplier is not 0, that means the Supplier was selected from the dropdownlist

                // Set the SupplierId on the Product
                p.SupplierId = p.Supplier.Id;
            }
            else
            {
                // The supplier id is zero, which means it was typed in and not selected
                // should now check if a supplier already exists with that name or create a new one

                // Check if the Supplier already exists in the list
                if (SupplierList.Where(s => s.Id.Equals(p.SupplierId)).Count() > 0)
                {
                    // Exists in the list update the Supplier instead
                    UpdateSupplier(p.Supplier);
                    new MessageDialog("Updating Supplier").ShowAsync();
                }
                else
                {
                    // Does not exist in the list, create a new one
                    CreateSupplier(p.Supplier);
                    new MessageDialog("Creating Supplier").ShowAsync();
                }
            }

            try
            {
                // Add in DB1
                PersistencyService.InsertProductAsync(p);

                // Add to ProductList
                ProductList.Add(p);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }
예제 #2
0
        /// <summary>
        /// Takes a product (p) as argument
        /// it checks if the product has a supplier and then it will call a method from PersistencyService
        /// and add the new product in our database.
        /// </summary>
        /// <param name="p">P is the variable we use for our product object</param>
        public void CreateProduct(Product p)
        {
            // Check if product name already exists in the collection
            if (ProductList.Any(product => product.Name.Equals(p.Name)))
            {
                throw new ArgumentException("Kan ikke oprette produkt. Produkt med dette navn findes i forvejen");
            }
            // Check if product ItemNr already exists in the collection
            if (ProductList.Any(product => product.ItemNr.Equals(p.ItemNr)))
            {
                throw new ArgumentException("Kan ikke oprette produkt. Produkt med dette Vare Nr. findes i forvejen");
            }

            if (p.Supplier == null)
            {
                // Supplier or any of suppliers properties are null
                throw new ArgumentNullException("Leverandør mangler");
            }

            // Instantiate a Supplier from the Product.Supplier
            Supplier pSupplier = p.Supplier;



            if (p.Supplier.Id != 0)
            {
                // If the supplier is not 0, that means the Supplier was selected from the dropdownlist

                // Set the SupplierId on the Product
                p.SupplierId = p.Supplier.Id;

                // HACK: Set Supplier to Null again, because the database sends an error
                //		 if we pass an entire Supplier with the Product
                p.Supplier = null;
            }
            else
            {
                // The supplier id is zero, which means it was typed in and not selected

                // Check if values are null
                if (p.Supplier.Name == null ||
                    p.Supplier.Address == null ||
                    p.Supplier.Phone == null)
                {
                    throw new ArgumentException("Leverandørens oplysninger mangler at blive udfyldt");
                }

                // should now check if a supplier already exists with that name or create a new one

                // Check if the Supplier already exists in the list
                if (SupplierList.Where(s => s.Id.Equals(p.SupplierId)).Count() > 0)                 //TODO Virker ikke endnu
                {
                    // Exists in the list update the Supplier instead
                    UpdateSupplier(p.Supplier);
                }
                else
                {
                    // Does not exist in the list, create a new one
                    CreateSupplier(p.Supplier);
                }
            }

            try
            {
                // Add to db, returns id if succesful, returns 0 if fail
                p.Id = PersistencyService.InsertProductAsync(p).Result;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                throw;
            }

            // Set the Product.Supplier to the instantiated Supplier
            p.Supplier = pSupplier;

            // Add to ProductList
            ProductList.Add(p);
        }