Ejemplo n.º 1
0
        public bool CreateWallet(Wallet model, ref List <KeyValuePair <string, string> > ErrorList)
        {
            var result = _context.Wallets.Add(model);

            if (_context.SaveChanges() < 0)
            {
                ErrorList.Add(new KeyValuePair <string, string>("DB", "Querry has been crashed"));
                return(false);
            }
            return(CreateTransaction(new Transaction {
                From = "ParrotWingsBank", To = result.Id, Value = 500, When = DateTime.Now.ToString()
            }, ref ErrorList));
        }
Ejemplo n.º 2
0
        private void Hydrate()
        {
            using (var context = new WalletContext(WalletContextOptions))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                var wallet = new Wallet
                {
                    Id         = "1435623429",
                    CustomerId = "ac11r21d554",
                    Name       = "default",
                    Balance    = 0,
                    Deleted    = false,
                    CreatedAt  = DateTime.Now,
                    Timestamp  = 1605032522017
                };

                var wallet2 = new Wallet
                {
                    Id         = "1735613459",
                    CustomerId = "pl989123hj1",
                    Name       = "default",
                    Balance    = 5000,
                    Deleted    = false,
                    CreatedAt  = DateTime.Now,
                    Timestamp  = 1605032617254
                };

                var wallet3 = new Wallet
                {
                    Id         = "1735613419",
                    CustomerId = "qx9102311a1",
                    Name       = "default",
                    Balance    = 10000,
                    Deleted    = false,
                    CreatedAt  = DateTime.Now,
                    Timestamp  = 1605032760952
                };

                context.AddRange(wallet, wallet2, wallet3);
                context.SaveChanges();
            }

            using (var context = new TransactionContext(TransactionContextOptions))
            {
                var transaction = new Transaction
                {
                    WalletId      = "1735613459",
                    Type          = TransactionType.Deposit,
                    WalletBalance = 5000,
                    Amount        = 5000,
                    CreatedAt     = DateTime.Now,
                    Timestamp     = 1605033203009
                };

                context.AddRange(transaction);
                context.SaveChanges();
            }
        }
        public async Task GetLastWalletEntryAsync_SingleEntry_ReturnsTheEntry()
        {
            //// Arrange

            // Setup In-Memory Database at desired state

            DbContextOptions <WalletContext> dbContextOptions = new DbContextOptionsBuilder <WalletContext>()
                                                                .UseInMemoryDatabase(databaseName: "GetLastWalletEntryAsync_SingleEntry_ReturnsTheEntry")
                                                                .Options;

            WalletEntry expectedEntry = new WalletEntry {
                Id = Guid.NewGuid().ToString(), EventTime = DateTime.UtcNow, Amount = 10, BalanceBefore = 0
            };

            using (WalletContext context = new WalletContext(dbContextOptions))
            {
                context.Add(expectedEntry);
                context.SaveChanges();
            }

            //// Act

            WalletEntry actualEntry;

            using (WalletContext context = new WalletContext(dbContextOptions))
            {
                IWalletRepository walletRepository = new WalletRepository(context);
                actualEntry = await walletRepository.GetLastWalletEntryAsync();
            }

            //// Assert

            Assert.NotNull(actualEntry);
            actualEntry.ShouldCompare(expectedEntry);
        }
Ejemplo n.º 4
0
        public int ExecuteQuery(string sql, params object[] paramaters)
        {
            var result = dataContext.Database.ExecuteSqlCommand(sql, paramaters);

            dataContext.SaveChanges();

            return(result);
        }
        public void AddSpending(SpendingDto model)
        {
            //var cat = db.Categories.FirstOrDefault(x => x.Name == model.Category) ?? new Category { Name = model.Category };

            var category = db.Categories.FirstOrDefault(x => x.Name == model.Category);

            if (category == null)
            {
                category = new Category()
                {
                    Name = model.Category
                };
                db.Categories.Add(category);
            }

            var spending = new Spending
            {
                Category    = category,
                Value       = model.Value,
                Date        = model.Date,
                Description = model.Description,
                Tags        = new List <SpendingTag>()
            };

            foreach (var t in model.Tags)
            {
                var tag = db.Tags.FirstOrDefault(x => x.Name == t);
                if (tag == null)
                {
                    tag = new Tag()
                    {
                        Name = t
                    };
                }
                spending.Tags.Add(new SpendingTag {
                    Tag = tag
                });
            }

            // added user

            //var user = db.Users.FirstOrDefault(x => x.Id == model.UserId);
            //spending.User = user;

            var claims = HttpContext.User.Claims;

            var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            spending.UserId = userId;

            //

            db.Spendings.Add(spending);

            db.SaveChanges();
        }
        public async Task InsertWalletAsync_PreviousWalletEntries_NewWalletEntry()
        {
            //// Arrange

            // Setup In-Memory Database at desired state

            DbContextOptions <WalletContext> dbContextOptions = new DbContextOptionsBuilder <WalletContext>()
                                                                .UseInMemoryDatabase(databaseName: "InsertWalletAsync_PreviousWalletEntries_NewWalletEntry")
                                                                .Options;

            WalletEntry[] previousEntries = new[] {
                new WalletEntry {
                    Id = Guid.NewGuid().ToString(), EventTime = DateTime.UtcNow, Amount = 10, BalanceBefore = 0
                },
                new WalletEntry {
                    Id = Guid.NewGuid().ToString(), EventTime = DateTime.UtcNow.AddTicks(1), Amount = 10, BalanceBefore = 0
                }
            };
            using (WalletContext context = new WalletContext(dbContextOptions))
            {
                await context.AddRangeAsync(previousEntries);

                context.SaveChanges();
            }

            // Initialize Entry

            WalletEntry expectedNewEntry = new WalletEntry {
                Id = Guid.NewGuid().ToString(), EventTime = DateTime.UtcNow, Amount = 10, BalanceBefore = 0
            };

            //// Act

            using (WalletContext context = new WalletContext(dbContextOptions))
            {
                IWalletRepository walletRepository = new WalletRepository(context);
                await walletRepository.InsertWalletEntryAsync(expectedNewEntry);
            }

            //// Assert

            DbSet <WalletEntry> actualWalletEntries;

            using (WalletContext context = new WalletContext(dbContextOptions))
            {
                actualWalletEntries = context.Transactions;
                Assert.Collection(actualWalletEntries,
                                  walletEntry => walletEntry.ShouldCompare(previousEntries[0]),
                                  walletEntry => walletEntry.ShouldCompare(previousEntries[1]),
                                  walletEntry => walletEntry.ShouldCompare(expectedNewEntry));
            }
        }
Ejemplo n.º 7
0
        public void EnsureSeedWalletTypeData(string filePath)
        {
            WalletContext walletContext = new WalletContext();

            if (!walletContext.WalletTypes.Any())
            {
                dynamic jsonData = ReadJsonFile(filePath);
                if (jsonData != null)
                {
                    foreach (dynamic d in jsonData)
                    {
                        walletContext.WalletTypes.Add(new WalletTypeEntity()
                        {
                            Name        = d.Name,
                            Category    = d.Category,
                            Description = d.Description
                        });
                    }
                    walletContext.SaveChanges();
                }
            }
        }
Ejemplo n.º 8
0
        public WalletController(WalletContext context)
        {
            _context = context;

            //для тестировки
            //_context.Database.EnsureDeleted();
            //_context.Database.EnsureCreated();
            if (!_context.Users.Any())
            {
                var user1 = new User()
                {
                    Name       = "Mark",
                    Currencies = new List <Currency>()
                    {
                        new Currency()
                        {
                            CurrencyCode = "RUB", Ammount = Convert.ToDecimal(10000)
                        }
                    }
                };

                var user2 = new User()
                {
                    Name       = "Polo",
                    Currencies = new List <Currency>()
                    {
                        new Currency()
                        {
                            CurrencyCode = "IDR", Ammount = Convert.ToDecimal(300)
                        }
                    }
                };

                _context.Users.AddRange(user1, user2);

                _context.SaveChanges();
            }
        }
        public async Task InsertWalletAsync_DuplicateId_ThrowsArgumentException()
        {
            //// Arrange

            // Setup In-Memory Database at desired state

            DbContextOptions <WalletContext> dbContextOptions = new DbContextOptionsBuilder <WalletContext>()
                                                                .UseInMemoryDatabase(databaseName: "InsertWalletAsync_DuplicateId_ThrowsArgumentException")
                                                                .Options;

            WalletEntry originalWalletEntry =
                new WalletEntry {
                Id = "IAmDuplicate", EventTime = DateTime.UtcNow, Amount = 10, BalanceBefore = 0
            };

            using (WalletContext context = new WalletContext(dbContextOptions))
            {
                await context.AddAsync(originalWalletEntry);

                context.SaveChanges();
            }

            // Initialize Entry

            WalletEntry duplicateEntry = new WalletEntry {
                Id = "IAmDuplicate", EventTime = DateTime.UtcNow, Amount = 10, BalanceBefore = 0
            };

            //// Act / Assert

            using (WalletContext context = new WalletContext(dbContextOptions))
            {
                IWalletRepository walletRepository = new WalletRepository(context);
                await Assert.ThrowsAsync <ArgumentException>(() =>
                                                             walletRepository.InsertWalletEntryAsync(duplicateEntry)
                                                             );
            }
        }
Ejemplo n.º 10
0
        public void EnsureSeedWalletData(string filePath)
        {
            WalletContext walletContext = new WalletContext();

            if (!walletContext.Wallets.Any())
            {
                dynamic jsonData = ReadJsonFile(filePath);
                if (jsonData != null)
                {
                    foreach (dynamic d in jsonData)
                    {
                        walletContext.Wallets.Add(new WalletEntity()
                        {
                            ClientId                = d.ClientId,
                            Name                    = d.Name,
                            Balance                 = d.Balance,
                            IsLocked                = d.IsLocked,
                            Currency                = d.Currency,
                            DateCreated             = d.DateCreated,
                            DateModified            = d.DateModified,
                            WalletTypeId            = d.WalletTypeId,
                            LockOnNotificationLimit = d.LockOnNotificationLimit,
                            LockOnSpendLimit        = d.LockOnSpendLimit,
                            LockOnWithdrawLimit     = d.LockOnWithdrawLimit,
                            SpendLimit              = d.SpendLimit,
                            WithdrawLimit           = d.WithdrawLimit,
                            NotificationLimit       = d.NotificationLimit,
                            IsDeleted               = d.IsDeleted,
                            DeleteDate              = d.DeleteDate,
                            ImageId                 = d.ImageId
                        });
                    }
                    walletContext.SaveChanges();
                }
            }
        }
Ejemplo n.º 11
0
 public Task InsertWalletEntryAsync(WalletEntry walletEntry)
 {
     _walletContext.Transactions.Add(walletEntry);
     _walletContext.SaveChanges();
     return(Task.CompletedTask);
 }