Пример #1
0
        public async Task <string> DeleteProductFromWishList(int?id)
        {
            try
            {
                if (id != null)
                {
                    if (HttpContext.User.Identity.IsAuthenticated)
                    {
                        ProductWishList productWishList = await _baselDb.AppUsers
                                                          .Where(x => x.UserName == HttpContext.User.Identity.Name)
                                                          .Include(x => x.WishList).ThenInclude(x => x.ProductWishLists)
                                                          .Select(x => x.WishList).SelectMany(x => x.ProductWishLists)
                                                          .Where(x => x.ProductId == id)
                                                          .SingleOrDefaultAsync();

                        AppUser appUser = await _baselDb.AppUsers
                                          .Where(x => x.UserName == HttpContext.User.Identity.Name)
                                          .Include(x => x.WishList).ThenInclude(x => x.ProductWishLists)
                                          .SingleOrDefaultAsync();

                        appUser.WishList.ProductWishLists.Remove(productWishList);

                        await _baselDb.SaveChangesAsync();

                        return("Product deleted from WishList");
                    }
                    return("Please, Sign in to Site");
                }
            }
            catch (Exception)
            {
                throw;
            }
            return("");
        }
Пример #2
0
        internal int Create(ProductWishList newPWL)
        {
            string sql = @"
      INSERT INTO productWish
      (productId, wishListId, creatorId)
      VALUES
      (@ProductId, @WishListId, @CreatorId);
      SELECT LAST_INSERT_ID();";

            return(_db.ExecuteScalar <int>(sql, newPWL));
        }
Пример #3
0
        public async Task <ActionResult <ProductWishList> > Create([FromBody] ProductWishList newPWL)
        {
            try
            {
                Profile userInfo = await HttpContext.GetUserInfoAsync <Profile>();

                newPWL.CreatorId = userInfo.Id;
                ProductWishList created = _pwl.Create(newPWL);
                return(Ok(created));
            }
            catch (System.Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
        // POST: api/Whichlists
        public void postAddProductTowishList(int id)
        {
            //get cart of current logged user
            var userID = "1";//User.Identity.GetUserId();

            var wishListID = Getwhichlist().Where(c => c.UserID == userID)
                             .Select(c => c.ID).FirstOrDefault();
            var productWishList = new ProductWishList()
            {
                wishListID = wishListID, ProductID = id
            };

            db.ProductWishLists.Add(productWishList);
            db.SaveChanges();
        }
Пример #5
0
 internal ProductWishList Create(ProductWishList newPWL)
 {
     newPWL.Id = _repo.Create(newPWL);
     return(newPWL);
 }
Пример #6
0
        public async Task <string> WishListAddProduct(int?id)
        {
            try
            {
                if (HttpContext.User.Identity.IsAuthenticated)
                {
                    if (id != null)
                    {
                        AppUser appUser = await _baselDb
                                          .AppUsers.Where(x => x.UserName == HttpContext.User.Identity.Name)
                                          .Include(x => x.WishList).ThenInclude(x => x.ProductWishLists)
                                          .SingleOrDefaultAsync();

                        if (appUser != null)
                        {
                            Product product = await _baselDb.Products
                                              .Where(x => x.Id == id)
                                              .SingleOrDefaultAsync();

                            if (product != null)
                            {
                                if (appUser.WishList == null)
                                {
                                    List <ProductWishList> productWishLists = new List <ProductWishList>
                                    {
                                        new ProductWishList()
                                        {
                                            Product = product
                                        }
                                    };

                                    WishList wishList = new WishList
                                    {
                                        AppUser          = appUser,
                                        ProductWishLists = productWishLists,
                                        AppUserId        = appUser.Id
                                    };

                                    await _baselDb.WishLists.AddAsync(wishList);

                                    await _baselDb.SaveChangesAsync();

                                    WishList newWishList = await _baselDb.WishLists
                                                           .Where(x => x.AppUserId == appUser.Id)
                                                           .SingleOrDefaultAsync();

                                    appUser.WishListId = newWishList.Id;
                                }
                                else
                                {
                                    if (!appUser.WishList.ProductWishLists
                                        .Any(x => x.ProductId == id))
                                    {
                                        ProductWishList productWishList = new ProductWishList()
                                        {
                                            Product   = product,
                                            ProductId = product.Id
                                        };

                                        appUser.WishList.ProductWishLists.Add(productWishList);
                                    }
                                }
                                await _baselDb.SaveChangesAsync();

                                return("Product Added to WishList");
                            }
                        }
                    }
                }
                else
                {
                    return("Please, Sign in to the Site");
                }
            }
            catch (Exception)
            {
                throw;
            }
            return("");
        }