/// <summary>
        /// The add package payment transaction.
        /// </summary>
        /// <param name="packageId">
        /// The package id.
        /// </param>
        /// <returns>
        /// The <see cref="Tuple"/>.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// Balance cannot be negative.
        /// </exception>
        public Tuple<Package, decimal> AddPackagePaymentTransaction(int packageId)
        {
            using (var context = new ShopAnyWareSql())
            {
                var package = context.PackagesWithUserAndWallet().Single(p => p.Id.Equals(packageId));
                var newTransaction = Transaction.CreatePackagePaymentTransaction(package);
                var wallet = package.User.Wallet;
                wallet.PackagePayment(newTransaction.OperationAmount);
                var brokenRules = wallet.GetBrokenRules();
                if (brokenRules.Any())
                {
                    throw new InvalidOperationException("Wallet amount can't be negative");
                }

                package.ChangePackageStatus(PackageStatus.Paid);
                context.Packages.Attach(package);
                context.Entry(package).State = System.Data.EntityState.Modified;
                context.Transactions.Add(newTransaction);
                context.SaveChanges();
                return new Tuple<Package, decimal>(package, wallet.Amount);
            }
        }
        /// <summary>
        /// Add or update delivery address.
        /// </summary>
        /// <param name="email">
        /// The email.
        /// </param>
        /// <param name="address">
        /// The address.
        /// </param>
        /// <returns>
        /// The TdService.Model.Addresses.DeliveryAddress.
        /// </returns>
        public DeliveryAddress AddOrUpdateDeliveryAddress(string email, DeliveryAddress address)
        {
            if (email == null)
            {
                throw new ArgumentNullException("email");
            }

            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            using (var context = new ShopAnyWareSql())
            {
                var user = context.Users.Include("DeliveryAddresses").Include("Profile").Include("Wallet").Include("Roles").SingleOrDefault(u => u.Email == email);
                if (user == null)
                {
                    throw new InvalidUserException(ErrorCode.UserNotFound.ToString());
                }

                address.UserId = user.Id;
                var addressInDb = address.Id == 0 ? null : context.DeliveryAddresses.Find(address.Id);
                if (addressInDb == null)
                {
                    addressInDb = context.DeliveryAddresses.Add(address);
                    context.SaveChanges();

                    if (user.DeliveryAddresses == null)
                    {
                        user.DeliveryAddresses = new List<DeliveryAddress>();
                    }
                }
                else
                {
                    addressInDb.AddressName = address.AddressName;
                    addressInDb.FirstName = address.FirstName;
                    addressInDb.LastName = address.LastName;
                    addressInDb.Phone = address.Phone;
                    addressInDb.Region = address.Region;
                    addressInDb.State = address.State;
                    addressInDb.ZipCode = address.ZipCode;
                    addressInDb.AddressLine1 = address.AddressLine1;
                    addressInDb.AddressLine2 = address.AddressLine2;
                    addressInDb.AddressLine3 = address.AddressLine3;
                    addressInDb.CountryId = address.CountryId;

                    context.Entry(addressInDb).State = EntityState.Modified;
                    context.SaveChanges();
                }

                addressInDb.Country = context.Countries.Single(c => c.Id.Equals(addressInDb.CountryId));
                return addressInDb;
            }
        }
 /// <summary>
 /// Update item.
 /// </summary>
 /// <param name="item">
 /// The item.
 /// </param>
 public void UpdateItem(Item item)
 {
     using (var context = new ShopAnyWareSql())
     {
         context.Entry(item).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Exemple #4
0
        public void GivenIHaveTheFollowingOrders(Table table)
        {
            var user = ScenarioContext.Current.Get<User>();
            using (var context = new ShopAnyWareSql())
            {
                var orders = table.CreateSet<Order>().Select(o => { context.Entry(o).State = EntityState.Added; return o; });
                if (user.Orders == null)
                {
                    user.Orders = new List<Order>();
                }

                user.Orders.AddRange(orders);
                context.Entry(user).State = EntityState.Modified;
                context.SaveChanges();

                ScenarioContext.Current.Set(user);
            }
        }
Exemple #5
0
        public void GivenIHaveTheFollowingDeliveryAddresses(Table table)
        {
            var addresses = table.CreateSet<DeliveryAddress>();
            var user = ScenarioContext.Current.Get<User>();
            using (var context = new ShopAnyWareSql())
            {
                context.Wallets.Attach(user.Wallet);
                context.Profiles.Attach(user.Profile);
                context.Users.Attach(user);
                if (user.DeliveryAddresses == null)
                {
                    user.DeliveryAddresses = new List<DeliveryAddress>();
                }

                var addedAddresses = addresses.Select(deliveryAddress => context.DeliveryAddresses.Add(deliveryAddress)).ToList();
                context.SaveChanges();

                user.DeliveryAddresses.AddRange(addedAddresses.ToList());
                context.Entry(user).State = EntityState.Modified;
                context.SaveChanges();

                ScenarioContext.Current.Set(user);
            }
        }
        /// <summary>
        /// Remove an order.
        /// </summary>
        /// <param name="email">
        /// The email.
        /// </param>
        /// <param name="orderId">
        /// The order ID to remove.
        /// </param>
        /// <returns>
        /// The TdService.Model.Orders.Order.
        /// </returns>
        public Order RemoveOrder(string email, int orderId)
        {
            using (var context = new ShopAnyWareSql())
            {
                var user = context.Users.Include("Profile").Include("Wallet").Include("Orders").Include("Roles").SingleOrDefault(u => u.Email == email);
                if (user == null)
                {
                    throw new ArgumentNullException(ErrorCode.UserNotFound.ToString());
                }

                var order = user.RemoveOrder(orderId);
                context.Orders.Attach(order);
                //// TODO : make a cascading delete in the database
                context.Entry(order).Collection(o => o.Items).Load();
                context.Orders.Remove(order);
                context.SaveChanges();
                return order;
            }
        }
 /// <summary>
 /// Update the order.
 /// </summary>
 /// <param name="order">
 /// The order to update.
 /// </param>
 /// <returns>
 /// The TdService.Model.Orders.Order.
 /// </returns>
 public Order UpdateOrder(Order order)
 {
     using (var context = new ShopAnyWareSql())
     {
         context.Orders.Attach(order);
         context.Entry(order).State = EntityState.Modified;
         context.SaveChanges();
         return order;
     }
 }
        /// <summary>
        /// Add order.
        /// </summary>
        /// <param name="email">
        /// The email.
        /// </param>
        /// <param name="order">
        /// The order to add.
        /// </param>
        /// <returns>
        /// The TdService.Model.Orders.Order.
        /// </returns>
        public Order AddOrder(string email, Order order)
        {
            using (var context = new ShopAnyWareSql())
            {
                var user = context.Users.Include("Profile").Include("Wallet").Include("Roles").Include("Orders")
                    .SingleOrDefault(u => u.Email == email);
                if (user == null)
                {
                    throw new ArgumentNullException(ErrorCode.UserNotFound.ToString());
                }

                order.Retailer = context.Retailers.SingleOrDefault(r => r.Url == order.Retailer.Url)
                               ?? context.Retailers.Add(order.Retailer);
                context.Orders.Add(order);
                if (user.Orders == null)
                {
                    user.Orders = new List<Order>();
                }

                user.AddOrder(order);
                context.Entry(user).State = EntityState.Modified;
                context.SaveChanges();

                return order;
            }
        }