示例#1
0
        protected override void Seed(TradingDBContext context)
        {
            var client1 = new Client {
                Name = "Aaron Wolf", PhoneNumber = "(812) 904 33 10", Balance = 10000M, Zone = "Free"
            };
            var client2 = new Client {
                Name = "Betany Stutoff", PhoneNumber = "(954) 404 13 11", Balance = 9030M, Zone = "Free"
            };
            var client3 = new Client {
                Name = "Anton Zaycev", Balance = 1076M, Zone = "Free"
            };
            var client4 = new Client {
                Name = "Paul Beat", PhoneNumber = "(383) 01 21 312 ", Balance = 51003M, Zone = "Free"
            };
            var client5 = new Client {
                Name = "Victor Powers", Balance = 19200M, Zone = "Free"
            };
            var client6 = new Client {
                Name = "Liam Piam", PhoneNumber = "(800) 2000 501", Balance = 22120M, Zone = "Free"
            };
            var client7 = new Client {
                Name = "Varian Wrynn", Balance = 20000M, Zone = "Free"
            };
            var client8 = new Client {
                Name = "Alexander Bely", Balance = 8100M, Zone = "Free"
            };

            context.Clients.Add(client1);
            context.Clients.Add(client2);
            context.Clients.Add(client3);
            context.Clients.Add(client4);
            context.Clients.Add(client5);
            context.Clients.Add(client6);
            context.Clients.Add(client7);
            context.Clients.Add(client8);

            var shares1 = new Shares {
                SharesType = "Sbeerbank", Price = 836M
            };
            var shares2 = new Shares {
                SharesType = "MMM", Price = 2874M
            };
            var shares3 = new Shares {
                SharesType = "Motherland", Price = 282M
            };

            context.Shares.Add(shares1);
            context.Shares.Add(shares2);
            context.Shares.Add(shares3);

            var clientShares1 = new ClientShares {
                Shares = shares1, Quantity = 43, Client = client1
            };
            var clientShares2 = new ClientShares {
                Shares = shares1, Quantity = 30, Client = client2
            };
            var clientShares3 = new ClientShares {
                Shares = shares1, Quantity = 12, Client = client3
            };
            var clientShares4 = new ClientShares {
                Shares = shares1, Quantity = 2, Client = client4
            };
            var clientShares5 = new ClientShares {
                Shares = shares1, Quantity = 92, Client = client5
            };
            var clientShares6 = new ClientShares {
                Shares = shares1, Quantity = 122, Client = client6
            };
            var clientShares7 = new ClientShares {
                Shares = shares1, Quantity = 0, Client = client7
            };
            var clientShares8 = new ClientShares {
                Shares = shares1, Quantity = 0, Client = client8
            };
            var clientShares9 = new ClientShares {
                Shares = shares2, Quantity = 2, Client = client1
            };
            var clientShares10 = new ClientShares {
                Shares = shares2, Quantity = 24, Client = client2
            };
            var clientShares11 = new ClientShares {
                Shares = shares2, Quantity = 43, Client = client3
            };
            var clientShares12 = new ClientShares {
                Shares = shares2, Quantity = 21, Client = client4
            };
            var clientShares13 = new ClientShares {
                Shares = shares2, Quantity = 24, Client = client5
            };
            var clientShares14 = new ClientShares {
                Shares = shares2, Quantity = 36, Client = client6
            };
            var clientShares15 = new ClientShares {
                Shares = shares2, Quantity = 1, Client = client7
            };
            var clientShares16 = new ClientShares {
                Shares = shares2, Quantity = 0, Client = client8
            };
            var clientShares17 = new ClientShares {
                Shares = shares3, Quantity = 87, Client = client1
            };
            var clientShares18 = new ClientShares {
                Shares = shares3, Quantity = 11, Client = client2
            };
            var clientShares19 = new ClientShares {
                Shares = shares3, Quantity = 39, Client = client3
            };
            var clientShares20 = new ClientShares {
                Shares = shares3, Quantity = 48, Client = client4
            };
            var clientShares21 = new ClientShares {
                Shares = shares3, Quantity = 101, Client = client5
            };
            var clientShares22 = new ClientShares {
                Shares = shares3, Quantity = 98, Client = client6
            };
            var clientShares23 = new ClientShares {
                Shares = shares3, Quantity = 2, Client = client7
            };
            var clientShares24 = new ClientShares {
                Shares = shares3, Quantity = 34, Client = client8
            };

            context.ClientShares.Add(clientShares1);
            context.ClientShares.Add(clientShares2);
            context.ClientShares.Add(clientShares3);
            context.ClientShares.Add(clientShares4);
            context.ClientShares.Add(clientShares5);
            context.ClientShares.Add(clientShares6);
            context.ClientShares.Add(clientShares7);
            context.ClientShares.Add(clientShares8);
            context.ClientShares.Add(clientShares9);
            context.ClientShares.Add(clientShares10);
            context.ClientShares.Add(clientShares11);
            context.ClientShares.Add(clientShares12);
            context.ClientShares.Add(clientShares13);
            context.ClientShares.Add(clientShares14);
            context.ClientShares.Add(clientShares15);
            context.ClientShares.Add(clientShares16);
            context.ClientShares.Add(clientShares17);
            context.ClientShares.Add(clientShares18);
            context.ClientShares.Add(clientShares19);
            context.ClientShares.Add(clientShares20);
            context.ClientShares.Add(clientShares21);
            context.ClientShares.Add(clientShares22);
            context.ClientShares.Add(clientShares23);
            context.ClientShares.Add(clientShares24);

            //var clientFund1 = new ClientFund { Client = client1, Balance = 10000M, Zone = "Free"};
            //var clientFund2 = new ClientFund { Client = client2, Balance = 9030M, Zone = "Free"};
            //var clientFund3 = new ClientFund { Client = client3, Balance = 51003M, Zone = "Free"};
            //var clientFund4 = new ClientFund { Client = client4, Balance = 1076M, Zone = "Free"};
            //var clientFund5 = new ClientFund { Client = client5, Balance = 19200M, Zone = "Free"};
            //var clientFund6 = new ClientFund { Client = client6, Balance = 22120M, Zone = "Free"};
            //var clientFund7 = new ClientFund { Client = client7, Balance = 20000M, Zone = "Free"};
            //var clientFund8 = new ClientFund { Client = client8, Balance = 8100M, Zone = "Free"};

            //context.ClientFunds.Add(clientFund1);
            //context.ClientFunds.Add(clientFund2);
            //context.ClientFunds.Add(clientFund3);
            //context.ClientFunds.Add(clientFund4);
            //context.ClientFunds.Add(clientFund5);
            //context.ClientFunds.Add(clientFund6);
            //context.ClientFunds.Add(clientFund7);
            //context.ClientFunds.Add(clientFund8);

            context.SaveChanges();
        }
示例#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);
                    }
                }
            }
        }