示例#1
0
        public Order CreateOrder(OrderModel order)
        {
            var user = _context.Users.Single(u => u.ID == order.User.ID);

            Order newOrder = new()
            {
                ID             = Guid.NewGuid(),
                Status         = "Mottagen",
                OrderDate      = DateTime.Now,
                ShipmentChoice = order.ShipmentChoice,
                PaymentChoice  = order.PaymentChoice,
                Address        = $"{order.ShippingAddress.Street}, {order.ShippingAddress.PostalCode}, {order.ShippingAddress.City}"
            };

            newOrder.OrderNumber = $"{newOrder.ID}{newOrder.OrderDate}".GetHashCode().ToString();

            if (newOrder.OrderNumber.First() == '-')
            {
                newOrder.OrderNumber = newOrder.OrderNumber.Remove(0, 1);
            }

            user.Orders.Add(newOrder);
            var returnOrder = _context.Orders.Add(newOrder);

            _context.SaveChanges();

            return(returnOrder.Entity);
        }
示例#2
0
        public void UpdateAddress(Address updatedAddress, Guid AddressID)
        {
            var addressToUpdate = _context.Addresses.Single(a => a.ID == AddressID);

            addressToUpdate.City       = updatedAddress.City;
            addressToUpdate.Country    = updatedAddress.Country;
            addressToUpdate.PostalCode = updatedAddress.PostalCode;
            addressToUpdate.State      = updatedAddress.State;
            addressToUpdate.Street     = updatedAddress.Street;
            _context.SaveChanges();
        }
示例#3
0
        public void CreateUser(UserRegisterModel model)
        {
            var salt = PasswordHasher.SaltGenerator();

            User user = new User
            {
                Role         = "Customer",
                FirstName    = model.FirstName,
                LastName     = model.LastName,
                EmailAddress = model.Email,
                Salt         = salt,
                Password     = PasswordHasher.HashPassword(model.Password, salt),
                RegisterDate = DateTime.Now
            };

            _context.Users.Add(user);
            _context.SaveChanges();
        }
 public void CommitTransaction()
 {
     if (_transaction != null)
     {
         _context.SaveChanges();
         _transaction.Commit();
     }
     _transaction = null;
 }
示例#5
0
        public TEntity Add(TEntity entity, bool autoSaveChanges = true)
        {
            TEntity _entity = Entities.Add(entity);

            if (autoSaveChanges)
            {
                _context.SaveChanges();
            }
            return(_entity);
        }
示例#6
0
        public void UpdateSaldo(OrderModel orderModel)
        {
            foreach (var item in orderModel.ProductList)
            {
                item.Product.Sales        += item.Quantity;
                item.Product.StockBalance -= item.Quantity;

                _context.Products.Attach(item.Product).State = EntityState.Modified;
            }

            _context.SaveChanges();
        }
示例#7
0
        public List <CartItem> CreateCartItem(List <CartItemModel> productList)
        {
            List <CartItem> cartItems = new();

            foreach (var product in productList)
            {
                CartItem cartItem = new CartItem
                {
                    ID       = Guid.NewGuid(),
                    Product  = product.Product,
                    Quantity = product.Quantity
                };
                cartItems.Add(cartItem);
            }

            _context.CartItem.AddRange(cartItems);
            _context.SaveChanges();
            return(cartItems);
        }
示例#8
0
文件: Worker.cs 项目: vmed/myBot
 public void Save()
 {
     db.SaveChanges();
 }