Exemple #1
0
        public IHttpActionResult PutCustomer(int id, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.CustomerId)
            {
                return(BadRequest());
            }

            db.Entry(customer).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PutItem(int id, Item item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != item.ItemId)
            {
                return(BadRequest());
            }

            db.Entry(item).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PostOrder(Order order)
        {
            try
            {
                //Order table
                if (order.OrderId == 0)
                {
                    db.Order.Add(order);
                }
                else
                {
                    //db.Order.Attach(order);
                    db.Entry(order).State = EntityState.Modified;
                }

                //OrderItems table
                foreach (var item in order.OrderItems)
                {
                    if (item.OrderItemId == 0)
                    {
                        db.OrderItems.Add(item);
                    }
                    else
                    {
                        db.Entry(item).State = EntityState.Modified;
                    }
                }

                //Delete for OrderItems
                if (order.DeletedOrderItemIds != null)
                {
                    foreach (var id in order.DeletedOrderItemIds.Split(',').Where(x => x != ""))
                    {
                        OrderItems x = db.OrderItems.Find(Convert.ToInt64(id));
                        db.OrderItems.Remove(x);
                    }
                }

                db.SaveChanges();

                return(Ok());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IHttpActionResult UpdateQty(int ID, int Qty)
        {
            var user_id = User.Identity.GetUserId();
            var DB      = new AngularDemoEntities();

            try
            {
                var Cart_item = DB.Cart_Master.Where(x => x.CartID == ID).FirstOrDefault();
                if (Cart_item != null)
                {
                    var Product = DB.Product_Master.Where(x => x.ProductID == Cart_item.ProductID).FirstOrDefault();
                    if (Qty < Product.ProductQuantity)
                    {
                        if (Qty <= Product.ProductQuantity && Qty >= Cart_item.ProductQuantity)
                        {
                            Product.ProductQuantity   -= Math.Abs(Qty);
                            Cart_item.ProductQuantity += Math.Abs(Qty);
                            DB.SaveChanges();
                            return(Ok("success"));
                        }
                        else
                        {
                            Product.ProductQuantity   -= Qty;
                            Cart_item.ProductQuantity += Qty;
                            DB.SaveChanges();
                            return(Ok("success"));
                        }
                    }
                    else
                    {
                        return(Ok("OutOfStock"));
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
        public IHttpActionResult Checkout(Order_Master O)
        {
            var DB = new AngularDemoEntities();

            try
            {
                var user_id = User.Identity.GetUserId();
                O.OrderDate    = DateTime.Now;
                O.DeliveryDate = DateTime.Now.AddDays(5);
                O.UserID       = user_id;
                DB.Order_Master.Add(O);
                DB.SaveChanges();
                int InsertedOrderID  = O.OrderID;
                List <Cart_Master> C = DB.Cart_Master.Where(x => x.UserID == user_id).ToList();
                if (C.Count > 0)
                {
                    foreach (var i in C)
                    {
                        OrderItems_Master OI = new OrderItems_Master
                        {
                            OrderID   = InsertedOrderID,
                            ProductID = Convert.ToInt32(i.ProductID),
                            Quantity  = Convert.ToDecimal(i.ProductQuantity),
                            UserID    = i.UserID,
                            UnitPrice = Convert.ToDecimal(i.Product_Master.ProductPrice)
                        };
                        DB.OrderItems_Master.Add(OI);
                        DB.Cart_Master.Remove(i);
                        DB.SaveChanges();
                    }
                    return(Ok("success"));
                }
                else
                {
                    return(Ok("Some problem"));
                }
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
 public IHttpActionResult DeleteProduct(int P_ID)
 {
     try
     {
         var DB      = new AngularDemoEntities();
         var Product = DB.Product_Master.Where(x => x.ProductID == P_ID).FirstOrDefault();
         if (Product != null)
         {
             DB.Product_Master.Remove(Product);
             DB.SaveChanges();
             return(Ok("success"));
         }
         else
         {
             return(Ok("Record not found"));
         }
     }
     catch (Exception ex)
     {
         throw ex.InnerException;
     }
 }
        public IHttpActionResult AddProfile(Profile P)
        {
            var DB = new AngularDemoEntities();

            try
            {
                if (P != null)
                {
                    byte[] ImageFile = null;
                    if (!string.IsNullOrEmpty(P.ProfilePicture))
                    {
                        ImageFile = Convert.FromBase64String(P.ProfilePicture.Split(',')[1]);
                    }
                    var user_id           = User.Identity.GetUserId();
                    UserProfile_Master UP = new UserProfile_Master
                    {
                        UserID         = user_id,
                        FirstName      = P.FirstName,
                        City           = P.City,
                        Gender         = P.Gender,
                        LastName       = P.LastName,
                        Mobile         = P.Mobile,
                        ProfilePicture = ImageFile,
                        State          = P.State
                    };
                    DB.UserProfile_Master.Add(UP);
                    DB.SaveChanges();
                    return(Ok("success"));
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
        public IHttpActionResult AddToCart(Cart_Master C)
        {
            var DB = new AngularDemoEntities();

            try
            {
                if (C != null)
                {
                    var  user_id = User.Identity.GetUserId();
                    bool IsThere = DB.Cart_Master.Any(x => x.ProductID == C.ProductID && x.UserID == user_id);
                    if (!IsThere)
                    {
                        C.UserID = User.Identity.GetUserId();
                        DB.Cart_Master.Add(C);
                        var Products = DB.Product_Master.Where(x => x.ProductID == C.ProductID).SingleOrDefault();
                        if (Products != null)
                        {
                            Products.ProductQuantity--;
                        }
                        DB.SaveChanges();
                        return(Ok("success"));
                    }
                    else
                    {
                        return(Ok("already"));
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
        public IHttpActionResult AddProduct(Products P)
        {
            var DB = new AngularDemoEntities();

            try
            {
                if (P != null)
                {
                    byte[] ImageFile = null;
                    if (!string.IsNullOrEmpty(P.ProductImage))
                    {
                        ImageFile = Convert.FromBase64String(P.ProductImage.Split(',')[1]);
                    }
                    Product_Master Obj_P = new Product_Master
                    {
                        CategoryID         = P.CategoryID,
                        InStock            = P.InStock,
                        ProductImage       = ImageFile,
                        ProductName        = P.ProductName,
                        ProductPrice       = P.ProductPrice,
                        ProductQuantity    = P.ProductQuantity,
                        ProductDescription = P.ProductDescription
                    };
                    DB.Product_Master.Add(Obj_P);
                    DB.SaveChanges();
                    return(Ok("success"));
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
        public IHttpActionResult RemoveCartItem(int ID)
        {
            var user_id = User.Identity.GetUserId();
            var DB      = new AngularDemoEntities();

            try
            {
                var Cart_item = DB.Cart_Master.Where(x => x.CartID == ID).FirstOrDefault();
                if (Cart_item != null)
                {
                    DB.Cart_Master.Remove(Cart_item);
                    DB.SaveChanges();
                    return(Ok("success"));
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }