// POST api/values
        public object Post()
        {
            JsonResultViewModel result = new JsonResultViewModel();

            result.status      = true;
            result.message     = "Comment successfully updated";
            result.messageCode = "S";

            var    httpRequest   = HttpContext.Current.Request;
            long   id            = string.IsNullOrEmpty(httpRequest["Id"]) ? 0 : long.Parse(httpRequest["Id"]);
            long   articleId     = string.IsNullOrEmpty(httpRequest["ArticleId"]) ? 0 : long.Parse(httpRequest["ArticleId"]);
            string postedComment = httpRequest["Comment"];
            string userId        = httpRequest["userId"];


            try
            {
                using (PatuhEntities db = new PatuhEntities())
                {
                    TrComment comment = db.TrComments.Where(x => x.Id == id).FirstOrDefault();

                    if (comment == null)
                    {
                        comment          = new TrComment();
                        comment.cCreated = userId;
                        comment.dCreated = DateTime.Now;
                        db.TrComments.AddObject(comment);

                        MsPoint msPoint = db.MsPoints.Where(x => x.ActionCode == "COMMENTART").FirstOrDefault();

                        if (msPoint != null)
                        {
                            TrArticle trArticle = db.TrArticles.Where(x => x.Id == articleId).FirstOrDefault();

                            if (trArticle != null)
                            {
                                TrPoint trPoint = new TrPoint();

                                trPoint.ArticleId    = trArticle.Id;
                                trPoint.UserID       = trArticle.cCreated;
                                trPoint.ActionCode   = "COMMENTART";
                                trPoint.PointValue   = msPoint.RewardPoint;
                                trPoint.cCreated     = userId;
                                trPoint.dCreated     = DateTime.Now;
                                trPoint.cLastUpdated = userId;
                                trPoint.dLastUpdated = DateTime.Now;

                                db.TrPoints.AddObject(trPoint);
                                //db.SaveChanges();
                            }
                        }
                    }



                    comment.ArticleId = articleId;
                    comment.Comment   = postedComment;

                    comment.cLastUpdated = userId;
                    comment.dLastUpdated = DateTime.Now;

                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                result.status      = false;
                result.message     = e.Message;
                result.messageCode = "Error in saving Comment";
            }

            return(result);
        }
        // POST api/values
        public object Post()
        {
            JsonResultViewModel result = new JsonResultViewModel();

            result.status      = true;
            result.message     = "Coupon successfully claimed";
            result.messageCode = "S";

            var    httpRequest = HttpContext.Current.Request;
            long   id          = string.IsNullOrEmpty(httpRequest["msCouponId"]) ? 0 : long.Parse(httpRequest["msCouponId"]);
            string userId      = httpRequest["UserId"];


            try
            {
                using (PatuhEntities db = new PatuhEntities())
                {
                    MsCoupon msCoupon = db.MsCoupons.Where(x => x.Id == id).FirstOrDefault();

                    long totalPoint = db.TrPoints.Where(x => x.UserID == userId).Sum(x => x.PointValue) ?? 0;

                    if ((totalPoint - msCoupon.PointNeeded) < 0)
                    {
                        result.status      = false;
                        result.message     = "User Points is not enough to claim the coupon";
                        result.messageCode = "Error in claiming Coupon";
                        return(result);
                    }

                    TrUserCoupon userCoupon = db.TrUserCoupons.Where(x => x.MsCouponId == id).FirstOrDefault();

                    if (userCoupon == null)
                    {
                        CouponCode.CouponCodeBuilder couponBuilder = new CouponCode.CouponCodeBuilder();
                        CouponCode.Options           opt           = new CouponCode.Options();
                        string couponCode = couponBuilder.Generate(opt);

                        userCoupon            = new TrUserCoupon();
                        userCoupon.MsCouponId = id;
                        userCoupon.UserID     = userId;
                        userCoupon.CouponCode = couponCode;
                        userCoupon.cCreated   = userId;
                        userCoupon.dCreated   = DateTime.Now;
                        db.TrUserCoupons.AddObject(userCoupon);

                        TrPoint trPoint = new TrPoint();
                        trPoint.ArticleId    = 0;
                        trPoint.UserID       = userId;
                        trPoint.ActionCode   = "CLAIM";
                        trPoint.PointValue   = (msCoupon.PointNeeded * -1);
                        trPoint.cCreated     = userId;
                        trPoint.dCreated     = DateTime.Now;
                        trPoint.cLastUpdated = userId;
                        trPoint.dLastUpdated = DateTime.Now;

                        db.TrPoints.AddObject(trPoint);
                    }

                    userCoupon.cStatus = "Y";

                    /*
                     * if (httpRequest.Files.Count > 0)
                     *  {
                     *
                     *      foreach (string file in httpRequest.Files)
                     *      {
                     *          var postedFile = httpRequest.Files[file];
                     *          Type fileType = postedFile.GetType();
                     *          byte[] couponImage = new byte[postedFile.ContentLength];
                     *
                     *          postedFile.InputStream.Read(couponImage, 0, postedFile.ContentLength);
                     *          userCoupon.c
                     *      }
                     */
                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                result.status      = false;
                result.message     = e.Message;
                result.messageCode = "Error in claiming Coupon";
            }

            return(result);
        }
        // POST api/values
        public object Post()
        {
            JsonResultViewModel result = new JsonResultViewModel();

            result.status      = true;
            result.message     = "Article successfully updated";
            result.messageCode = "S";

            var    httpRequest = HttpContext.Current.Request;
            long   id          = string.IsNullOrEmpty(httpRequest["Id"]) ? 0 : long.Parse(httpRequest["Id"]);
            string title       = httpRequest["title"];
            string story       = httpRequest["story"];
            string location    = httpRequest["location"];
            double latitude    = string.IsNullOrEmpty(httpRequest["latitude"]) ? 0 : double.Parse(httpRequest["latitude"]);
            double longitude   = string.IsNullOrEmpty(httpRequest["longitude"]) ? 0 : double.Parse(httpRequest["longitude"]);
            string userId      = httpRequest["userId"];


            try
            {
                using (PatuhEntities db = new PatuhEntities())
                {
                    TrArticle article = db.TrArticles.Where(x => x.Id == id).FirstOrDefault();

                    if (article == null)
                    {
                        article          = new TrArticle();
                        article.cCreated = userId;
                        article.dCreated = DateTime.Now;
                        db.TrArticles.AddObject(article);
                    }


                    article.Category    = "ARTICLE";
                    article.Title       = title;
                    article.Story       = story;
                    article.GPSLocation = location;
                    article.GPSLong     = longitude;
                    article.GPSLat      = latitude;

                    article.cLastUpdated = userId;
                    article.dLastUpdated = DateTime.Now;

                    db.SaveChanges();

                    try
                    {
                        if (httpRequest.Files.Count > 0)
                        {
                            IList <TrImageAttachment> currentImages = db.TrImageAttachments.Where(x => x.HeaderId == article.Id).ToList();
                            if (currentImages != null && currentImages.Count > 0)
                            {
                                foreach (TrImageAttachment dbImg in currentImages)
                                {
                                    db.TrImageAttachments.DeleteObject(dbImg);
                                }
                            }

                            string extention = "";
                            string guid      = "";

                            string[] supportedTypes = new string[] { "jpg", "jpeg", "bmp", "png" };
                            int      fileSequence   = 0;

                            foreach (string file in httpRequest.Files)
                            {
                                var  postedFile = httpRequest.Files[file];
                                Type fileType   = postedFile.GetType();
                                if (postedFile != null)
                                {
                                    if (postedFile.FileName != "")
                                    {
                                        byte[] theImage = new byte[postedFile.ContentLength];
                                        extention = (Path.GetExtension(postedFile.FileName).TrimStart('.')).ToLower();

                                        if (supportedTypes.Contains(extention))
                                        {
                                            postedFile.InputStream.Read(theImage, 0, postedFile.ContentLength);


                                            TrImageAttachment imageAtt = new TrImageAttachment();// db.TrImageAttachments.Where(x => x.Id == id).FirstOrDefault();


                                            imageAtt          = new TrImageAttachment();
                                            imageAtt.HeaderId = article.Id;

                                            imageAtt.cCreated = userId;
                                            imageAtt.dCreated = DateTime.Now;

                                            imageAtt.Sequence     = ++fileSequence;
                                            imageAtt.Image        = theImage;
                                            imageAtt.cLastUpdated = userId;
                                            imageAtt.dLastUpdated = DateTime.Now;

                                            db.TrImageAttachments.AddObject(imageAtt);
                                            db.SaveChanges();
                                        }
                                    }
                                }
                            }
                        }//

                        // incerement points earned by user's creating the article

                        if (id == 0)
                        {
                            MsPoint msPoint = db.MsPoints.Where(x => x.ActionCode == "CREATEART").FirstOrDefault();

                            if (msPoint != null)
                            {
                                TrPoint trPoint = new TrPoint();

                                trPoint.ArticleId    = article.Id;
                                trPoint.UserID       = article.cCreated;
                                trPoint.ActionCode   = "CREATEART";
                                trPoint.PointValue   = msPoint.RewardPoint;
                                trPoint.cCreated     = userId;
                                trPoint.dCreated     = DateTime.Now;
                                trPoint.cLastUpdated = userId;
                                trPoint.dLastUpdated = DateTime.Now;

                                db.TrPoints.AddObject(trPoint);
                                db.SaveChanges();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        result.status      = false;
                        result.message     = e.Message;
                        result.messageCode = "Error in saving article";
                        return(result);
                    }
                }
            }
            catch (Exception e)
            {
                result.status      = false;
                result.message     = e.Message;
                result.messageCode = "Error in saving Article";
            }

            return(result);
        }