コード例 #1
0
        public bool Update(User user)
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                var userEntity = context.Users.SingleOrDefault(p => string.Equals(p.Email, user.Email, StringComparison.InvariantCultureIgnoreCase));

                if (userEntity != null)
                {
                    userEntity.FirstName    = user.FirstName;
                    userEntity.LastName     = user.LastName;
                    userEntity.DateOfBirth  = user.DateOfBirth;
                    userEntity.IsSubscribed = user.IsSubscribed;
                    userEntity.Email        = user.Email;

                    if (!string.IsNullOrEmpty(user.Password))
                    {
                        user.Password = _passwordEncryptionService.SetPassword(user.Password);
                    }

                    context.Entry(userEntity).State = EntityState.Modified;
                    context.Users.Update(userEntity);
                    context.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
コード例 #2
0
 public List <User> GetAllUsers()
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Users.ToList());
     }
 }
コード例 #3
0
 /// <summary>
 /// If isUnitTest = true, this boots up the database without any products as it relies on file structure for images
 /// </summary>
 /// <param name="isUnitTest"></param>
 public void InitializeEFInMemoryDatabase(bool isUnitTest)
 {
     using (context = new ECommerceContextDb(GetOptions()))
     {
         LoadData(context, isUnitTest);
     }
 }
コード例 #4
0
 public Role GetRoleById(int roleId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Roles.Single(p => p.Id == roleId));
     }
 }
コード例 #5
0
 public Category GetCategoryById(int categoryId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Categories.Single(p => p.Id == categoryId));
     }
 }
コード例 #6
0
 public bool BrandHasProducts(int brandId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Products.Any(p => p.BrandId == brandId));
     }
 }
コード例 #7
0
 /// <summary>
 /// Gets all Categories
 /// </summary>
 /// <returns></returns>
 public List <Category> GetSubMenuItems()
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Categories.ToList());
     }
 }
コード例 #8
0
 public List <Brand> GetAllBrands()
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Brands.ToList());
     }
 }
コード例 #9
0
 public bool RoleHasUsers(int roleId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Users.Any(p => p.RoleId == roleId));
     }
 }
コード例 #10
0
 public bool IsEmailInUse(string emailAddress)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Users.Any(p => string.Equals(p.Email, emailAddress, StringComparison.InvariantCultureIgnoreCase)));
     }
 }
コード例 #11
0
 public bool CategoryHasProducts(int categoryId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Products.Any(p => p.CategoryId == categoryId));
     }
 }
コード例 #12
0
 public List <ProductType> GetAllProductTypes()
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.ProductTypes.ToList());
     }
 }
コード例 #13
0
 public Brand GetBrandById(int brandId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Brands.Single(p => p.Id == brandId));
     }
 }
コード例 #14
0
 public ProductType GetProductTypeById(int productTypeId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.ProductTypes.Single(p => p.Id == productTypeId));
     }
 }
コード例 #15
0
 public User GetUserById(int userId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Users.SingleOrDefault(p => p.Id == userId));
     }
 }
コード例 #16
0
        /// <summary>
        /// Decreases the quantity of a Product by 1.
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="sizeId"></param>
        /// <returns></returns>
        public bool ReduceProductQuantity(int productId, int sizeId, int quantityToReduce)
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                var product = context.Products.
                              Include("ProductType")
                              .SingleOrDefault(p => p.Id == productId);

                if (product == null)
                {
                    return(false);
                }

                var productSizes = context.ProductSizes.SingleOrDefault(p => p.ProductTypeId == product.ProductTypeId);
                var size         = context.ProductSizes.SingleOrDefault(p => p.Id == sizeId);

                if (size == null)
                {
                    return(false);
                }

                size.Quantity            -= quantityToReduce;
                context.Entry(size).State = EntityState.Modified;
                context.SaveChanges();

                return(true);
            }
        }
コード例 #17
0
 /// <summary>
 /// Gets the items for the Carousel on the Home page.
 /// </summary>
 /// <returns></returns>
 public List <Product> GetHomeCarouselItems()
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Products.
                Include("Images").
                Where(p => p.Images.Any(c => c.FilePath.Contains("hero"))).ToList());
     }
 }
コード例 #18
0
 public List <Transaction> GetDailyTakings(int numberOfDays)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Transactions
                .Where(p => p.CreatedDate > DateTime.Now.AddDays(-numberOfDays) && p.CreatedDate <= DateTime.Now)
                .Take(5).ToList());
     }
 }
コード例 #19
0
 /// <summary>
 /// Creates a Transaction with the new Order
 /// </summary>
 /// <param name="newOrder"></param>
 /// <returns></returns>
 public Order PlaceOrder(Order newOrder)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         newOrder.Id = context.Orders.Add(newOrder).Entity.Id;
         newOrder.ReferenceNumber = Guid.NewGuid().ToString();
         context.SaveChanges();
         return(newOrder);
     }
 }
コード例 #20
0
        public bool Add(Category category)
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                var categoryEntity = context.Categories.Add(category);
                context.Entry(category).State = EntityState.Added;
                context.SaveChanges();

                return(context.Categories.Any(p => p.Id == categoryEntity.Entity.Id));
            }
        }
コード例 #21
0
        public List <Transaction> GetLatestTransactions()
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                var startDate = DateTime.Now.AddDays(-8);
                var endDate   = DateTime.Now.AddDays(-1);

                return(context.Transactions
                       .Where(p => p.CreatedDate >= startDate && p.CreatedDate <= endDate).ToList());
            }
        }
コード例 #22
0
        public bool Add(Role role)
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                var roleEntity = context.Roles.Add(role);
                context.Entry(role).State = EntityState.Added;
                context.SaveChanges();

                return(context.Brands.Any(p => p.Id == roleEntity.Entity.Id));
            }
        }
コード例 #23
0
 /// <summary>
 /// Gets the Product based off of the Product ID provided.
 /// </summary>
 /// <param name="productId"></param>
 /// <returns></returns>
 public Product GetProductById(int productId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Products.
                Include("Images").
                Include("Brand").
                Include("Sizes").
                Include("ProductType").Single(p => p.Id == productId));
     }
 }
コード例 #24
0
        public bool Add(Brand brand)
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                var brandEntity = context.Brands.Add(brand);
                context.Entry(brand).State = EntityState.Added;
                context.SaveChanges();

                return(context.Brands.Any(p => p.Id == brandEntity.Entity.Id));
            }
        }
コード例 #25
0
 public List <Product> GetAllProducts()
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(context.Products.
                Include("Images").
                Include("Brand").
                Include("Sizes").
                Include("ProductType")
                .ToList());
     }
 }
コード例 #26
0
        public List <User> GetLatestNewUsers(int numberOfUsers)
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                var startDate = DateTime.Now.AddDays(-7);
                var endDate   = DateTime.Now;

                return(context.Users
                       .Where(p => p.CreatedDate >= startDate && p.CreatedDate <= endDate)
                       .Take(numberOfUsers).ToList());
            }
        }
コード例 #27
0
 public bool ProductNameExists(string productName, int?productId)
 {
     using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
     {
         return(productId != null?
                context.Products
                .Any(p => string.Equals(p.ProductName, productName, StringComparison.InvariantCultureIgnoreCase) &&
                     p.Id != productId.Value) :
                    context.Products
                    .Any(p => string.Equals(p.ProductName, productName, StringComparison.InvariantCultureIgnoreCase)));
     }
 }
コード例 #28
0
        public bool BrandNameExists(Brand brand)
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                if (brand.Id != 0)
                {
                    return(context.Brands.Any(p => string.Equals(brand.BrandName, p.BrandName, StringComparison.InvariantCultureIgnoreCase) && p.Id != brand.Id));
                }

                return(context.Brands.Any(p => string.Equals(brand.BrandName, p.BrandName, StringComparison.InvariantCultureIgnoreCase)));
            }
        }
コード例 #29
0
        /// <summary>
        /// Gets a list of products selected at random with the maximum amount being provided in the parameter.
        /// </summary>
        /// <param name="numberOfProducts"></param>
        /// <returns></returns>
        public List <Product> GetRandomProducts(int numberOfProducts)
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                var listAllProducts = context.Products.
                                      Include("Images").
                                      Include("Brand").
                                      Include("Sizes").
                                      Include("ProductType").ToList();

                return(listAllProducts.Take(3).ToList());
            }
        }
コード例 #30
0
        /// <summary>
        /// Gets a list of products that the User has previously viewed or purchased.
        /// </summary>
        /// <param name="numberOfProducts"></param>
        /// <returns></returns>
        public List <Product> GetSuggestedProductsByUser(int userId)
        {
            using (var context = new ECommerceContextDb(new ECommerceDatabase.StartupDatabase().GetOptions()))
            {
                var listAllProducts = context.Products.
                                      Include("Images").
                                      Include("Brand").
                                      Include("Sizes").
                                      Include("ProductType").ToList();

                return(listAllProducts.Take(3).ToList());
            }
        }