Exemple #1
0
        public async Task <ProductPostResponse> Post([FromBody] ProductRequest products)
        {
            var response = new ProductPostResponse();

            if (new EmailAddressAttribute().IsValid(products.Email) && products.Password != "")
            {
                var passwordBytes = System.Text.Encoding.UTF8.GetBytes(products.Password);
                var user          = (from u in _shopoContext.Users
                                     where u.Email == products.Email && u.Password == passwordBytes
                                     select new Users {
                    UserId = u.UserId
                }).ToList();
                if (user.Count > 0 && user[0].UserId == 16)
                {
                    Products product = new Products();
                    product.Size              = products.Size;
                    product.Color             = products.Size;
                    product.Name              = products.Name;
                    product.Description       = products.Description;
                    product.Price             = products.Price;
                    product.AvailableQuantity = products.AvailableQuantity;
                    var encodedPicture = Convert.FromBase64String(products.Picture);
                    product.Picture  = encodedPicture;
                    response.product = product;
                    if (response.product != null)
                    {
                        response.Success = true;
                        _shopoContext.Products.Add(product);
                        await _shopoContext.SaveChangesAsync();
                    }
                }
                else
                {
                    NotFound();
                }
            }
            else
            {
                response.product = new Products();
                response.Success = false;
            }
            return(response);
        }
        public async Task <PostResponse> Post([FromBody] UserBody user)
        {
            var response = new PostResponse();

            if (new EmailAddressAttribute().IsValid(user.Email))
            {
                if (!_shopoContext.Users.Any(u => u.Email == user.Email))
                {
                    var encodedPassword = System.Text.Encoding.UTF8.GetBytes(user.Password);

                    Users newUser = new Users();
                    newUser.Email       = user.Email;
                    newUser.Name        = user.Name;
                    newUser.DateOfBirth = user.DateOfBirth;
                    newUser.Phone       = user.Phone;
                    newUser.Activated   = user.Activated;
                    newUser.DateJoined  = DateTime.Now;
                    newUser.UpdatedDate = DateTime.Now;
                    newUser.Password    = encodedPassword;

                    _shopoContext.Users.Add(newUser);
                    await _shopoContext.SaveChangesAsync();

                    var fetchUser = (from u in _shopoContext.Users
                                     where u.Email == newUser.Email
                                     select new Users {
                        UserId = u.UserId
                    }).ToList();

                    response.Id = fetchUser[0].UserId;
                }
            }
            else
            {
                BadRequest();
            }
            return(response);
        }
        public async Task <ActionResult <IEnumerable <Orders> > > Post([FromBody] int userId)
        {
            bool orderPlaced      = false;
            bool orderIdGenerated = false;
            bool orderIdFetched   = false;

            try
            {
                var productsInCart = (from c in _context.Carts
                                      where c.UserId == userId
                                      select new Carts
                {
                    CartId = c.CartId,
                    ProductId = c.ProductId,
                    UserId = c.UserId,
                    AddressId = c.AddressId,
                    Quantity = c.Quantity,
                    TimeAdded = c.TimeAdded
                }).ToList();

                if (productsInCart.Count > 0)
                {
                    foreach (var eachProduct in productsInCart)
                    {
                        var products = (from p in _context.Products
                                        where p.ProductId == eachProduct.ProductId
                                        select new Products
                        {
                            ProductId = p.ProductId,
                            Size = p.Size,
                            Color = p.Color,
                            Name = p.Name,
                            Description = p.Description,
                            Price = p.Price,
                            AvailableQuantity = p.AvailableQuantity,
                            Picture = p.Picture
                        }).ToList();

                        var fetchingOrderId = (from o in _context.Orders
                                               where o.UserId == userId
                                               orderby o.OrderId descending
                                               select new Orders
                        {
                            OrderId = o.OrderId
                        }).ToList();

                        int remainingQuantity = products[0].AvailableQuantity - eachProduct.Quantity;

                        if (remainingQuantity > 0)
                        {
                            int newOrderId;

                            if (fetchingOrderId.Count > 0 && orderIdFetched == false)
                            {
                                newOrderId       = fetchingOrderId[0].OrderId;
                                newOrderId      += 1;
                                orderIdFetched   = true;
                                orderIdGenerated = true;
                            }
                            else if (orderIdFetched == true && orderIdGenerated == true)
                            {
                                newOrderId = fetchingOrderId[0].OrderId;
                            }
                            else
                            {
                                newOrderId = 1;
                            }


                            Orders order = new Orders();
                            order.UserId        = userId;
                            order.ProductId     = eachProduct.ProductId;
                            order.CartId        = eachProduct.CartId;
                            order.Quantity      = eachProduct.Quantity;
                            order.Status        = "order placed";
                            order.Amount        = products[0].Price;
                            order.OrderDateTime = DateTime.Now;
                            order.OrderId       = newOrderId;

                            _context.Orders.Add(order);
                            await _context.SaveChangesAsync();

                            orderPlaced = true;

                            Products updateProductsQty = new Products();
                            updateProductsQty.ProductId         = eachProduct.ProductId;
                            updateProductsQty.Size              = products[0].Size;
                            updateProductsQty.Color             = products[0].Color;
                            updateProductsQty.Name              = products[0].Name;
                            updateProductsQty.Description       = products[0].Description;
                            updateProductsQty.Price             = products[0].Price;
                            updateProductsQty.AvailableQuantity = remainingQuantity;
                            updateProductsQty.Picture           = products[0].Picture;

                            _context.Products.Update(updateProductsQty);
                            await _context.SaveChangesAsync();
                        }
                        else
                        {
                            return(BadRequest("sorry this product is not available"));
                        }
                    }

                    return(await _context.Orders.ToListAsync());
                }
                else
                {
                    return(BadRequest("no products in the cart for this user id"));
                }
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }
Exemple #4
0
        public async Task <ActionResult <IEnumerable <Carts> > > Post([FromBody] int productid, [FromQuery] int userId, [FromQuery] int quantity)
        {
            try
            {
                var products = (from p in _context.Products
                                where p.ProductId == productid
                                select new Products
                {
                    AvailableQuantity = p.AvailableQuantity
                }).ToList();

                var address = (from a in _context.Addresses
                               where a.UserId == userId
                               select new Addresses
                {
                    AddressId = a.AddressId
                }).ToList();

                foreach (var item in products)
                {
                    if (products != null)
                    {
                        productIsAvailable = true;
                        availableProducts  = item.AvailableQuantity;
                    }
                }

                foreach (var item in address)
                {
                    if (address != null)
                    {
                        addressIdFetched = true;
                        addressId        = item.AddressId;
                    }
                }

                var productsInCart = (from c in _context.Carts
                                      where c.ProductId == productid && c.UserId == userId && c.Quantity >= 1
                                      select new Carts
                {
                    CartId = c.CartId,
                    ProductId = c.ProductId,
                    UserId = c.UserId,
                    AddressId = c.AddressId,
                    Quantity = c.Quantity,
                    TimeAdded = c.TimeAdded
                }).ToList();

                if (productsInCart.Count > 0)
                {
                    foreach (var eachProduct in productsInCart)
                    {
                        if (eachProduct.Quantity >= 1)
                        {
                            int   new_quantity = eachProduct.Quantity + quantity;
                            Carts cart         = new Carts();
                            cart.CartId    = eachProduct.CartId;
                            cart.ProductId = eachProduct.ProductId;
                            cart.UserId    = eachProduct.UserId;
                            cart.AddressId = eachProduct.AddressId;
                            cart.Quantity  = new_quantity;
                            cart.TimeAdded = DateTime.Now;

                            _context.Carts.Update(cart);
                            await _context.SaveChangesAsync();

                            return(await _context.Carts.ToListAsync());
                        }
                    }
                    await _context.SaveChangesAsync();

                    return(await _context.Carts.ToListAsync());
                }

                else
                {
                    if (productIsAvailable == true && availableProducts > 0 && addressIdFetched == true)
                    {
                        Carts cart = new Carts();
                        cart.ProductId = productid;
                        cart.UserId    = userId;
                        cart.AddressId = addressId;
                        cart.Quantity  = quantity;
                        cart.TimeAdded = DateTime.Now;

                        _context.Carts.Add(cart);
                        await _context.SaveChangesAsync();

                        return(await _context.Carts.ToListAsync());
                    }
                    await _context.SaveChangesAsync();

                    return(await _context.Carts.ToListAsync());
                }
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }