public void should_update_sale_record_return()
        {
            var userId = TestData.GenerateRandomInt();

            var company = TestData.GenerateRandomString();

            var symbol = TestData.GenerateRandomString();

            var quantity = 70;

            var session = Substitute.For <ISession>();

            _dbSessionService.OpenSession().Returns(session);

            _stockQueryService.QueryStocks(Arg.Any <ISession>(), Arg.Any <int>(), Arg.Any <string>())
            .Returns(new List <Stock>
            {
                new Stock
                {
                    UserId   = userId,
                    Symbol   = symbol,
                    Quantity = 28
                }
            });

            _sut.CreateSaleRecord(userId, company, symbol, quantity);
        }
        public Stock CreateSaleRecord(int userId, string company, string symbol, int quantity)
        {
            using (var session = _dbSessionService.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var result = _stockQueryService.QueryStocks(session, userId, symbol);

                    if (result[0].Quantity - quantity > 0)
                    {
                        result[0].Quantity = result[0].Quantity - quantity;

                        session.SaveOrUpdate(result[0]);

                        transaction.Commit();

                        return(result[0]);
                    }

                    session.Delete(result[0]);

                    transaction.Commit();

                    return(result[0]);
                }
            }
        }
Example #3
0
        public Stock CreatePurchaseRecord(int userId, string company, string symbol, int quantity)
        {
            using (var session = _dbSessionService.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var result = _stockQueryService.QueryStocks(session, userId, symbol);

                    if (result.Count != 0)
                    {
                        result[0].Quantity = result[0].Quantity + quantity;

                        session.SaveOrUpdate(result[0]);

                        transaction.Commit();

                        return(result[0]);
                    }

                    var stockObject = new Stock
                    {
                        UserId      = userId,
                        Company     = company,
                        Symbol      = symbol,
                        Quantity    = quantity,
                        CreatedDate = DateTime.Now
                    };

                    session.Save(stockObject);

                    transaction.Commit();

                    return(stockObject);
                }
            }
        }