public async Task AddsCustomerToDb()
        {
            //Arrange - create an object to configure your inmemory DB.
            var options = new DbContextOptionsBuilder <ConsoleShopperDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddsCustomerToDb")
                          .Options;


            //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext
            using (var db = new ConsoleShopperDbContext(options))
            {
                CustomerAddress customerAddress = new CustomerAddress {
                    Id = 1, Street = "8286 Clay Ave.", City = "Spokane", State = "WA", Zip = "11111"
                };
                Customer customer = new Customer {
                    Id = 6, FirstName = "Maribeth", LastName = "Fontenot", Email = "*****@*****.**", PhoneNo = "1234112233", Password = "******", UserTypeId = 2, CustomerAddress = customerAddress
                };
                db.Add(customer);
                db.SaveChanges();
            }

            //Assert
            using (var context = new ConsoleShopperDbContext(options))
            {
                Assert.Equal(1, context.Customers.Count());
                var customer1 = await context.Customers.Where(x => x.FirstName == "Maribeth")
                                .AsNoTracking().FirstOrDefaultAsync();

                var customer1Address = await context.Customers.
                                       Include(c => c.CustomerAddress).AsNoTracking().FirstOrDefaultAsync();

                Assert.Equal("Maribeth", customer1.FirstName);
                Assert.Equal("11111", customer1Address.CustomerAddress.Zip);
            }
        }
        public async Task AddsProductInventoryIntoDatabase()
        {
            //Arrange - create an object to configure your inmemory DB.
            var options = new DbContextOptionsBuilder <ConsoleShopperDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddsCustomerToDb")
                          .Options;


            //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext
            using (var db = new ConsoleShopperDbContext(options))
            {
                // Put Name in Product entity

                Product product = new Product {
                    Id = 1, Name = "Guitar", ProductCode = "P00007", Price = 155.50M
                };
                Store store = new Store {
                    Id = 1, Name = "Texas"
                };
                InventoryItem inventory = new InventoryItem {
                    Id           = 1,
                    ProductId    = 1,
                    Quantity     = 1,
                    Product      = product,
                    Store        = store,
                    LoggedUserId = 1,
                    ChangedDate  = DateTime.Now.ToLocalTime()
                };
                db.Add(inventory);
                db.SaveChanges();
            }

            //Assert
            using (var context = new ConsoleShopperDbContext(options))
            {
                Assert.Equal(1, context.Products.Count());
                var inventory1 = await context.Inventory
                                 .Include(i => i.Product)
                                 .Include(i => i.Store)
                                 .Where(i => i.Product.Name == "Guitar")
                                 .AsNoTracking().FirstOrDefaultAsync();


                Assert.Equal("Guitar", inventory1.Product.Name);
            }
        }
        public async Task AddsOrderIntoDatabase()
        {
            //Arrange - create an object to configure your inmemory DB.
            var options = new DbContextOptionsBuilder <ConsoleShopperDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddsCustomerToDb")
                          .Options;


            //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext
            using (var db = new ConsoleShopperDbContext(options))
            {
                /*
                 * Create order and orderline objects.
                 *  1. Customer :- should be (logged in) -> from UI.
                 *      => take customer id from here and put it in order object.
                 *  2. Ask Customer to choose the Store location.
                 *      => take Store location from here and put it in order object.
                 *  3. Ask Customer about which product/products and what quantites they want to order.
                 *      => Show the list of products, aviliable quantites and their respective prices to the user.
                 *  4. Allow customers to choose multiple products with multiple quantities.
                 *      => hold the product code + quantity in a dictionary.
                 *  5. Ask user to confirm the purchase.
                 *  6. Insert into order the
                 *      => the userId
                 *      => the storeLocation
                 *      => and time of purchase.
                 *  7. Take out productCode and quantity one by one
                 *      => Insert into the orderline object
                 *          => InventoryItemId(i.e: product Id)
                 *          => Quantity
                 *          => Price
                 *          => OrderId
                 *      => Decrease the Quantity in InventoryItem after purchase.
                 *
                 */

                // Make sure its in same store.
                Customer cust = new Customer {
                    Id = 1
                };
                // Ask user list of products
                InventoryItem i1 = new InventoryItem {
                    StoreId = 1, ProductId = 1, Quantity = 20
                };
                InventoryItem i2 = new InventoryItem {
                    StoreId = 1, ProductId = 2, Quantity = 20
                };

                // hold product chosen by user and quantity specified by user
                Dictionary <int, int> pairs = new Dictionary <int, int>
                {
                    { i1.ProductId, 5 },
                    { i2.ProductId, 4 }
                };
                // Oh no ! user wants to add another  2 items !
                // have no fear !
                pairs[i1.ProductId] += 2;

                Order order = new Order {
                    Id = 1, CustomerId = cust.Id, StoreId = i1.StoreId, OrderDate = DateTimeOffset.Now.LocalDateTime
                };

                OrderLineItem oLI1 = new OrderLineItem
                {
                    Id              = 1,
                    OrderId         = 1,
                    InventoryItemId = i1.ProductId,
                    Quantity        = pairs[i1.ProductId]
                };
                OrderLineItem oLI2 = new OrderLineItem
                {
                    Id              = 2,
                    OrderId         = 1,
                    InventoryItemId = i2.ProductId,
                    Quantity        = pairs[i2.ProductId]
                };
                db.Add(order);
                db.Add(oLI1);
                db.Add(oLI2);
                db.SaveChanges();
            }

            //Assert
            using (var context = new ConsoleShopperDbContext(options))
            {
                //Assert.Equal("Guitar", inventory1.Product.Name);
                var test = await context.OrderLineItems.Include(x => x.Order).CountAsync();

                Assert.Equal(2, test);
                var order1 = await context.OrderLineItems
                             .Where(x => x.InventoryItemId == 1)
                             .FirstOrDefaultAsync(x => x.Id == 1);

                Assert.Equal(1, order1.Id);
            }
        }
 /// <summary>
 /// Product Repository Constructor injects dbContext from DI container 
 /// and initializes the private constructor we have. 
 /// </summary>
 /// <param name="dbContext"></param>
 public ProductRepository(ConsoleShopperDbContext dbContext)
 {
     _dbContext = dbContext;
 }
 public CustomerRepository(ConsoleShopperDbContext dbContext)
 {
     _dbContext = dbContext;
 }
示例#6
0
 /// <summary>
 /// Constructor for OrderService injects dbContxt from DiContainer and initializes it.
 /// </summary>
 /// <param name="dbContext"></param>
 public OrderService(ConsoleShopperDbContext dbContext)
 {
     _dbContext = dbContext;
 }