예제 #1
0
 private bool ProductExists(int id)
 {
     using (var context = new TackleHackSQLContext())
     {
         return(context.Product.Any(e => e.Id == id));
     }
 }
예제 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Phone,Email")] Vendor vendor)
        {
            if (id != vendor.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    using (var context = new TackleHackSQLContext())
                    {
                        context.Update(vendor);
                        await context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!VendorExists(vendor.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(vendor));
        }
예제 #3
0
        // GET: Product/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            using (var context = new TackleHackSQLContext())
            {
                var product = await context.Product.FindAsync(id);

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

                var vendorIds = await context.Membership
                                .Where(x => x.UserName == User.Identity.Name)
                                .Select(x => x.VendorId)
                                .ToListAsync();

                var vendors = await context.Vendor
                              .Where(x => vendorIds.Contains(x.Id))
                              .ToListAsync();

                ViewData["VendorId"] = new SelectList(vendors, "Id", "Name", product.VendorId);
                return(View(product));
            }
        }
        public async Task <IActionResult> Review(String reviewText, String userName, int productId, int productRating)
        {
            using (var context = new TackleHackSQLContext())
            {
                var review = new Review()
                {
                    Text     = reviewText,
                    UserName = userName,
                    DateTime = DateTime.Now,
                    Rating   = productRating
                };
                context.Add(review);
                await context.SaveChangesAsync();

                var productReview = new ProductReview()
                {
                    ReviewId  = review.Id,
                    ProductId = productId
                };
                context.Add(productReview);
                await context.SaveChangesAsync();

                return(RedirectToAction("Details", new { id = productId }));
            }
        }
예제 #5
0
 private bool VendorExists(int id)
 {
     using (var context = new TackleHackSQLContext())
     {
         return(context.Vendor.Any(e => e.Id == id));
     }
 }
예제 #6
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            using (var context = new TackleHackSQLContext())
            {
                var productFeatures = context.ProductFeature.Where(x => x.ProductId == id);
                context.ProductFeature.RemoveRange(productFeatures);
                await context.SaveChangesAsync();

                var productReviews = context.ProductReview.Where(x => x.ProductId == id);
                context.ProductReview.RemoveRange(productReviews);
                await context.SaveChangesAsync();

                var cartItems = context.Cart.Where(x => x.ProductId == id);
                context.Cart.RemoveRange(cartItems);
                await context.SaveChangesAsync();

                var media = context.Media.Where(x => x.ProductId == id);
                context.Media.RemoveRange(media);
                await context.SaveChangesAsync();

                var product = await context.Product.FindAsync(id);

                context.Product.Remove(product);
                await context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
        }
예제 #7
0
 // GET: Cart
 public async Task <IActionResult> Index()
 {
     using (var context = new TackleHackSQLContext())
     {
         var tackleHackSQLContext = context.Cart.Include(c => c.Product).Where(x => x.UserName == User.Identity.Name);
         return(View(await tackleHackSQLContext.ToListAsync()));
     }
 }
 // GET: Shop
 public async Task <IActionResult> Index()
 {
     using (var context = new TackleHackSQLContext())
     {
         var tackleHackSQLContext = context.Product.Include(p => p.Vendor);
         return(View(await tackleHackSQLContext.ToListAsync()));
     }
 }
예제 #9
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            using (var context = new TackleHackSQLContext())
            {
                var vendor = await context.Vendor.FindAsync(id);

                context.Vendor.Remove(vendor);
                await context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index)));
        }
예제 #10
0
        // POST: Cart/Delete/5
        public async Task <IActionResult> Delete(int id)
        {
            using (var context = new TackleHackSQLContext())
            {
                var cart = await context.Cart.FindAsync(id);

                context.Remove(cart);
                await context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
        }
예제 #11
0
        public async Task <IActionResult> Create([Bind("Id,ItemNumber,BrandName,ProductName,Description,Sku,Msrp,VendorId")] Product product)
        {
            using (var context = new TackleHackSQLContext())
            {
                if (ModelState.IsValid)
                {
                    context.Add(product);
                    await context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                ViewData["VendorId"] = new SelectList(await context.Vendor.ToListAsync(), "Id", "Name", product.VendorId);
                return(View(product));
            }
        }
예제 #12
0
 public static void Initialize()
 {
     using (var context = new TackleHackSQLContext())
     {
         if (context.Account.Count() == 0)
         {
             var account = new Account()
             {
                 Name        = "Demo",
                 Description = "Account for demo"
             };
             context.Account.Add(account);
             context.SaveChanges();
         }
     }
 }
예제 #13
0
        // GET: Vendor
        public async Task <IActionResult> Index()
        {
            using (var context = new TackleHackSQLContext())
            {
                var vendorIds = await context.Membership
                                .Where(x => x.UserName == User.Identity.Name)
                                .Select(x => x.VendorId)
                                .ToListAsync();

                var vendors = await context.Vendor
                              .Where(x => vendorIds.Contains(x.Id))
                              .ToListAsync();

                return(View(vendors));
            }
        }
예제 #14
0
        public async Task <IActionResult> CartItemBadge()
        {
            var userName = User.Identity.Name;

            if (userName == null)
            {
                return(NotFound());
            }

            using (var context = new TackleHackSQLContext())
            {
                var cartItems = await context.Cart.Where(x => x.UserName == userName).ToListAsync();

                return(Ok(cartItems.Count()));
            }
        }
예제 #15
0
        // GET: Product/Create
        public async Task <IActionResult> CreateAsync()
        {
            using (var context = new TackleHackSQLContext())
            {
                var vendorIds = await context.Membership
                                .Where(x => x.UserName == User.Identity.Name)
                                .Select(x => x.VendorId)
                                .ToListAsync();

                var vendors = await context.Vendor
                              .Where(x => vendorIds.Contains(x.Id))
                              .ToListAsync();

                ViewData["VendorId"] = new SelectList(vendors, "Id", "Name");
                return(View());
            }
        }
        public async Task <IActionResult> AddToCart(String userName, int productId)
        {
            using (var context = new TackleHackSQLContext())
            {
                var cart = new Cart()
                {
                    UserName  = userName,
                    DateTime  = DateTime.Now,
                    ProductId = productId,
                    Quantity  = 1,
                    Status    = 0
                };
                context.Add(cart);
                await context.SaveChangesAsync();

                return(RedirectToAction("Details", new { id = productId }));
            }
        }
예제 #17
0
        // GET: Vendor/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            using (var context = new TackleHackSQLContext())
            {
                var vendor = await context.Vendor.FindAsync(id);

                if (vendor == null)
                {
                    return(NotFound());
                }
                return(View(vendor));
            }
        }
예제 #18
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,ItemNumber,BrandName,ProductName,Description,Sku,Msrp,VendorId")] Product product)
        {
            if (id != product.Id)
            {
                return(NotFound());
            }

            using (var context = new TackleHackSQLContext())
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        context.Update(product);
                        await context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!ProductExists(product.Id))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return(RedirectToAction(nameof(Index)));
                }

                var vendorIds = await context.Membership
                                .Where(x => x.UserName == User.Identity.Name)
                                .Select(x => x.VendorId)
                                .ToListAsync();

                var vendors = await context.Vendor
                              .Where(x => vendorIds.Contains(x.Id))
                              .ToListAsync();

                ViewData["VendorId"] = new SelectList(vendors, "Id", "Name", product.VendorId);
                return(View(product));
            }
        }
예제 #19
0
        // GET: Vendor/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            using (var context = new TackleHackSQLContext())
            {
                var vendor = await context.Vendor
                             .FirstOrDefaultAsync(m => m.Id == id);

                if (vendor == null)
                {
                    return(NotFound());
                }

                return(View(vendor));
            }
        }
        public async Task <ShopItem> GetShopItem(int?id)
        {
            if (id == null)
            {
                return(null);
            }

            var shopItem = new ShopItem();

            using (var context = new TackleHackSQLContext())
            {
                shopItem.Product = await context.Product
                                   .Include(p => p.Vendor)
                                   .Include(p => p.Media)
                                   .Include(p => p.ProductFeature)
                                   .ThenInclude(p => p.Feature)
                                   .Include(p => p.ProductReview)
                                   .ThenInclude(p => p.Review)
                                   .FirstOrDefaultAsync(m => m.Id == id);

                if (shopItem.Product == null)
                {
                    return(null);
                }

                if (shopItem.Product.Media.Count() > 0)
                {
                    shopItem.YouTubeLink         = shopItem.Product.Media.First().Link;
                    shopItem.YouTubeEmbeddedLink = "https://www.youtube.com/embed/"
                                                   + shopItem.YouTubeLink.Substring(shopItem.YouTubeLink.LastIndexOf('/') + 1);
                }

                if (shopItem.Product.ProductReview.Count() > 0)
                {
                    shopItem.PercentReview = Convert.ToInt32(GetPercentReview(shopItem.Product.ProductReview));
                    shopItem.AverageReview = Convert.ToInt32(GetAverageReview(shopItem.Product.ProductReview));
                }

                return(shopItem);
            }
        }
예제 #21
0
        // GET: Product/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            using (var context = new TackleHackSQLContext())
            {
                var product = await context.Product
                              .Include(p => p.Vendor)
                              .FirstOrDefaultAsync(m => m.Id == id);

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

                return(View(product));
            }
        }
        public void LeaveReview(String reviewText, String userName, int productId)
        {
            using (var context = new TackleHackSQLContext())
            {
                var review = new Review()
                {
                    Text     = reviewText,
                    UserName = userName,
                    DateTime = new DateTime(),
                };
                context.Review.Add(review);
                context.SaveChanges();

                var productReview = new ProductReview()
                {
                    ReviewId  = review.Id,
                    ProductId = productId
                };
                context.ProductReview.Add(productReview);
                context.SaveChanges();
            }
        }
예제 #23
0
        public async Task <IActionResult> Create([Bind("Id,Name,Phone,Email")] Vendor vendor)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TackleHackSQLContext())
                {
                    context.Add(vendor);
                    await context.SaveChangesAsync();

                    var membership = new Membership()
                    {
                        AccountId = 1,
                        VendorId  = vendor.Id,
                        UserName  = User.Identity.Name
                    };
                    context.Add(membership);
                    await context.SaveChangesAsync();
                }

                return(RedirectToAction(nameof(Index)));
            }
            return(View(vendor));
        }
예제 #24
0
        public static void ProcessVendorProducts(int vendorId, String filePath)
        {
            using (var context = new TackleHackSQLContext())
            {
                DataTable vendorData     = ReadFileToDataTable(filePath);
                var       columnNames    = vendorData.Columns.Cast <DataColumn>().Select(x => x.ColumnName).ToList();
                var       featureColumns = columnNames.Where(x => x.Contains("Key_Feature"));
                foreach (DataRow row in vendorData.Rows)
                {
                    var product = new Product()
                    {
                        BrandName   = (String)row["BRAND"],
                        ItemNumber  = (String)row["SKU"],
                        Sku         = (String)row["SKU"],
                        ProductName = (String)row["Product Name"],
                        Description = (String)row["Product Description"],
                        Msrp        = (double)row["Price"],
                        VendorId    = vendorId
                    };

                    context.Product.Add(product);
                    context.SaveChanges();

                    foreach (String column in featureColumns)
                    {
                        var description = (row[column] == DBNull.Value ? string.Empty : (String)row[column]);
                        if (!String.IsNullOrEmpty(description))
                        {
                            var feature = context.Feature.Where(x => x.Description.Equals(description)).FirstOrDefault();
                            if (feature == null)
                            {
                                feature = new Feature()
                                {
                                    Description = description
                                };
                                context.Feature.Add(feature);
                                context.SaveChanges();
                            }

                            var productFeature = new ProductFeature()
                            {
                                ProductId = product.Id,
                                FeatureId = feature.Id
                            };
                            context.ProductFeature.Add(productFeature);
                            context.SaveChanges();
                        }
                    }

                    var videoLink = (row["Video"] == DBNull.Value ? string.Empty : (String)row["Video"]);
                    if (!String.IsNullOrEmpty(videoLink))
                    {
                        var media = new Media()
                        {
                            Title     = product.ProductName,
                            Link      = videoLink,
                            ProductId = product.Id
                        };
                        context.Media.Add(media);
                        context.SaveChanges();
                    }
                }
            }
        }