public TransactionHistory RecordTransactionHistory(int sellerId, int buyerId, int sharesId, int quantity, DateTime dateTime)
        {
            using (TradingDBContext db = new TradingDBContext())
            {
                try
                {
                    Client             seller    = db.Clients.Where(c => c.ClientID == sellerId).Single();
                    Client             buyer     = db.Clients.Where(c => c.ClientID == buyerId).Single();
                    Shares             shares    = db.Shares.Where(s => s.SharesID == sharesId).Single();
                    TransactionHistory newRecord = new TransactionHistory {
                        Seller     = seller,
                        Buyer      = buyer,
                        DateTime   = dateTime,
                        Quantity   = quantity,
                        SelledItem = shares
                    };

                    db.TransactionHistories.Add(newRecord);
                    db.SaveChanges();
                    return(newRecord);
                }
                catch (SystemException ex)
                {
                    inputOutputDevice.Print($"{phraseProvider.GetPhrase("HistoryError")}: {ex.Message}");
                    return(new TransactionHistory());
                }
            }
        }
Beispiel #2
0
        private bool Transaction(int sellerId, int buyerId, int sharesId, int quantity, out DateTime TransactionDateTime)
        {
            using (TradingDBContext db = new TradingDBContext())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        Client seller     = db.Clients.Where(c => c.ClientID == sellerId).First();
                        Client buyer      = db.Clients.Where(c => c.ClientID == buyerId).First();
                        string sellerInfo = $"Seller: {seller.Name}, Balance: {seller.Balance}";
                        string buyerInfo  = $"Buyer: {buyer.Name}, Balance: {buyer.Balance}";
                        Logger.Log.Info(sellerInfo);
                        Logger.Log.Info(buyerInfo);

                        if (buyer.Zone == "Black")
                        {
                            throw new SystemException(phraseProvider.GetPhrase("BuyerInBlackZone"));
                        }

                        decimal sum = db.Shares.Where(s => s.SharesID == sharesId).First().Price *quantity;
                        seller.Balance += sum;
                        if (seller.Balance == 0M)
                        {
                            seller.Zone = "Orange";
                        }

                        if (seller.Balance > 0)
                        {
                            seller.Zone = "Free";
                        }

                        buyer.Balance -= sum;
                        if (buyer.Balance == 0M)
                        {
                            buyer.Zone = "Orange";
                        }

                        if (buyer.Balance < 0)
                        {
                            buyer.Zone = "Black";
                        }

                        ClientShares sellersItem = db.ClientShares.Where(
                            s => s.Shares.SharesID == sharesId).Where(
                            s => s.ClientID == sellerId).Single();
                        sellersItem.Quantity -= quantity;

                        ClientShares buyersItem = db.ClientShares.Where(
                            s => s.Shares.SharesID == sharesId).Where(
                            s => s.ClientID == buyerId).Single();
                        buyersItem.Quantity += quantity;
                        string sharesInfo = $"Shares type: {sellersItem.Shares.SharesType}, Quantity: {quantity}, Total: {sum}";
                        Logger.Log.Info(sharesInfo);

                        db.SaveChanges();
                        transaction.Commit();
                        this.inputOutputDevice.Print(this.phraseProvider.GetPhrase("Success"));
                        Logger.Log.Info(phraseProvider.GetPhrase("Success"));
                        sellerInfo = $"Seller: {seller.Name}, Balance: {seller.Balance}";
                        buyerInfo  = $"Buyer: {buyer.Name}, Balance: {buyer.Balance}";
                        Logger.Log.Info(sellerInfo);
                        Logger.Log.Info(buyerInfo);
                        TransactionDateTime = DateTime.Now;
                        return(true);
                    }
                    catch (System.Data.Entity.Validation.DbEntityValidationException)
                    {
                        transaction.Rollback();
                        this.inputOutputDevice.Print($"{this.phraseProvider.GetPhrase("Failure")}: " +
                                                     $"{this.phraseProvider.GetPhrase("Validation")}");
                        Logger.Log.Info($"{this.phraseProvider.GetPhrase("Failure")}: " +
                                        $"{this.phraseProvider.GetPhrase("Validation")}");
                        TransactionDateTime = DateTime.Now;
                        return(false);
                    }
                    catch (SystemException ex)
                    {
                        transaction.Rollback();
                        this.inputOutputDevice.Print($"{this.phraseProvider.GetPhrase("Failure")}: {ex.Message}");
                        Logger.Log.Info($"{this.phraseProvider.GetPhrase("Failure")}: {ex.Message}");
                        TransactionDateTime = DateTime.Now;
                        return(false);
                    }
                }
            }
        }