Example #1
0
        public async Task <IActionResult> PutCustomerCart([FromRoute] string id, [FromBody] CustomerCart customerCart)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customerCart.CustomerCartId)
            {
                return(BadRequest());
            }

            _context.Entry(customerCart).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerCartExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutAnonymousCustomer([FromRoute] string id, [FromBody] AnonymousCustomer anonymousCustomer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != anonymousCustomer.AnonymousCustomerId)
            {
                return(BadRequest());
            }

            _context.Entry(anonymousCustomer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AnonymousCustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutGeneralImage([FromRoute] string id, [FromBody] GeneralImage generalImage)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != generalImage.GeneralImageId)
            {
                return(BadRequest());
            }

            _context.Entry(generalImage).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GeneralImageExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutProductStatus([FromRoute] int id, [FromBody] ProductStatus productStatus)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != productStatus.ProductStatusId)
            {
                return(BadRequest());
            }

            _context.Entry(productStatus).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductStatusExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutBrand([FromBody] Brand brand)
        {
            _context.Entry(brand).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(NotFound());
            }

            return(Ok());
        }
        public async Task <IActionResult> UpdateCustomerInfo([FromBody] JObject info)
        {
            var fullname = (string)info.GetValue("fullname");
            var dob      = DateTime.Parse(info.GetValue("dob").ToString());
            var gender   = (string)info.GetValue("gender");

            var currentCustomerId = User.Identities.FirstOrDefault(u => u.IsAuthenticated)
                                    ?.FindFirst(
                c => c.Type == JwtRegisteredClaimNames.NameId || c.Type == ClaimTypes.NameIdentifier)
                                    ?.Value;

            var customer = _context.Users.Find(currentCustomerId);

            customer.DateOfBirth = dob;
            customer.FullName    = fullname;
            customer.Gender      = gender == "male";

            _context.Users.Update(customer);

            if (await _context.SaveChangesAsync() > 0)
            {
                var current = _context.Users.Find(currentCustomerId);

                return(Json(new
                {
                    Status = "Success",
                    Message = "Update user info success",
                    customer = new
                    {
                        current.Email,
                        current.FullName,
                        DOB = current.DateOfBirth,
                        current.Gender
                    }
                }));
            }

            return(Json(new
            {
                Status = "Failed",
                Message = "Cannot update user info"
            }));
        }
Example #7
0
        public async Task <IActionResult> PutCategory([FromBody] Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(NotFound());
            }

            return(Ok());
        }
Example #8
0
        public async Task <IActionResult> AddToCart([FromQuery] string customerId, [FromQuery] int productId, [FromQuery] int?quantity = 1)
        {
            var product = _context.Product.Find(productId);

            if (product == null)
            {
                return(Json(new
                {
                    Status = "Failed",
                    Message = "Cannot find the product"
                }));
            }

            if (quantity > product.MaximumQuantity)
            {
                return(Json(new
                {
                    Status = "Failed",
                    Message = "Product Quantiy is over maximum quantity can order in a time"
                }));
            }

            if (quantity > product.CurrentQuantity)
            {
                return(Json(new
                {
                    Status = "Failed",
                    Message = "Product Quantiy is over current quantity in store"
                }));
            }

            if (User.Identity.IsAuthenticated)
            {
                var currentCustomerId = User.Identities.FirstOrDefault(u => u.IsAuthenticated)
                                        ?.FindFirst(
                    c => c.Type == JwtRegisteredClaimNames.NameId || c.Type == ClaimTypes.NameIdentifier)
                                        ?.Value;

                if (string.IsNullOrEmpty(customerId) || currentCustomerId != customerId)
                {
                    //try to get id through identity
                    customerId = currentCustomerId;
                }

                var cartDetail = _context.CustomerCartDetail.Find(customerId, productId);

                if (cartDetail != null)
                {
                    if (product.MaximumQuantity >= cartDetail.Quantity + quantity)
                    {
                        if (product.CurrentQuantity >= cartDetail.Quantity + quantity)
                        {
                            cartDetail.Quantity += quantity;

                            if (await _context.SaveChangesAsync() > 0)
                            {
                                var cart = _context.CustomerCart.Find(customerId);

                                var quantityRemain = _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId)
                                                     .Sum(c => c.Quantity);
                                var totalPriceRemain = _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId)
                                                       .Select(c => new { totalPriceItem = c.DisplayPrice * c.Quantity }).Sum(c => c.totalPriceItem);

                                cart.DisplayPrice = totalPriceRemain;
                                cart.TotalPrice   = _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId)
                                                    .Sum(c => c.Price);
                                cart.TotalQuantity = quantityRemain;
                                cart.ShippingFee   = totalPriceRemain > 100 ? 0 : 25;

                                _context.CustomerCart.Update(cart);

                                if (await _context.SaveChangesAsync() > 0)
                                {
                                    return(Json(new
                                    {
                                        Status = "Success",
                                        ProductName = product.Name,
                                        Product = _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId).Select(x =>
                                                                                                                                new
                                        {
                                            x.ProductId,
                                            x.DisplayPrice,
                                            x.Product.ProductThumbImage,
                                            x.Quantity,
                                            x.Product.Name,
                                            x.Product.Slug
                                        }).ToList()
                                    }));
                                }

                                return(Json(new
                                {
                                    Status = "Failed",
                                    Message = "Error while updating cart"
                                }));
                            }

                            return(Json(new
                            {
                                Status = "Failed",
                                Message = "Cannot add product into cart"
                            }));
                        }

                        return(Json(new
                        {
                            Status = "Failed",
                            Message = "Product Quantiy is over current quantity in store"
                        }));
                    }

                    return(Json(new
                    {
                        Status = "Failed",
                        Message = "Product Quantiy is over maximum quantity can order in a time"
                    }));
                }

                _context.CustomerCartDetail.Add(new CustomerCartDetail
                {
                    CustomerCartId = customerId,
                    ProductId      = productId,
                    Quantity       = quantity,
                    DisplayPrice   = product.DisplayPrice,
                    Price          = product.RealPrice,
                });

                if (await _context.SaveChangesAsync() > 0)
                {
                    var cart = _context.CustomerCart.Find(customerId);

                    var quantityRemain = _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId)
                                         .Sum(c => c.Quantity);
                    var totalPriceRemain = _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId)
                                           .Select(c => new { totalPriceItem = c.DisplayPrice * c.Quantity }).Sum(c => c.totalPriceItem);

                    cart.DisplayPrice = totalPriceRemain;
                    cart.TotalPrice   = _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId)
                                        .Sum(c => c.Price);
                    cart.TotalQuantity = quantityRemain;
                    cart.ShippingFee   = totalPriceRemain > 100 ? 0 : 25;

                    _context.CustomerCart.Update(cart);

                    if (await _context.SaveChangesAsync() > 0)
                    {
                        return(Json(new
                        {
                            Status = "Success",
                            ProductName = product.Name,
                            Product = _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId).Select(x =>
                                                                                                                    new
                            {
                                x.ProductId,
                                x.DisplayPrice,
                                x.Product.ProductThumbImage,
                                x.Quantity,
                                x.Product.Name,
                                x.Product.Slug
                            }).ToList()
                        }));
                    }

                    return(Json(new
                    {
                        Status = "Failed",
                        Message = "Error while updating cart"
                    }));
                }

                return(Json(new
                {
                    Status = "Failed",
                    Message = "Cannot add product into cart"
                }));
            }

            if (string.IsNullOrEmpty(customerId))
            {
                customerId = Guid.NewGuid().ToString();

//                Set("AnonymousId", customerId);

                _context.AnonymousCustomer.Add(
                    new AnonymousCustomer {
                    AnonymousCustomerId = customerId, VisitDate = DateTime.Now
                });

                await _context.SaveChangesAsync();

                _context.AnonymousCustomerCart.Add(
                    new AnonymousCustomerCart {
                    AnonymousCustomerCartId = customerId
                });

                await _context.SaveChangesAsync();

                _context.AnonymousCustomerCartDetail.Add(new AnonymousCustomerCartDetail
                {
                    AnonymousCustomerCartId = customerId,
                    ProductId    = productId,
                    Quantity     = quantity,
                    DisplayPrice = product.DisplayPrice,
                    Price        = product.RealPrice,
                });

                if (await _context.SaveChangesAsync() > 0)
                {
                    var cart = _context.AnonymousCustomerCart.Find(customerId);

                    var quantityRemain = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                         .Sum(c => c.Quantity);
                    var totalPriceRemain = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                           .Select(c => new { totalPriceItem = c.DisplayPrice * c.Quantity }).Sum(c => c.totalPriceItem);

                    cart.DisplayPrice = totalPriceRemain;
                    cart.TotalPrice   = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                        .Sum(c => c.Price);
                    cart.TotalQuantity = quantityRemain;
                    cart.ShippingFee   = totalPriceRemain > 100 ? 0 : 25;

                    _context.AnonymousCustomerCart.Update(cart);

                    if (await _context.SaveChangesAsync() > 0)
                    {
                        return(Json(new
                        {
                            Status = "Success",
                            ProductName = product.Name,
                            AnonymousId = customerId,
                            Product = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId).Select(x =>
                                                                                                                                      new
                            {
                                x.ProductId,
                                x.DisplayPrice,
                                x.Product.ProductThumbImage,
                                x.Quantity,
                                x.Product.Name,
                                x.Product.Slug
                            }).ToList()
                        }));
                    }

                    return(Json(new
                    {
                        Status = "Failed",
                        Message = "Error while updating cart"
                    }));
                }

                return(Json(new
                {
                    Status = "Failed",
                    Message = "Cannot add product into cart"
                }));
            }

            var anonymousCart = _context.AnonymousCustomerCart.Find(customerId);

            if (anonymousCart != null)
            {
                var cartDetail = _context.AnonymousCustomerCartDetail.Find(customerId, productId);

                if (cartDetail != null)
                {
                    if (product.MaximumQuantity >= cartDetail.Quantity + quantity)
                    {
                        if (product.CurrentQuantity >= cartDetail.Quantity + quantity)
                        {
                            cartDetail.Quantity += quantity;

                            if (await _context.SaveChangesAsync() > 0)
                            {
                                var quantityRemain = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                                     .Sum(c => c.Quantity);
                                var totalPriceRemain = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                                       .Select(c => new { totalPriceItem = c.DisplayPrice * c.Quantity }).Sum(c => c.totalPriceItem);

                                anonymousCart.DisplayPrice = totalPriceRemain;
                                anonymousCart.TotalPrice   = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                                             .Sum(c => c.Price);
                                anonymousCart.TotalQuantity = quantityRemain;
                                anonymousCart.ShippingFee   = totalPriceRemain > 100 ? 0 : 25;

                                _context.AnonymousCustomerCart.Update(anonymousCart);

                                if (await _context.SaveChangesAsync() > 0)
                                {
                                    return(Json(new
                                    {
                                        Status = "Success",
                                        ProductName = product.Name,
                                        Product = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId).Select(x =>
                                                                                                                                                  new
                                        {
                                            x.ProductId,
                                            x.DisplayPrice,
                                            x.Product.ProductThumbImage,
                                            x.Quantity,
                                            x.Product.Name,
                                            x.Product.Slug
                                        }).ToList()
                                    }));
                                }

                                return(Json(new
                                {
                                    Status = "Failed",
                                    Message = "Error while updating cart"
                                }));
                            }

                            return(Json(new
                            {
                                Status = "Failed",
                                Message = "Cannot add product into cart"
                            }));
                        }

                        return(Json(new
                        {
                            Status = "Failed",
                            Message = "Product Quantiy is over current quantity in store"
                        }));
                    }

                    return(Json(new
                    {
                        Status = "Failed",
                        Message = "Product Quantiy is over maximum quantity can order in a time"
                    }));
                }

                _context.AnonymousCustomerCartDetail.Add(new AnonymousCustomerCartDetail
                {
                    AnonymousCustomerCartId = customerId,
                    ProductId    = productId,
                    Quantity     = quantity,
                    DisplayPrice = product.DisplayPrice,
                    Price        = product.RealPrice,
                });

                if (await _context.SaveChangesAsync() > 0)
                {
                    var quantityRemain = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                         .Sum(c => c.Quantity);
                    var totalPriceRemain = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                           .Select(c => new { totalPriceItem = c.DisplayPrice * c.Quantity }).Sum(c => c.totalPriceItem);

                    anonymousCart.DisplayPrice = totalPriceRemain;
                    anonymousCart.TotalPrice   = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                                 .Sum(c => c.Price);
                    anonymousCart.TotalQuantity = quantityRemain;
                    anonymousCart.ShippingFee   = totalPriceRemain > 100 ? 0 : 25;

                    _context.AnonymousCustomerCart.Update(anonymousCart);

                    if (await _context.SaveChangesAsync() > 0)
                    {
                        return(Json(new
                        {
                            Status = "Success",
                            ProductName = product.Name,
                            Product = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId).Select(x =>
                                                                                                                                      new
                            {
                                x.ProductId,
                                x.DisplayPrice,
                                x.Product.ProductThumbImage,
                                x.Quantity,
                                x.Product.Name,
                                x.Product.Slug
                            }).ToList()
                        }));
                    }

                    return(Json(new
                    {
                        Status = "Failed",
                        Message = "Error while updating cart"
                    }));
                }

                return(Json(new
                {
                    Status = "Failed",
                    Message = "Cannot add product into cart"
                }));
            }

            customerId = Guid.NewGuid().ToString();
//            Set("AnonymousId", customerId);

            _context.AnonymousCustomer.Add(
                new AnonymousCustomer {
                AnonymousCustomerId = customerId, VisitDate = DateTime.Now
            });

            await _context.SaveChangesAsync();

            _context.AnonymousCustomerCart.Add(
                new AnonymousCustomerCart {
                AnonymousCustomerCartId = customerId
            });

            await _context.SaveChangesAsync();

            _context.AnonymousCustomerCartDetail.Add(new AnonymousCustomerCartDetail
            {
                AnonymousCustomerCartId = customerId,
                ProductId    = productId,
                Quantity     = quantity,
                DisplayPrice = product.DisplayPrice,
                Price        = product.RealPrice,
            });

            if (await _context.SaveChangesAsync() > 0)
            {
                var cart = _context.AnonymousCustomerCart.Find(customerId);

                var quantityRemain = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                     .Sum(c => c.Quantity);
                var totalPriceRemain = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                       .Select(c => new { totalPriceItem = c.DisplayPrice * c.Quantity }).Sum(c => c.totalPriceItem);

                cart.DisplayPrice = totalPriceRemain;
                cart.TotalPrice   = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId)
                                    .Sum(c => c.Price);
                cart.TotalQuantity = quantityRemain;
                cart.ShippingFee   = totalPriceRemain > 100 ? 0 : 25;

                _context.AnonymousCustomerCart.Update(cart);

                if (await _context.SaveChangesAsync() > 0)
                {
                    return(Json(new
                    {
                        Status = "Success",
                        ProductName = product.Name,
                        AnonymousId = customerId,
                        Product = _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId).Select(x =>
                                                                                                                                  new
                        {
                            x.ProductId,
                            x.DisplayPrice,
                            x.Product.ProductThumbImage,
                            x.Quantity,
                            x.Product.Name,
                            x.Product.Slug
                        }).ToList()
                    }));
                }

                return(Json(new
                {
                    Status = "Failed",
                    Message = "Error while updating cart"
                }));
            }

            return(Json(new
            {
                Status = "Failed",
                Message = "Cannot add product into cart"
            }));
        }
Example #9
0
        public async Task <IActionResult> AddPromotionToCart([FromQuery] string code, [FromQuery] string customerId)
        {
            var promotion = _context.Promotion.FirstOrDefault(p => p.PromotionCode == code.Trim());

            if (promotion != null)
            {
                var currentTime = DateTime.Now;

                if (promotion.TargetApply.ToLower() == "all")
                {
                    if (currentTime > promotion.StartDate && currentTime < promotion.EndDate)
                    {
                        if (User.Identity.IsAuthenticated)
                        {
                            if (string.IsNullOrEmpty(customerId))
                            {
                                customerId = User.Identities.FirstOrDefault(u => u.IsAuthenticated)
                                             ?.FindFirst(
                                    c => c.Type == JwtRegisteredClaimNames.NameId ||
                                    c.Type == ClaimTypes.NameIdentifier)
                                             ?.Value;
                            }

                            var customerCart = _context.CustomerCart.Find(customerId);

                            if (customerCart != null)
                            {
                                customerCart.PromotionId   = promotion.PromotionId;
                                customerCart.PriceDiscount = customerCart.DisplayPrice * (double)promotion.PercentOff;

                                _context.CustomerCart.Update(customerCart);

                                var customerCartDetail =
                                    _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId).ToList();

                                if (customerCartDetail.Any())
                                {
                                    customerCartDetail.ForEach(c =>
                                    {
                                        c.PromotionId   = promotion.PromotionId;
                                        c.PriceDiscount = c.DisplayPrice * (double)promotion.PercentOff;
                                    });
                                }

                                if (await _context.SaveChangesAsync() > 0)
                                {
                                    return(Json(new
                                    {
                                        Status = "Success",
                                        Message = "Promotion is applied",
                                    }));
                                }

                                return(Json(new
                                {
                                    Status = "Failed",
                                    Message = "Cannot apply promotion to your cart",
                                }));
                            }

                            return(Json(new
                            {
                                Status = "Failed",
                                Message = "Cannot find customer cart",
                            }));
                        }

                        if (string.IsNullOrEmpty(customerId))
                        {
                            return(Json(new
                            {
                                Status = "Failed",
                                Message = "Cannot find customer cart",
                            }));
                        }

                        var anonymousCart = _context.AnonymousCustomerCart.Find(customerId);

                        if (anonymousCart != null)
                        {
                            anonymousCart.PromotionId   = promotion.PromotionId;
                            anonymousCart.PriceDiscount = anonymousCart.DisplayPrice * (double)promotion.PercentOff;

                            _context.AnonymousCustomerCart.Update(anonymousCart);

                            var anonymousCartDetail =
                                _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId).ToList();

                            if (anonymousCartDetail.Any())
                            {
                                anonymousCartDetail.ForEach(c =>
                                {
                                    c.PromotionId   = promotion.PromotionId;
                                    c.PriceDiscount = c.DisplayPrice * (double)promotion.PercentOff;
                                });
                            }

                            if (await _context.SaveChangesAsync() > 0)
                            {
                                return(Json(new
                                {
                                    Status = "Success",
                                    Message = "Promotion is applied",
                                }));
                            }

                            return(Json(new
                            {
                                Status = "Failed",
                                Message = "Cannot apply promotion to your cart",
                            }));
                        }

                        return(Json(new
                        {
                            Status = "Failed",
                            Message = "Cannot find customer cart",
                        }));
                    }

                    return(Json(new
                    {
                        Status = "Failed",
                        Message = "Promotion invalid"
                    }));
                }

                if (promotion.TargetApply.ToLower() == "brand")
                {
                    var promoBrand =
                        _context.PromotionBrand.FirstOrDefault(b => b.PromotionId == promotion.PromotionId);
                    if (promoBrand != null)
                    {
                        if (currentTime > promotion.StartDate && currentTime < promotion.EndDate)
                        {
                            if (User.Identity.IsAuthenticated)
                            {
                                if (string.IsNullOrEmpty(customerId))
                                {
                                    customerId = User.Identities.FirstOrDefault(u => u.IsAuthenticated)
                                                 ?.FindFirst(
                                        c => c.Type == JwtRegisteredClaimNames.NameId ||
                                        c.Type == ClaimTypes.NameIdentifier)
                                                 ?.Value;
                                }

                                var customerCartDetail =
                                    _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId && c.Product.BrandId == promoBrand.BrandId).ToList();

                                if (customerCartDetail.Any())
                                {
                                    customerCartDetail.ForEach(c =>
                                    {
                                        c.PromotionId   = promotion.PromotionId;
                                        c.PriceDiscount = c.DisplayPrice * (double)promotion.PercentOff;
                                    });

                                    if (await _context.SaveChangesAsync() > 0)
                                    {
                                        var customerCart = _context.CustomerCart.Find(customerId);
                                        customerCart.PromotionId   = promotion.PromotionId;
                                        customerCart.PriceDiscount = customerCartDetail.Sum(c => c.PriceDiscount);

                                        _context.CustomerCart.Update(customerCart);

                                        if (await _context.SaveChangesAsync() > 0)
                                        {
                                            return(Json(new
                                            {
                                                Status = "Success",
                                                Message = "Promotion is applied",
                                            }));
                                        }
                                    }

                                    return(Json(new
                                    {
                                        Status = "Failed",
                                        Message = "Cannot apply promotion to your cart",
                                    }));
                                }

                                return(Json(new
                                {
                                    Status = "Failed",
                                    Message = "Your cart don't have valid product for promotion",
                                }));
                            }

                            if (string.IsNullOrEmpty(customerId))
                            {
                                return(Json(new
                                {
                                    Status = "Failed",
                                    Message = "Cannot find customer cart",
                                }));
                            }

                            var anonymouseCartDetail =
                                _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId && c.Product.BrandId == promoBrand.BrandId).ToList();

                            if (anonymouseCartDetail.Any())
                            {
                                anonymouseCartDetail.ForEach(c =>
                                {
                                    c.PromotionId   = promotion.PromotionId;
                                    c.PriceDiscount = c.DisplayPrice * (double)promotion.PercentOff;
                                });

                                if (await _context.SaveChangesAsync() > 0)
                                {
                                    var anonymousCart = _context.AnonymousCustomerCart.Find(customerId);
                                    anonymousCart.PromotionId   = promotion.PromotionId;
                                    anonymousCart.PriceDiscount = anonymouseCartDetail.Sum(c => c.PriceDiscount);

                                    _context.AnonymousCustomerCart.Update(anonymousCart);

                                    if (await _context.SaveChangesAsync() > 0)
                                    {
                                        return(Json(new
                                        {
                                            Status = "Success",
                                            Message = "Promotion is applied",
                                        }));
                                    }
                                }

                                return(Json(new
                                {
                                    Status = "Failed",
                                    Message = "Cannot apply promotion to your cart",
                                }));
                            }

                            return(Json(new
                            {
                                Status = "Failed",
                                Message = "Your cart don't have valid product for promotion",
                            }));
                        }

                        return(Json(new
                        {
                            Status = "Failed",
                            Message = "Promotion invalid",
                        }));
                    }
                }

                if (promotion.TargetApply.ToLower() == "category")
                {
                    var promoCategory =
                        _context.PromotionCategory.FirstOrDefault(b => b.PromotionId == promotion.PromotionId);

                    if (promoCategory != null)
                    {
                        if (currentTime > promotion.StartDate && currentTime < promotion.EndDate)
                        {
                            if (User.Identity.IsAuthenticated)
                            {
                                if (string.IsNullOrEmpty(customerId))
                                {
                                    customerId = User.Identities.FirstOrDefault(u => u.IsAuthenticated)
                                                 ?.FindFirst(
                                        c => c.Type == JwtRegisteredClaimNames.NameId ||
                                        c.Type == ClaimTypes.NameIdentifier)
                                                 ?.Value;
                                }

                                var customerCartDetail =
                                    _context.CustomerCartDetail.Where(c => c.CustomerCartId == customerId && c.Product.CategoryId == promoCategory.CategoryId).ToList();

                                if (customerCartDetail.Any())
                                {
                                    customerCartDetail.ForEach(c =>
                                    {
                                        c.PromotionId   = promotion.PromotionId;
                                        c.PriceDiscount = c.DisplayPrice * (double)promotion.PercentOff;
                                    });

                                    if (await _context.SaveChangesAsync() > 0)
                                    {
                                        var customerCart = _context.CustomerCart.Find(customerId);
                                        customerCart.PromotionId   = promotion.PromotionId;
                                        customerCart.PriceDiscount = customerCartDetail.Sum(c => c.PriceDiscount);

                                        _context.CustomerCart.Update(customerCart);

                                        if (await _context.SaveChangesAsync() > 0)
                                        {
                                            return(Json(new
                                            {
                                                Status = "Success",
                                                Message = "Promotion is applied",
                                            }));
                                        }
                                    }

                                    return(Json(new
                                    {
                                        Status = "Failed",
                                        Message = "Cannot apply promotion to your cart",
                                    }));
                                }

                                return(Json(new
                                {
                                    Status = "Failed",
                                    Message = "Your cart don't have valid product for promotion",
                                }));
                            }

                            if (string.IsNullOrEmpty(customerId))
                            {
                                return(Json(new
                                {
                                    Status = "Failed",
                                    Message = "Cannot find customer cart",
                                }));
                            }

                            var anonymouseCartDetail =
                                _context.AnonymousCustomerCartDetail.Where(c => c.AnonymousCustomerCartId == customerId && c.Product.CategoryId == promoCategory.CategoryId).ToList();

                            if (anonymouseCartDetail.Any())
                            {
                                anonymouseCartDetail.ForEach(c =>
                                {
                                    c.PromotionId   = promotion.PromotionId;
                                    c.PriceDiscount = c.DisplayPrice * (double)promotion.PercentOff;
                                });

                                if (await _context.SaveChangesAsync() > 0)
                                {
                                    var anonymousCart = _context.AnonymousCustomerCart.Find(customerId);
                                    anonymousCart.PromotionId   = promotion.PromotionId;
                                    anonymousCart.PriceDiscount = anonymouseCartDetail.Sum(c => c.PriceDiscount);

                                    _context.AnonymousCustomerCart.Update(anonymousCart);

                                    if (await _context.SaveChangesAsync() > 0)
                                    {
                                        return(Json(new
                                        {
                                            Status = "Success",
                                            Message = "Promotion is applied",
                                        }));
                                    }
                                }

                                return(Json(new
                                {
                                    Status = "Failed",
                                    Message = "Cannot apply promotion to your cart",
                                }));
                            }

                            return(Json(new
                            {
                                Status = "Failed",
                                Message = "Your cart don't have valid product for promotion",
                            }));
                        }

                        return(Json(new
                        {
                            Status = "Failed",
                            Message = "Promotion invalid",
                        }));
                    }
                }
            }

            return(Json(new
            {
                Status = "Failed",
                Message = "Promotion invalid"
            }));
        }
Example #10
0
        public async Task <IActionResult> EmailNotificaion([FromQuery] string email, [FromQuery] int id)
        {
            if (string.IsNullOrEmpty(email))
            {
                return(Json(new
                {
                    Status = "Failed",
                    Message = "Invalid email"
                }));
            }

            var product = _context.Product.Find(id);

            if (product != null)
            {
                var productStatus = _context.Product.Where(p => p.ProductId == product.ProductId)
                                    .Select(p => p.ProductStatus.StatusCode).AsNoTracking().ToList();
                if (productStatus[0] == "SoldOut" || productStatus[0] == "StopSelling")
                {
                    var notify = _context.ProductNotification.Find(id, email);
                    if (notify == null)
                    {
                        _context.ProductNotification.Add(new ProductNotification
                        {
                            ProductId = id,
                            Email     = email.Trim()
                        });

                        if (await _context.SaveChangesAsync() > 0)
                        {
                            return(Json(new
                            {
                                Status = "Success",
                                Message = "Your email has been added to system"
                            }));
                        }

                        return(Json(new
                        {
                            Status = "Failed",
                            Message = "Cannot add your email to the system"
                        }));
                    }

                    return(Json(new
                    {
                        Status = "Success",
                        Message = "Your email already in the system, you will get notity as soon as product available"
                    }));
                }

                return(Json(new
                {
                    Status = "Failed",
                    Message = "Your email has been added to system"
                }));
            }

            return(Json(new
            {
                Status = "Failed",
                Message = "The product id is invalid"
            }));
        }
Example #11
0
        public async Task <IdentityResult> AddStaff(StaffInfoDTO staff, string adminId)
        {
            if (await FindUserByUserName(staff.Email) == null)
            {
                var newStaff = new ApplicationUser
                {
                    UserName = staff.Email,
                    Email    = staff.Email,
                    FullName = staff.FullName
                };

                var result = await _userManager.CreateAsync(newStaff, staff.Password);

                if (result.Succeeded)
                {
                    var newUser = await FindUserByUserName(staff.Email);

                    var currentAdmin = adminId;

                    await _context.Staff.AddAsync(new Staff
                    {
                        StaffId = newUser.Id,
                        AddBy   = currentAdmin,
                        AddDate = staff.AddDate,
                        Address = staff.Address,
                        Phone   = staff.Phone,
                        Salary  = staff.Salary
                    });

                    await _context.SaveChangesAsync();

                    return(await _userManager.AddToRoleAsync(newStaff, staff.Role));
                }
            }

            return(IdentityResult.Failed());
        }
Example #12
0
        public async Task <IActionResult> UpdateAddressDefault([FromQuery] int infoId)
        {
            if (User.Identity.IsAuthenticated)
            {
                var info = _context.ShippingInfo.Find(infoId);

                if (info != null)
                {
                    var currentCustomerId = User.Identities.FirstOrDefault(u => u.IsAuthenticated)
                                            ?.FindFirst(
                        c => c.Type == JwtRegisteredClaimNames.NameId || c.Type == ClaimTypes.NameIdentifier)
                                            ?.Value;

                    if (currentCustomerId == info.CustomerId)
                    {
                        if (!info.IsDefault)
                        {
                            var shippingInfos = _context.ShippingInfo.Where(c => c.CustomerId == currentCustomerId).ToList();

                            shippingInfos.ForEach(c => c.IsDefault = false);
                            info.IsDefault = true;

                            if (await _context.SaveChangesAsync() > 0)
                            {
                                return(Json(new
                                {
                                    Status = "Success",
                                    Message = "The address info is set to default"
                                }));
                            }

                            return(Json(new
                            {
                                Status = "Failed",
                                Message = "Cannot update this address info"
                            }));
                        }

                        return(Json(new
                        {
                            Status = "Failed",
                            Message = "This address info is default already"
                        }));
                    }

                    return(Json(new
                    {
                        Status = "Failed",
                        Message = "The current user doesn't own this address info"
                    }));
                }

                return(Json(new
                {
                    Status = "Failed",
                    Message = "Shipping address not found"
                }));
            }

            return(Json(new
            {
                Status = "Failed",
                Message = "Unauthorize user"
            }));
        }