Exemple #1
0
        /// <summary>
        /// Checks if shopping cart contains anything if it does
        /// adds to the quantity otherwise adds as new.
        /// </summary>
        /// <param name="product"></param>
        /// <param name="amount"></param>
        public void AddToCart(Product product, int amount, string username, string storeId)
        {
            var shoppingCartItem =
                _context.ShoppingCartItems.SingleOrDefault(
                    s => s.Product.Id == product.Id && s.ShoppingCartId == ShoppingCartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    Product        = product,
                    Quantity       = 1,
                    customerEmail  = username,
                    StoreId        = storeId
                };

                _context.ShoppingCartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Quantity++;
            }
            _context.SaveChanges();
        }
        public async void GetsAllOrdersForCustomer()
        {
            //Arrange
            var options = InMemoryDb("GetsCustomersOrders");

            //Act
            using (var context = new MSDbContext(options))
            {
                var customer = new Customer
                {
                    Id        = 4,
                    FirstName = "Conan",
                    LastName  = "perse",
                    Email     = "*****@*****.**"
                };
                context.Add(customer);
                context.SaveChanges();
                CreateTwoproducts(context);

                var store = new Store
                {
                    Id   = 7,
                    Name = "SomeLocation"
                };
                context.Add(store);
                context.SaveChanges();

                var order = new Order
                {
                    CustomerId = 2,
                    OrderDate  = DateTime.Now,
                    StoreId    = 3,
                };
                context.Add(order);
                order = new Order
                {
                    CustomerId = 6,
                    OrderDate  = DateTime.Now,
                    StoreId    = 5,
                };
                context.Add(order);
                context.SaveChanges();
            }
            //Assert
            using (var context = new MSDbContext(options))
            {
                IHttpContextAccessor contextAccessor = new HttpContextAccessor();
                var shoppingCartRepo = new ShoppingCart(context);
                var orderRepo        = new OrderService(context, shoppingCartRepo, contextAccessor);
                var orders           = await orderRepo.FindAsync(o => o.CustomerId == 1);

                Assert.Empty(orders);
            }
        }
        public void ShoppingCartHasItems()
        {
            // Arrange
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "CustomerByName")
                          .Options;
            ShoppingCartItem cartItem = new ShoppingCartItem
            {
                ShoppingCartId = "1",
                customerEmail  = "*****@*****.**",
                Quantity       = 2,
                StoreId        = "5"
            };


            //Act
            using (var context = new MSDbContext(options))
            {
                context.Add(cartItem);
                context.SaveChanges();

                var result = context.ShoppingCartItems
                             .Where(c => c.customerEmail.Contains(cartItem.customerEmail))
                             .AsNoTracking().FirstOrDefault();

                //Assert
                Assert.Equal(cartItem.customerEmail, result.customerEmail);
            }
        }
        public void AddsProductToDbTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddsProductToDbTest")
                          .Options;

            //Act
            using (var db = new MSDbContext(options))
            {
                Product sitar = new Product
                {
                    Id    = 17,
                    Name  = "sitar",
                    Price = 100M
                };

                db.Add(sitar);
                db.SaveChanges();
            }

            //Assert
            using (var context = new MSDbContext(options))
            {
                Assert.Equal(1, context.Products.Count());

                var product1 = context.Products.Where(p => p.Id == 17).FirstOrDefault();
                Assert.Equal(17, product1.Id);
                Assert.Equal("sitar", product1.Name);
            }
        }
Exemple #5
0
 public static int InsertToken(NewTokenReq newToken)
 {
     try
     {
         var token = new Token()
         {
             IsActive      = true,
             UserId        = newToken.UserId,
             TokenStr      = newToken.TokenStr,
             LastUsageTime = newToken.TokenTime
         };
         using (var dbCtx = new MSDbContext())
         {
             dbCtx.Tokens.Add(token);
             int result = dbCtx.SaveChanges();
             if (result == 1)
             {
                 return(token.Id);
             }
             else
             {
                 throw new Exception();
             }
         }
     }
     catch (Exception e)
     {
         var ex = new InsertIntoDataBaseException(ExceptionMessage.InsertIntoTokensException, e);
         Logger.Log.Error(ex.Message, ex);
         throw ex;
     }
 }
Exemple #6
0
 public static int InsertUser(NewUserReq newUser)
 {
     try
     {
         var user = new User()
         {
             IsActive = true,
             Name     = newUser.Name,
             Tel      = newUser.Tel
         };
         using (var dbCtx = new MSDbContext())
         {
             dbCtx.Users.Add(user);
             int result = dbCtx.SaveChanges();
             if (result == 1)
             {
                 return(user.Id);
             }
             else
             {
                 throw new Exception();
             }
         }
     }
     catch (Exception e)
     {
         var ex = new InsertIntoDataBaseException(ExceptionMessage.InsertIntoUsersException, e);
         Logger.Log.Error(ex.Message, ex);
         throw ex;
     }
 }
        public void AddsStoreToDbTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddsStoreToDbTest")
                          .Options;

            //Act
            using (var db = new MSDbContext(options))
            {
                Store location = new Store
                {
                    Name = "Spokane"
                };

                db.Add(location);
                db.SaveChanges();
            }

            //Assert
            using (var context = new MSDbContext(options))
            {
                Assert.Equal(1, context.Stores.Count());

                var store2 = context.Stores.Where(s => s.Id == 1).FirstOrDefault();
                Assert.Equal(1, store2.Id);
                Assert.Equal("Spokane", store2.Name);
            }
        }
Exemple #8
0
 public static bool ValidateToken(string phoneNumber, string token)
 {
     try
     {
         using (var dbCtx = new MSDbContext())
         {
             var userToken = (from t in dbCtx.Tokens
                              join u in dbCtx.Users on t.UserId equals u.Id
                              where t.TokenStr == token &&
                              u.Tel == phoneNumber &&
                              t.IsActive
                              select new { t, u }).FirstOrDefault();
             if (userToken == null)
             {
                 return(false);
             }
             var now  = DateTime.Now;
             var diff = now - userToken.t.LastUsageTime;
             if (diff.Days > 30)
             {
                 return(false);
             }
             userToken.t.LastUsageTime = now;
             dbCtx.SaveChanges();
             return(true);
         }
     }
     catch (Exception e)
     {
         var ex = new SelectFromDataBaseException(ExceptionMessage.ValidatingToken, e);
         Logger.Log.Error(ex.Message, ex);
         throw ex;
     }
 }
        public async void GetsAllProducts()
        {
            //Arrange
            var options = InMemoryDb("GetsAllProducts");

            //Act
            using (var context = new MSDbContext(options))
            {
                var product = new Product
                {
                    Id    = 6,
                    Price = 150.55M,
                };
                context.Add(product);
                product = new Product
                {
                    Id    = 7,
                    Price = 150.55M,
                };
                context.Add(product);
                context.SaveChanges();
            }
            //Assert
            using (var context = new MSDbContext(options))
            {
                var productRepo = new ProductRepository(context);
                var products    = await productRepo.GetAllAsync();

                Assert.Equal(6, products.Count());
            }
        }
Exemple #10
0
        public async Task AddsCustomerToDb()
        {
            //Arrange - create an object to configure your inmemory DB.
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .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 MSDbContext(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 MSDbContext(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);
            }
        }
Exemple #11
0
 public static bool DeleteUser(int id)
 {
     try
     {
         var user = new User()
         {
             Id = id
         };
         using (var dbCtx = new MSDbContext())
         {
             dbCtx.Users.Remove(user);
             int result = dbCtx.SaveChanges();
             if (result == 1)
             {
                 return(true);
             }
             else
             {
                 throw new Exception();
             }
         }
     }
     catch (Exception e)
     {
         var ex = new DeleteFromDataBaseException(ExceptionMessage.DeleteFromUsersException, e);
         Logger.Log.Error(ex.Message, ex);
         throw ex;
     }
 }
        public void CustomerSearchByUserByLastName()
        {
            // Arrange
            var options = new DbContextOptionsBuilder <MSDbContext>()
                          .UseInMemoryDatabase(databaseName: "CustomerByName")
                          .Options;
            Customer cust = new Customer
            {
                FirstName = "Test User",
                LastName  = "Test Last"
            };

            string lastName = "Test";

            //Act
            using (var context = new MSDbContext(options))
            {
                context.Add(cust);
                context.SaveChanges();

                var result = context.Customers
                             .Where(c => c.LastName.Contains(lastName))
                             .AsNoTracking().FirstOrDefault();

                //Assert
                Assert.Equal(cust.LastName, result.LastName);
            }
        }
Exemple #13
0
 public static int InsertActivation(NewActivationReq newActivation)
 {
     try
     {
         var activation = new Activation()
         {
             UserId         = newActivation.UserId,
             ActivationCode = newActivation.ActicationCode.ToString(),
             IsActive       = true
         };
         using (var dbCtx = new MSDbContext())
         {
             dbCtx.Activations.Add(activation);
             int result = dbCtx.SaveChanges();
             if (result == 1)
             {
                 return(activation.Id);
             }
             else
             {
                 throw new Exception();
             }
         }
     }
     catch (Exception e)
     {
         var ex = new InsertIntoDataBaseException(ExceptionMessage.InsertIntoActivationsException, e);
         Logger.Log.Error(ex.Message, ex);
         throw ex;
     }
 }
        public static int InsertServerInfo(BE.Entities.Request.ServerInfoReq serverInfo)
        {
            try
            {
                var server = new Entities.Server()
                {
                    Name       = serverInfo.Name,
                    Address    = serverInfo.Address,
                    Tel        = serverInfo.Tel,
                    IsActive   = true,
                    CategoryId = serverInfo.CategoryId
                };
                using (var dbCtx = new MSDbContext())
                {
                    dbCtx.Servers.Add(server);
                    int result = dbCtx.SaveChanges();

                    if (result == 1)
                    {
                        foreach (var serverPhoto in serverInfo.Photos)
                        {
                            byte[] photoByte      = Convert.FromBase64String(serverPhoto);
                            var    serverPhotoEnt = new Entities.ServerPhoto()
                            {
                                Photo    = photoByte,
                                ServerId = server.Id
                            };
                            dbCtx.ServerPhotos.Add(serverPhotoEnt);
                            dbCtx.SaveChanges();
                        }
                        return(server.Id);
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
            }
            catch (Exception e)
            {
                var ex = new InsertIntoDataBaseException(ExceptionMessage.InsertIntoServersException, e);
                Logger.Log.Error(ex.Message, ex);
                throw ex;
            }
        }
        private void CreateOneCustomer(MSDbContext context)
        {
            var customer = new Customer
            {
                Id        = 1,
                FirstName = "Conan",
                LastName  = "perse",
                Email     = "*****@*****.**"
            };

            context.Add(customer);
            context.SaveChanges();
        }
        private void CreateTwoproducts(MSDbContext context)
        {
            var product = new Product
            {
                Id    = 1,
                Price = 150.55M,
            };

            context.Add(product);
            product = new Product
            {
                Id    = 2,
                Price = 150.55M,
            };
            context.Add(product);
            context.SaveChanges();
        }
 public static bool DeleteServiceProviderLike(ServiceProviderLikeReq likeReq)
 {
     try
     {
         using (var dbCtx = new MSDbContext())
         {
             dbCtx.ServiceProviderLike.RemoveRange(
                 dbCtx.ServiceProviderLike.Where(x => x.User.Tel == likeReq.UserMobileNumber && x.ServerId == likeReq.ServerId));
             dbCtx.SaveChanges();
             return(true);
         }
     }
     catch (Exception e)
     {
         var ex = new InsertIntoDataBaseException(ExceptionMessage.DeleteServiceProviderLikeException, e);
         Logger.Log.Error(ex.Message, ex);
         throw ex;
     }
 }
        public static int InsertServiceProviderLike(ServiceProviderLikeReq likeReq)
        {
            try
            {
                using (var dbCtx = new MSDbContext())
                {
                    var user           = UserAccounts.SelectUserByTel(likeReq.UserMobileNumber);
                    var checkExistLike = (from likes in dbCtx.ServiceProviderLike
                                          where likes.ServerId == likeReq.ServerId && likes.UserId == user.Id
                                          select likes).Count();
                    if (checkExistLike > 0)
                    {
                        return(0);
                    }
                    var serviceProvideLike = new Entities.ServiceProviderLike()
                    {
                        ServerId = likeReq.ServerId,
                        UserId   = user.Id
                    };
                    dbCtx.ServiceProviderLike.Add(serviceProvideLike);
                    int result = dbCtx.SaveChanges();

                    if (result == 1)
                    {
                        return(serviceProvideLike.Id);
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
            }
            catch (Exception e)
            {
                var ex = new InsertIntoDataBaseException(ExceptionMessage.InsertIntoServiceProviderLikeException, e);
                Logger.Log.Error(ex.Message, ex);
                throw ex;
            }
        }
        public void GetsOrderLineItem()
        {
            var options = InMemoryDb("GetsOrderLineItems");

            //Act
            using (var context = new MSDbContext(options))
            {
                var orderLineItem = new OrderLineItem
                {
                    Id       = 45,
                    OrderId  = 5,
                    Price    = 25M,
                    Quantity = 4
                };
                context.Add(orderLineItem);
                context.SaveChanges();

                // Assert
                var result = context.OrderLineItems.Where(o => o.Id == orderLineItem.Id).AsNoTracking().FirstOrDefault();

                Assert.Equal(orderLineItem.Quantity, result.Quantity);
            }
        }
        public async void GetsAllOrdersForLocation()
        {
            //Arrange
            var options = InMemoryDb("GetsLocationOrders");

            //Act
            using (var context = new MSDbContext(options))
            {
                var customer = new Customer
                {
                    Id        = 10,
                    FirstName = "Conan",
                    LastName  = "perse",
                    Email     = "*****@*****.**"
                };
                context.Add(customer);
                context.SaveChanges();

                var product = new Product
                {
                    Id    = 11,
                    Price = 150.55M,
                };
                context.Add(product);
                product = new Product
                {
                    Id    = 12,
                    Price = 150.55M,
                };
                context.Add(product);
                context.SaveChanges();

                var store = new Store
                {
                    Id   = 4,
                    Name = "Plorida"
                };
                context.Add(store);
                context.SaveChanges();

                var order = new Order
                {
                    Id        = 13,
                    OrderDate = DateTime.Now,
                    StoreId   = 1,
                };
                context.Add(order);
                order = new Order
                {
                    Id        = 3,
                    OrderDate = DateTime.Now,
                    StoreId   = 1,
                };
                context.Add(order);
                context.SaveChanges();
            }
            //Assert
            using (var context = new MSDbContext(options))
            {
                IHttpContextAccessor contextAccessor = new HttpContextAccessor();
                var shoppingCartRepo = new ShoppingCart(context);
                var orderRepo        = new OrderService(context, shoppingCartRepo, contextAccessor);
                var orders           = await orderRepo.FindAsync(o => o.StoreId == 1 && o.Id == 2);

                Assert.Equal(0, orders.Select(i => i.Id).FirstOrDefault());
            }
        }