Beispiel #1
0
        private void BTN_remove_Click(object sender, EventArgs e)
        {
            using (var db = new CrmAzureDbContext())
            {
                int val;

                if (int.TryParse(TB_idRemove.Text, out val)) //Check if entry is a valid id format else ignore it
                {
                    //Check if entry exists else just ignore the input
                    var flag = (db.Customer.Where(p => p.Id == int.Parse(TB_idRemove.Text)).FirstOrDefault());
                    if (flag == null)
                    {
                        TB_idRemove.Text = String.Empty;
                        return;
                    }
                    else
                    {
                        db.Entry(flag).State = Microsoft.EntityFrameworkCore.EntityState.Detached;
                    }
                    var customer = new Model.Customer()
                    {
                        Id = int.Parse(TB_idRemove.Text)
                    };
                    db.Customer.Attach(customer);
                    db.Customer.Remove(customer);
                    db.SaveChanges();
                }
            }
            TB_idRemove.Text = String.Empty;
        }
Beispiel #2
0
        public List <OrderProduct> FindAllOrderProducts()
        {
            using CrmAzureDbContext appDb = new CrmAzureDbContext();
            List <OrderProduct> oproducts = appDb
                                            .OrderProduct
                                            .Select(p => p)
                                            .ToList();

            return(oproducts);
        }
Beispiel #3
0
        public List <Order> FindOrders(int CustId)
        {
            using CrmAzureDbContext appDb = new CrmAzureDbContext();
            List <Order> orders = appDb
                                  .Order
                                  .Where(p => p.CustomerId == CustId)
                                  .ToList();

            return(orders);
        }
Beispiel #4
0
        public List <Product> FindAllProducts()
        {
            using CrmAzureDbContext appDb = new CrmAzureDbContext();
            List <Product> products = appDb
                                      .Product
                                      .Select(p => p)
                                      .ToList();

            return(products);
        }
Beispiel #5
0
        public List <int> FindCustIds()
        {
            using CrmAzureDbContext appDb = new CrmAzureDbContext();
            List <int> ids = appDb
                             .Customer
                             .Select(p => p.Id)
                             .ToList();

            return(ids);
        }
Beispiel #6
0
        public List <Order> FindAll()
        {
            using CrmAzureDbContext appDb = new CrmAzureDbContext();
            List <Order> orders = appDb
                                  .Order
                                  .Select(p => p)
                                  .ToList();

            return(orders);
        }
Beispiel #7
0
        public List <Model.Customer> FindAll(int offset, int pageSize)
        {
            using CrmAzureDbContext appDb = new CrmAzureDbContext();
            List <Customer> customers = appDb
                                        .Customer
                                        .Select(p => p)
                                        .Skip(offset)
                                        .Take(pageSize)
                                        .ToList();

            return(customers);
        }
Beispiel #8
0
        private void BTN_addOrdProd_Click(object sender, EventArgs e)
        {
            int prId  = int.Parse(textBox2.Text);
            int ordId = int.Parse(textBox1.Text);

            using (var db = new CrmAzureDbContext())
            {
                var orderproduct = new Model.OrderProduct()
                {
                    ProductId = prId,
                    OrderId   = ordId
                };
                db.OrderProduct.Add(orderproduct);
                db.SaveChanges();
            }
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
        }
Beispiel #9
0
        private void BTN_AddCust_Click(object sender, EventArgs e)
        {
            string name  = TB_name.Text;
            string lname = TB_lname.Text;
            string ia    = CB_IA.Checked.ToString();

            using (var db = new CrmAzureDbContext())
            {
                var customer = new Model.Customer()
                {
                    FirstName = name,
                    LastName  = lname,
                    IsActive  = ia == "False" ? false : true
                };
                db.Customer.Add(customer);
                db.SaveChanges();
            }
            TB_name.Text  = String.Empty;
            TB_lname.Text = String.Empty;
            CB_IA.Checked = false;
        }
Beispiel #10
0
        private void BTN_addProduct_Click(object sender, EventArgs e)
        {
            string  name  = TB_name.Text;
            string  code  = TB_code.Text;
            decimal price = decimal.Parse(TB_price.Text);

            using (var db = new CrmAzureDbContext())
            {
                var products = new Product()
                {
                    Name  = name,
                    Code  = code,
                    Price = price
                };
                db.Product.Add(products);
                db.SaveChanges();
            }
            TB_name.Text  = String.Empty;
            TB_code.Text  = String.Empty;
            TB_price.Text = String.Empty;
        }
Beispiel #11
0
        private void BTN_addOrder_Click_1(object sender, EventArgs e)
        {
            string descr  = TB_descr.Text;
            var    date   = Convert.ToDateTime(dateTimePicker1.Text);
            string addr   = TB_addr.Text;
            int    custId = int.Parse(CB_custIds.Text);

            using (var db = new CrmAzureDbContext())
            {
                var orders = new Order()
                {
                    Description = descr,
                    Created     = date,
                    Address     = addr,
                    CustomerId  = custId
                };
                db.Order.Add(orders);
                db.SaveChanges();
            }
            TB_descr.Text   = String.Empty;
            TB_addr.Text    = String.Empty;
            CB_custIds.Text = String.Empty;
        }
Beispiel #12
0
        static void Main()
        {
            //using (var db = new CrmAzureDbContext())
            //{
            //    for (int i = 30; i < 40; i++)
            //    {
            //        Random rand = new Random();
            //        var customer = new Customer()
            //        {
            //            FirstName = "Customer" + i,
            //            LastName = "LastCustName" + i,
            //            IsActive = rand.Next(0, 2) == 0 ? false : true
            //        };
            //        db.Customer.Add(customer);
            //    }
            //    db.SaveChanges();
            //}

            //using CrmAzureDbContext appdb = new CrmAzureDbContext();
            //{
            //    var custs = appdb.OrderProduct
            //      .Where(a => a.ProductId == 1)
            //      .Include(a => a.Product)
            //      .FirstOrDefault(a => a.ProductId == 1);

            //    var x = custs.Product.Price;
            //}

            //using CrmAzureDbContext appdb = new CrmAzureDbContext();
            //{

            //    var x = appdb.Order
            //    .Include(a => a.Customer)
            //    .Where(a => a.CustomerId == a.Customer.Id)
            //    .OrderBy(a => a.CustomerId)
            //    .ToList();

            //    Debug.WriteLine(x.Where(a => a.CustomerId == 13).Count());

            //}

            using CrmAzureDbContext appdb = new CrmAzureDbContext();
            {
                //get the total orders of each customer
                var customers = appdb.Customer.ToList();

                foreach (var i in customers)
                {
                    var x = appdb.OrderProduct
                            .Include(a => a.Order)
                            .ThenInclude(b => b.Customer)
                            .Where(b => b.Order.CustomerId == i.Id)
                            .Select(a => a.Product.Price)
                            .Sum();
                    Debug.WriteLine(x, i.Id.ToString());
                }
                //foreach (var  a in x)
                //{
                //    var y = a.ProductId;
                //    var z = appdb.Product
                //      //  .Include( b => b.Price)
                //        .Where(b => b.Id == y)
                //        .Select(b => b.Price)
                //        .Sum();


                //    Debug.WriteLine(z);
                //}
            }



            //foreach(var oi in custs)
            //{

            //    Debug.WriteLine(oi.Id);
            //    Debug.WriteLine(oi.Orders.Where(a => a.CustomerId == oi.Id).Count());
            //}



            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainMenu());

            Console.ReadLine();
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            using (var db = new CrmAzureDbContext())
            {
                // --------Added 30 customers ---------------

                //for (int i = 0; i < 30; i++)
                //{
                //    var customer = new Customer()
                //    {
                //        FirstName = $"Firstname {i} ",
                //        LastName = $"Lastname {i} ",
                //        IsActive = true,
                //        Gross = 0

                //    };

                //    db.Add(customer);
                //---------------------------------------------


                //--------Added 10 Orders(with 1 Product in each) to random Customers--------------------

                //for (int i = 0; i < 10; i++)
                //{
                //    var randomCustomer = db.Customer
                //    .OrderBy(r => Guid.NewGuid())
                //    .Skip(0)
                //    .Take(1)
                //    .Include(c => c.Orders)
                //    .ThenInclude(o => o.Products)
                //    .SingleOrDefault();

                //    var order = new Order()
                //    {
                //        Description = $"This is Description number {i}",
                //        Created = DateTimeOffset.UtcNow,
                //        Address = $"Address number {i} ",
                //        Customer = randomCustomer,
                //        CustomerId = randomCustomer.Id,
                //    };

                //    Product product = new Product()
                //    {
                //        Name = "Huawei Y40",
                //        Price = 200,
                //        Code = Guid.NewGuid().ToString()
                //};
                //    List<OrderProduct> orderProducts = new List<OrderProduct>()
                //    {
                //        new OrderProduct()
                //        {
                //          Order = order,
                //          Product = product,
                //          OrderId = order.Id,
                //          ProductId = product.Id
                //        }
                //    };

                //    order.Products = orderProducts;
                //    randomCustomer.Gross += product.Price;
                //    randomCustomer.Orders.Add(order);
                //}

                // --------------------------------------------------------------------------

                //// Display Top 3 Customers
                //var TopGross = (from c in db.Customer
                //                orderby c.Gross descending
                //                select c).Take(3).ToList();

                //TopGross.ForEach(a => { Console.WriteLine($"Customer Name: {a.FirstName} Surname: {a.LastName} Total Gross: {a.Gross}"); }

                //);
                //Console.ReadLine();


                db.SaveChanges();
            };
        }