public Customer RegisterCustomer(string newUsername)
        {
            var pwd         = new Password().IncludeLowercase().IncludeUppercase().IncludeSpecial();
            var newPassword = pwd.Next();
            // TODO check if customer exists / create customer if so and redirect to product screen logged in?
            Customer x = this.FindByUsername(newUsername);

            if (x == null)
            {
                customerSet newCustomer = new customerSet
                {
                    username = newUsername,
                    password = newPassword,
                    balance  = new Random().Next(5, 105)
                };

                using (prac5_dbEntities db = new prac5_dbEntities())
                {
                    customerSet c = db.customerSets.Add(newCustomer);
                    db.SaveChanges();
                    return(new Customer
                    {
                        Id = c.Id,
                        UserName = c.username,
                        Balance = c.balance,
                        Password = c.password
                    });
                }
            }
            else
            {
                throw new FaultException <CustomerFaultService>(new CustomerFaultService("User bestaat al..."));
            }
        }
Exemple #2
0
        public bool Order(int customerId, int storeId, List <BuyingProduct> products)
        {
            orderSet Order = new orderSet {
                customer_Id = customerId,
                date        = DateTime.Now,
            };

            using (prac5_dbEntities db = new prac5_dbEntities())
            {
                customerSet customer = (from c in db.customerSets
                                        where c.Id == customerId
                                        select c).First();

                double price = (
                    from p2 in products
                    select p2.Amount * (
                        from p in db.productSets
                        where p.Id == p2.Id
                        select p.price
                        ).First()
                    ).Sum();

                int lowestStock = (
                    from p2 in products
                    select(
                        from p in db.productSets
                        where p.Id == p2.Id
                        select p.stock
                        ).First() - p2.Amount
                    ).Min();

                if (lowestStock < 0)
                {
                    throw new FaultException <OrderServiceFault>(new OrderServiceFault("Not enough Stok"));
                }

                if (price > customer.balance)
                {
                    throw new FaultException <OrderServiceFault>(new OrderServiceFault("Not enough balance to pay for this."));
                }

                customer.balance -= price;

                db.customerSets.AddOrUpdate(customer);
                orderSet SavedOrder = db.orderSets.Add(Order);

                foreach (BuyingProduct bp in products)
                {
                    productSet product = (from p in db.productSets
                                          where p.Id == bp.Id
                                          select p).First();

                    product.stock -= bp.Amount;

                    orderLineSet orderLine = new orderLineSet
                    {
                        amount     = bp.Amount,
                        order_Id   = SavedOrder.Id,
                        product_Id = bp.Id,
                    };

                    db.orderLineSets.Add(orderLine);
                    db.productSets.AddOrUpdate(product);
                }


                db.SaveChanges();
            }

            return(true);
        }