Beispiel #1
0
        public IHttpActionResult Delete(int id)
        {
            OrderCartRepository orderCartrepo = new OrderCartRepository();

            orderCartrepo.Delete(id);
            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult GetCartsByCustomerNOrderID(int cid, int oid)
        {
            var identity  = (ClaimsIdentity)User.Identity;
            int lidFromDB = Convert.ToInt32(identity.Claims.FirstOrDefault(x => x.Type == "ID").Value);

            int cidFromDB = customerDB.GetCustomerByLoginID(lidFromDB).ID;

            if (cidFromDB != cid)
            {
                return(BadRequest());
            }

            OrderCartRepository orderCartDB = new OrderCartRepository();

            var cartFromDB = orderCartDB.GetCartsByCustomerNOrderID(cid, oid);

            if (cartFromDB.Count != 0)
            {
                return(Ok(cartFromDB));
            }
            else
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }
        }
        public IHttpActionResult PutCartByCustomerNOrderID(int cid, int oid, int ocid, OrderCart orderCart)
        {
            OrderCartRepository orderCartDB = new OrderCartRepository();
            ProductRepository   productDB   = new ProductRepository();


            var productPrice = productDB.Get((int)orderCart.ProductID).SellPrice;


            orderCart.CartAmount = orderCart.Quantity * productPrice;

            if (ModelState.IsValid)
            {
                orderCartDB.Update(orderCart);
                //orderCartDB.UpdateCart(orderCart);



                return(Ok(orderCart));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            List <ValidationResult> errors = new List <ValidationResult>();
            OrderCartRepository     db     = new OrderCartRepository();

            if (CartAmount < 0)
            {
                errors.Add(new ValidationResult($"{nameof(CartAmount)} cannot be a negative value.", new List <string> {
                    nameof(CartAmount)
                }));
            }
            if (Quantity < 0)
            {
                errors.Add(new ValidationResult($"{nameof(Quantity)} cannot be a negative value.", new List <string> {
                    nameof(Quantity)
                }));
            }

            //var cartFromDB = db.GetAll().Where(x => x.OrderID == OrderID && x.ProductID == ProductID).ToList();

            //if (cartFromDB.Count != 0)
            //{
            //    errors.Add(new ValidationResult($"Item Already Exists in the Cart."));
            //}



            return(errors);
        }
        public IHttpActionResult GetCartByCustomerNOrderID(int cid, int oid, int ocid)
        {
            OrderCartRepository orderCartDB = new OrderCartRepository();
            ProductRepository   productDB   = new ProductRepository();



            return(Ok(orderCartDB.Get(ocid)));
        }
Beispiel #6
0
        public IHttpActionResult PutnewProduct([FromUri] int id, [FromBody] OrderCart ordercart)
        {
            OrderCartRepository cartrepo = new OrderCartRepository();

            ordercart.ID = id;

            cartrepo.Update(ordercart);

            return(Ok(ordercart));
        }
        public IHttpActionResult GetOrderCart([FromUri] int uid, [FromUri] int oid)
        {
            OrderCartRepository orderCartrepo = new OrderCartRepository();
            var orderFromDB = orderCartrepo.GetCartsByEmployeeNOrderID(uid, oid);

            if (orderFromDB.Count() != 0)
            {
                return(Ok(orderFromDB));
            }
            else
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }
        }
Beispiel #8
0
        public IHttpActionResult Post([FromBody] OrderCart ordercart)
        {
            OrderCartRepository cartrepo = new OrderCartRepository();

            if (ModelState.IsValid)
            {
                cartrepo.Insert(ordercart);
                return(Created("api/sellproducts/" + ordercart.ID, ordercart));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
        public IHttpActionResult GetOrdercart(int oid)
        {
            OrderCartRepository orderCartrepo = new OrderCartRepository();
            var orderCartList = orderCartrepo.GetCartsByOrderID(oid);

            if (orderCartList.Count() != 0)
            {
                return(Ok(orderCartList));
            }
            else
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }
        }
Beispiel #10
0
        public IHttpActionResult GetOrderCartByOrderIdNProductId(int oid, int pid)
        {
            OrderCartRepository cartrepo = new OrderCartRepository();
            var cartFromDB = cartrepo.GetCartsByOrderIDNProductID(oid, pid);

            if (cartFromDB.Count() != 0)
            {
                return(Ok(cartFromDB));
            }
            else
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }
        }
Beispiel #11
0
        public IHttpActionResult Getcart(int id)
        {
            OrderCartRepository cartrepo = new OrderCartRepository();
            var cart = cartrepo.GetCartsByOrderID(id);

            if (cart.Count() != 0)
            {
                return(Ok(cart));
            }
            else
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }
        }
        public IHttpActionResult GetCartsByOrderID(int oid)
        {
            OrderCartRepository orderCartDB = new OrderCartRepository();

            var cartFromDB = orderCartDB.GetCartsByOrderID(oid);

            if (cartFromDB.Count != 0)
            {
                return(Ok(cartFromDB));
            }
            else
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }
        }
        public IHttpActionResult DeleteCartByCustomerNOrderID(int ocid)
        {
            OrderCartRepository orderCartDB = new OrderCartRepository();

            //var orderCartFromDB = orderCartDB.GetAll().Where(x => x.ID == ocid).ToList();

            //if (orderCartFromDB.Count != 0)
            //{
            orderCartDB.Delete(ocid);

            return(StatusCode(HttpStatusCode.NoContent));
            //}
            //    else
            //    {
            //return BadRequest("Cart Not Found");
            //}
        }
        public IHttpActionResult PutRejected([FromUri] int oid, [FromUri] int pid, [FromBody] Product product)
        {
            //OrderRepository orderrepo = new OrderRepository();
            OrderCartRepository orderCartrepo = new OrderCartRepository();
            //ProductRepository prodrepo = new ProductRepository();

            //var orderCartData = orderCartrepo.GetCartsByOrderID(oid);
            //int quantity;

            //foreach (var item in orderCartData)
            //{
            //    var prodFromDB = prodrepo.Get((int)item.ProductID);
            //    product.ID = prodFromDB.ID;
            //    quantity = (prodFromDB.Quantity + item.Quantity);
            //    product.Quantity = quantity;
            //    product.Image = prodFromDB.Image;
            //    product.Name = prodFromDB.Name;
            //    product.BuyPrice = prodFromDB.BuyPrice;
            //    product.SellPrice = prodFromDB.SellPrice;
            //    product.ProductType = prodFromDB.ProductType;
            //    product.ProductStatus = prodFromDB.ProductStatus;
            //    product.ProductTypeID = prodFromDB.ProductTypeID;
            //    product.ProductStatusID = prodFromDB.ProductStatusID;
            //    product.Vendor = prodFromDB.Vendor;
            //    product.VendorID = prodFromDB.VendorID;
            //    product.Login = prodFromDB.Login;
            //    product.ModifiedBy = prodFromDB.ModifiedBy;

            //    prodrepo.Update(product);
            //    orderCartrepo.Delete(item.ID);

            //}



            ProductRepository prodrepo = new ProductRepository();

            product.ID            = pid;
            product.ProductTypeID = product.ProductTypeID;
            prodrepo.Update(product);

            return(Ok(product));
        }
        public IHttpActionResult PutCancel([FromUri] int id, [FromBody] Order order)
        {
            // var pro = orderDB.Get(id);
            order.ID = id;
            //order.SellBy = pro.SellBy;


            OrderCartRepository orderCartDB = new OrderCartRepository();
            ProductRepository   productDB   = new ProductRepository();


            var cartFromDB = orderCartDB.GetAll().Where(x => x.OrderID == id).ToList();

            //var productFromDB = orderCartDB.GetAll();

            foreach (var item in cartFromDB)
            {
                var productToDB = productDB.Get((int)item.ProductID);

                productToDB.Quantity = productToDB.Quantity + item.Quantity;

                if (productToDB.Quantity <= 0)
                {
                    productToDB.ProductStatusID = 2;
                }

                if (productToDB.Quantity > 0)
                {
                    productToDB.ProductStatusID = 1;
                }

                productDB.Update(productToDB);
            }

            orderDB.UpdateOrderStatusCancel(order);

            return(Ok(order));



            //Cancel Order
        }
        public IHttpActionResult PutUpdateAmountOrder(int id, Order order)
        {
            OrderCartRepository orderCartDB = new OrderCartRepository();


            var cartFromDB = orderCartDB.GetAll().Where(x => x.OrderID == id).ToList();

            //var orderFromDB = orderDB.GetAll().Where(x => x.ID == id).FirstOrDefault();

            //order.CustomerName = orderFromDB.CustomerName;

            var total = (float)0;

            if (cartFromDB.Count != 0)
            {
                foreach (var item in cartFromDB)
                {
                    total += (float)item.CartAmount;
                }
                order.TotalAmount = (float)total;
            }
            else
            {
                order.TotalAmount = (float)0;
            }



            if (ModelState.IsValid)
            {
                OrderRepository orderDB2 = new OrderRepository();
                orderDB2.Update(order);
                return(Ok(order));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
        public IHttpActionResult PostCartByCustomerNOrderID(int cid, int oid, OrderCart orderCart)
        {
            OrderCartRepository orderCartDB = new OrderCartRepository();
            ProductRepository   productDB   = new ProductRepository();

            var productFromDB = productDB.Get((int)orderCart.ProductID);


            orderCart.OrderID    = oid;
            orderCart.CartAmount = orderCart.Quantity * productFromDB.SellPrice;



            var cartFromDB = orderCartDB.GetAll().Where(x => x.OrderID == orderCart.OrderID && x.ProductID == orderCart.ProductID).ToList();

            if (cartFromDB.Count != 0)
            {
                return(BadRequest($"Item Already Exists in the Cart."));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    orderCartDB.Insert(orderCart);



                    var uri = Url.Link("GetAllCartsByCustomerNOrderID", null);

                    return(Created(uri, orderCart));
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
        }
        public IHttpActionResult PutCheckoutOrder(int id, Order order)
        {
            OrderCartRepository orderCartDB = new OrderCartRepository();
            ProductRepository   productDB   = new ProductRepository();


            var cartFromDB    = orderCartDB.GetAll().Where(x => x.OrderID == id).ToList();
            var productFromDB = orderCartDB.GetAll();

            //var orderFromDB = orderDB.GetAll().Where(x => x.ID == id).FirstOrDefault();

            //order.CustomerName = orderFromDB.CustomerName;

            var total = (float)0;

            if (cartFromDB.Count != 0)
            {
                foreach (var item in cartFromDB)
                {
                    total += (float)item.CartAmount;
                }
                order.TotalAmount = (float)total;
            }
            else
            {
                order.TotalAmount = (float)0;
            }


            foreach (var item in cartFromDB)
            {
                if (item.Quantity > productDB.Get((int)item.ProductID).Quantity || productDB.Get((int)item.ProductID).ProductStatusID == 2)
                {
                    return(BadRequest($"Some products you ordered which is not available as you desired e.g. \"{item.Product.Name}\"." + "<br>" + " Remove all UNAVAILABLE/NOT FOR SALE products to out."));
                }

                if (productDB.Get((int)item.ProductID).ProductStatusID == 4 || productDB.Get((int)item.ProductID).ProductStatusID == 3)
                {
                    return(BadRequest($"Some products you ordered which is not for sale e.g. \"{item.Product.Name}\"." + "<br>" + " Remove all UNAVAILABLE/NOT FOR SALE products to out."));
                }
            }



            if (ModelState.IsValid)
            {
                foreach (var item in cartFromDB)
                {
                    var productToDB = productDB.Get((int)item.ProductID);

                    productToDB.Quantity = productToDB.Quantity - item.Quantity;

                    if (productToDB.Quantity <= 0)
                    {
                        productToDB.ProductStatusID = 2;
                    }

                    productDB.Update(productToDB);
                }

                OrderRepository orderDB2 = new OrderRepository();
                orderDB2.Update(order);
                return(Ok(order));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Beispiel #19
0
        public async Task CreateOrder_Success()
        {
            // Arrange
            var loggerRepository = Loggers.OrderRepositoryLogger();

            var mapper = Mapper.Get();

            var dbContext = _fixture.Context;

            var category = NewDatas.NewCategory();
            var user     = NewDatas.NewUser();

            await dbContext.Categories.AddAsync(category);

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var product1 = NewDatas.NewProduct();

            product1.CategoryId = category.CategoryId;

            var product2 = NewDatas.NewProduct();

            product2.CategoryId = category.CategoryId;

            await dbContext.Products.AddRangeAsync(product1, product2);

            await dbContext.SaveChangesAsync();

            var contextAccessor = ContextAccesser.mockHttpAccessor(user.Id);

            var orderCartRepository = new OrderCartRepository(loggerRepository, contextAccessor, mapper, dbContext);

            var orderRequest = new List <CartOrderRequest> {
                new CartOrderRequest {
                    ProductId = product1.ProductId,
                    Quantity  = 3,
                },
                new CartOrderRequest {
                    ProductId = product2.ProductId,
                    Quantity  = 2,
                },
            };

            // Act
            var orderController = new OrdersController(orderCartRepository);
            var result          = await orderController.CreateOrder(orderRequest);

            // Assert
            var orderResultType      = Assert.IsType <ActionResult <IEnumerable <CartOrderRespone> > >(result);
            var getOrdersResultValue = Assert.IsType <CreatedResult>(result.Result);

            var ordersValue = getOrdersResultValue.Value as List <CartOrderRespone>;

            Assert.NotEmpty(ordersValue);

            var totalPriceReq = (product1.Price * orderRequest[0].Quantity) + (product2.Price * orderRequest[1].Quantity);

            var totalPriceRes = 0;

            ordersValue.ForEach(order => totalPriceRes += order.Product.Price * order.Quantity);

            Assert.Equal(totalPriceReq, totalPriceRes);
        }