Пример #1
0
        public async Task RegisterUser(string email, string firstname, string username, string lastname, string password, string personalId, int genderId, DateTime birthDate, string address)
        {
            var pincode = await _userRepository.GetAvailablePincode();

            var user = await _userRepository.GetUserByPersonalNumber(personalId);

            if (user != null)
            {
                throw new BgsException((int)WebApiErrorCodes.PersonalNumberIsAlreadyUsed);
            }

            var userbyUsername = _userRepository.GetUserByUsername(username);

            if (userbyUsername != null)
            {
                throw new BgsException((int)WebApiErrorCodes.UsernameAlreadyExists);
            }

            using (var transaction = new BgsTransactionScope())
            {
                await _userRepository.AddUser(email, firstname, username, lastname, password.ToSHA256(pincode), (int)UserStatus.Active, pincode, personalId, genderId, DateTime.Now, birthDate, address);

                await _userRepository.ReleasePincode(pincode, DateTime.Now);

                transaction.Complete();
            }
        }
Пример #2
0
        public void AddCartItem(int productId, int userId)
        {
            var stock = _productRepository.GetProductAvailableStock(productId);

            if (stock == 0)
            {
                throw new BgsException((int)WebApiErrorCodes.OutOfStock);
            }
            else
            {
                var cartItem = _cartRepository.GetCartItem(productId, userId);

                using (var transaction = new BgsTransactionScope())
                {
                    if (cartItem == null)
                    {
                        _cartRepository.AddCartItem(productId, userId, 1, DateTime.Now);
                    }
                    else
                    {
                        _cartRepository.UpdateCartItemQuantity(cartItem.Id, cartItem.Quantity + 1);
                    }

                    BlockStock(productId, 1);

                    transaction.Complete();
                }
            }
        }
Пример #3
0
        public void AddBalance(int userId, decimal amount)
        {
            var balance = _userRepository.GetBalance(userId) ?? 0;

            balance = balance + amount;

            using (var transaction = new BgsTransactionScope())
            {
                _userRepository.UpdateBalance(userId, balance);
                _transactionRepository.AddTransaction((int)TransactionType.Deposit, userId, DateTime.Now, amount);
                transaction.Complete();
            };
        }
Пример #4
0
        public void PlaceOrder(int userId)
        {
            var balance = _userRepository.GetBalance(userId);

            var cartItems = _cartRepository.GetCartItems(userId);

            var totalAmount = cartItems.Sum(x => x.Price * x.Quantity);

            balance = balance - totalAmount;

            if (balance < 0)
            {
                throw new BgsException((int)WebApiErrorCodes.NotEnoughBalance);
            }

            using (var transaction = new BgsTransactionScope())
            {
                _userRepository.UpdateBalance(userId, balance);

                foreach (var item in cartItems)
                {
                    var blocked = (int)_productRepository.GetBlockedProductQuantity(item.ProductId);
                    _productRepository.UpdateBlockedProductQuantity(item.ProductId, blocked - item.Quantity);

                    var stock = _productRepository.GetProductStock(item.ProductId) ?? 0;

                    if ((stock - item.Quantity) < 0)
                    {
                        throw new BgsException((int)WebApiErrorCodes.OutOfStock);
                    }


                    _productRepository.UpdateProductStock(item.ProductId, stock - item.Quantity);
                }

                _transactionRepository.AddTransaction((int)TransactionType.Payment, userId, DateTime.Now, totalAmount);

                _cartRepository.DeleteCartItems(userId);

                var orderId = _orderRepository.AddOrder(userId, (int)OrderStatus.Pending, totalAmount, DateTime.Now, DateTime.Now);

                foreach (var item in cartItems)
                {
                    var amount = item.Price * item.Quantity;
                    _orderRepository.AddOrderItem(orderId, item.ProductId, item.Quantity, item.Price, amount);
                }

                transaction.Complete();
            }
        }
Пример #5
0
        public async Task AddBalance(int userId, decimal amount)
        {
            var balance = await _userRepository.GetBalance(userId) ?? 0;

            balance = balance + amount;

            using (var transaction = new BgsTransactionScope())
            {
                await _userRepository.UpdateBalance(userId, balance);

                await _transactionRepository.AddTransaction((int)TransactionType.Deposit, userId, (int)TransactionStatus.Success, DateTime.Now, amount);

                transaction.Complete();
            };
        }
Пример #6
0
        public void RegisterUser(string email, string firstname, string lastname, string password)
        {
            var pincode = _userRepository.GetAvailablePincode();
            var user    = _userRepository.GetUserByEmail(email);

            if (user != null)
            {
                throw new BgsException((int)WebApiErrorCodes.EmailAlreadyExists);
            }

            using (var transaction = new BgsTransactionScope())
            {
                _userRepository.AddUser(email, firstname, lastname, password.ToSHA256(pincode), (int)UserStatus.Active, pincode);
                _userRepository.ReleasePincode(pincode, DateTime.Now);

                transaction.Complete();
            }
        }
Пример #7
0
        public void AddProductAttachment(int productId, IEnumerable <IFormFile> files)
        {
            using (var transaction = new BgsTransactionScope())
            {
                foreach (var file in files)
                {
                    var multiContent = file.ToHttpContent();

                    var response = _httpClient.PostAsync($"{_multimediaApiBaseUri}/image/add", multiContent).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        _productRepository.AddProductAttachment(productId, response.Content.ReadAsStringAsync().Result);
                    }
                }

                transaction.Complete();
            }
        }
Пример #8
0
        public async Task <BetResponseModel> Bet(int userId, decimal amount, decimal number)
        {
            var balance = await _UserRepository.GetBalance(userId);

            balance = balance - amount;

            if (balance < 0)
            {
                await _TransactionRepository.AddTransaction((int)TransactionType.Bet, userId, (int)TransactionStatus.Failed, DateTime.Now, amount);

                throw new BgsException((int)WebApiErrorCodes.NotEnoughBalance);
            }

            var     winNumber = new Random().Next(0, 36);
            decimal winAmount = 0;

            if (winNumber == number)
            {
                winAmount = amount * 36;
                balance   = balance + winAmount;
            }

            using (var transaction = new BgsTransactionScope())
            {
                await _UserRepository.UpdateBalance(userId, balance);

                await _TransactionRepository.AddTransaction((int)TransactionType.Win, userId, (int)TransactionStatus.Success, DateTime.Now, winAmount);

                transaction.Complete();
            }

            return(new BetResponseModel
            {
                WinAmount = winAmount,
                WinnerNumber = winNumber
            });
        }