Example #1
0
        public int Create(WishlistProduct newWp)
        {
            string sql = @"
        INSERT INTO wishlistproducts
        (wishlistId, productId, creatorId)
        VALUES
        (@WishlistId, @ProductId, @CreatorId);
        SELECT LAST_INSERT_ID();";

            return(_db.ExecuteScalar <int>(sql, newWp));
        }
Example #2
0
        public async Task <ActionResult <WishlistProduct> > Post([FromBody] WishlistProduct newWp)
        {
            try
            {
                Profile userInfo = await HttpContext.GetUserInfoAsync <Profile>();

                newWp.CreatorId = userInfo.Id;
                return(Ok(_wps.Create(newWp)));
            }
            catch (System.Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
        public ActionResult AdminPostWishlistItems([FromBody] WishlistViewModel _wishlistItem, int Id)
        {
            WishlistViewModelValidator validator = new WishlistViewModelValidator();
            ValidationResult           results   = validator.Validate(_wishlistItem);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    Errors.AddErrorToModelState(failure.PropertyName, failure.ErrorMessage, ModelState);
                }
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_context.Users.Find(Id) != null)
            {
                var wishlistId = (from wishlist in _context.Wishlists
                                  where wishlist.UserId == Id
                                  select wishlist.Id).ToArray();

                var exists = (from wl in _context.WishlistProduct
                              where wl.Wishlist.UserId == Id && wl.ProductId == _wishlistItem.ProductId
                              select wl).ToArray();

                if (exists.Length != 0)
                {
                    _context.WishlistProduct.Remove(exists[0]);
                    //return Ok("Staat al in Wishlist");
                }

                else
                {
                    WishlistProduct product = new WishlistProduct()
                    {
                        WishlistId = wishlistId[0],
                        ProductId  = _wishlistItem.ProductId
                    };

                    _context.Add(product);
                }

                _context.SaveChanges();
                return(Ok());
            }
            return(NotFound());
        }
        public ActionResult PostWishlistItems([FromBody] WishlistViewModel _wishlistItem)
        {
            WishlistViewModelValidator validator = new WishlistViewModelValidator();
            ValidationResult           results   = validator.Validate(_wishlistItem);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    Errors.AddErrorToModelState(failure.PropertyName, failure.ErrorMessage, ModelState);
                }
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userId     = _caller.Claims.Single(c => c.Type == "id");
            var wishlistId = (from wishlist in _context.Wishlists
                              where wishlist.UserId == int.Parse(userId.Value)
                              select wishlist.Id).ToArray();

            var exists = (from wl in _context.WishlistProduct
                          where wl.Wishlist.UserId == int.Parse(userId.Value) && wl.ProductId == _wishlistItem.ProductId
                          select wl).ToArray();

            if (exists.Length != 0)
            {
                _context.WishlistProduct.Remove(exists[0]);
                //return Ok("Staat al in Wishlist");
            }

            else
            {
                WishlistProduct product = new WishlistProduct()
                {
                    WishlistId = wishlistId[0],
                    ProductId  = _wishlistItem.ProductId
                };

                _context.Add(product);
            }

            _context.SaveChanges();
            return(Ok());
        }
Example #5
0
        internal string Delete(int id, string userId)
        {
            WishlistProduct original = _repo.Get(id);

            if (original == null)
            {
                throw new Exception("Bad Id");
            }
            if (original.CreatorId != userId)
            {
                throw new Exception("Not the User : Access Denied");
            }
            if (_repo.Remove(id))
            {
                return("deleted succesfully");
            }
            return("did not remove succesfully");
        }
        public int Update(string id, string productId, string productType)
        {
            var testfilter = Builders <Wishlist> .Filter.ElemMatch(x => x.products, x => x.productID == productId);

            var res = _wishlists.Find(testfilter).FirstOrDefault();

            if (res == null)
            {
                WishlistProduct newProd = new WishlistProduct();
                newProd.productID   = productId;
                newProd.productType = productType;
                var filter = Builders <Wishlist> .Filter.Eq(e => e.Id, id);

                var update = Builders <Wishlist> .Update.Push <WishlistProduct>(e => e.products, newProd);

                var result = _wishlists.FindOneAndUpdateAsync(filter, update);
                return(1);
            }
            return(0);
        }
Example #7
0
        public async Task <Result> AddProductAsync(
            int productId, string userId)
        {
            var wishlist = await this
                           .All()
                           .FirstOrDefaultAsync(w => w.UserId == userId);

            wishlist ??= new Wishlist
            {
                UserId = userId
            };

            var wishlistProduct = new WishlistProduct
            {
                Wishlist  = wishlist,
                ProductId = productId
            };

            await this.Data.AddAsync(wishlistProduct);

            await this.Data.SaveChangesAsync();

            return(Result.Success);
        }
Example #8
0
 public WishlistProduct Create(WishlistProduct newWp)
 {
     newWp.Id = _repo.Create(newWp);
     return(newWp);
 }