Beispiel #1
0
        /// <summary>
        /// 创建订单,涉及到的操作有2个:1. 把购物车中的项中购物车移除; 2.创建一个订单。
        /// 这两个操作必须同时完成或失败。
        /// </summary>
        /// <param name="user"></param>
        /// <param name="shoppingCart"></param>
        /// <returns></returns>
        public Order CreateOrder(User user, ShoppingCart shoppingCart)
        {
            var order = new Order();
            var shoppingCartItems =
                _shoppingCartItemRepository.GetAll(
                    new ExpressionSpecification<ShoppingCartItem>(s => s.ShoopingCart.Id == shoppingCart.Id));
            if (shoppingCartItems == null || !shoppingCartItems.Any())
                throw new InvalidOperationException("购物篮中没有任何物品");

            order.OrderItems = new List<OrderItem>();
            foreach (var shoppingCartItem in shoppingCartItems)
            {
                var orderItem = shoppingCartItem.ConvertToOrderItem();
                orderItem.Order = order;
                order.OrderItems.Add(orderItem);
                _shoppingCartItemRepository.Remove(shoppingCartItem);
            }
            order.User = user;
            order.Status = OrderStatus.Paid;
            _orderRepository.Add(order);
            _repositoryContext.Commit();
            return order;
        }
Beispiel #2
0
 public static UserRole CreateUserRole(User user, Role role)
 {
     return new UserRole() { UserId = user.Id, RoleId = role.Id };
 }
Beispiel #3
0
        // 将指定的用户从角色中移除。
        public void UnassignRole(User user, Role role = null)
        {
            if (user == null)
                throw new ArgumentNullException("user");
            Expression<Func<UserRole, bool>> specExpression = null;
            if (role == null)
                specExpression = ur => ur.UserId == user.Id;
            else
                specExpression = ur => ur.UserId == user.Id && ur.RoleId == role.Id;

            UserRole userRole = _userRoleRepository.GetBySpecification(Specification<UserRole>.Eval(specExpression));

            if (userRole == null) return;

            _userRoleRepository.Remove(userRole);
            _repositoryContext.Commit();
        }
Beispiel #4
0
        // 将指定的用户赋予特定的角色。
        public UserRole AssignRole(User user, Role role)
        {
            if (user == null)
                throw new ArgumentNullException("user");
            if (role == null)
                throw new ArgumentNullException("role");
            var userRole = _userRoleRepository.GetBySpecification(Specification<UserRole>.Eval(ur => ur.UserId == user.Id));
            if (userRole == null)
            {
                userRole = UserRole.CreateUserRole(user, role);
                _userRoleRepository.Add(userRole);
            }
            else
            {
                userRole.RoleId = role.Id;
                _userRoleRepository.Update(userRole);
            }

            _repositoryContext.Commit();
            return userRole;
        }