public IHttpActionResult AddShareathon(ShareathonViewModel shareathon)
        {
            Shareathon _shreathon = new Shareathon();
            string id = "";
            for (int i = 0; i < shareathon.FacebookPageId.Length; i++)
            {
                string dataid = shareathon.FacebookPageId[i];
                id = dataid + "," + id;
            }
            _shreathon.Facebookpageid = id.TrimEnd(',');
            _shreathon.Facebookaccountid = shareathon.Facebookaccountid;
            _shreathon.Timeintervalminutes = shareathon.Timeintervalminutes;
            _shreathon.Id = Guid.NewGuid();
            _shreathon.Userid = shareathon.Userid;
            if (!sharepo.IsShareathonExist(shareathon.Userid, shareathon.Facebookaccountid, _shreathon.Facebookpageid))
            {
                if (sharepo.AddShareathon(_shreathon))
                {
                    return Ok();
                }
                else
                {
                    return BadRequest();
                }
            }
            else
            {
                return BadRequest("Shareathon exist");
            }


        }
        public bool AddShareathon(Shareathon shareathon)
        {
            bool IsSuccess = false;
            //Creates a database connection and opens up a session
            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                //After Session creation, start Transaction.
                using (NHibernate.ITransaction transaction = session.BeginTransaction())
                {
                    //Proceed action, to Save data.
                    try
                    {
                        session.Save(shareathon);
                        transaction.Commit();
                        IsSuccess = true;
                        return IsSuccess;
                    }
                    catch (Exception ex)
                    {

                        Console.WriteLine(ex.StackTrace);
                        return IsSuccess;
                    }

                }//End Transaction
            }//End Session
        }
        public bool updateShareathon(Domain.Socioboard.Domain.Shareathon shareathon)
        {
            bool isUpdate = false;

            //Creates a database connection and opens up a session
            using (NHibernate.ISession session = SessionFactory.GetNewSession())
            {
                //After Session creation, start Transaction.
                using (NHibernate.ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        // Proceed action to Update Data.
                        // And Set the reuired paremeters to find the specific values.
                        session.CreateQuery("Update Shareathon set Facebookaccountid=:Facebookaccountid,Facebookpageid=:Facebookpageid,Timeintervalminutes=:Timeintervalminutes where Id = :Id")
                        .SetParameter("Facebookaccountid", shareathon.Facebookaccountid)
                        .SetParameter("Facebookpageid", shareathon.Facebookpageid)
                        .SetParameter("Timeintervalminutes", shareathon.Timeintervalminutes)
                        .SetParameter("Id", shareathon.Id)
                        .ExecuteUpdate();
                        transaction.Commit();
                        isUpdate = true;
                        return(isUpdate);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                        return(isUpdate);
                    }
                } //End Transaction
            }     //End session
        }
 public Domain.Socioboard.Domain.Shareathon getShareathon(Guid Id)
 {
     Domain.Socioboard.Domain.Shareathon shareathon = null;
     //Creates a database connection and opens up a session
     using (NHibernate.ISession session = SessionFactory.GetNewSession())
     {
         //After Session creation, start Transaction.
         using (NHibernate.ITransaction transaction = session.BeginTransaction())
         {
             try
             {
                 NHibernate.IQuery query = session.CreateQuery("from Shareathon where Id = :ID");
                 query.SetParameter("ID", Id);
                 Domain.Socioboard.Domain.Shareathon result = (Domain.Socioboard.Domain.Shareathon)query.UniqueResult();
                 shareathon = result;
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.StackTrace);
             }
         } //End Transaction
     }     //End session
     return(shareathon);
 }
        public void PostDataPage(Shareathon item, string pageid)
        {
            FacebookAccount facebookpage = sharegrprepo.getFbAccount(Guid.Parse(pageid));
            FacebookAccount facebookAccount = sharegrprepo.getFbAccount(item.Facebookaccountid);

            string feeds = FacebookHelper.getFacebookRecentPost(facebookpage.AccessToken, facebookpage.FbUserId);


            string feedId = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(feeds) && !feeds.Equals("[]"))
                {
                    JObject fbpageNotes = JObject.Parse(feeds);
                    foreach (JObject obj in JArray.Parse(fbpageNotes["data"].ToString()))
                    {
                        try
                        {
                            string feedid = obj["id"].ToString();
                            feedid = feedid.Split('_')[1];
                            FacebookHelper.ShareFeed(facebookAccount.AccessToken, feedid, facebookpage.FbUserId, "", facebookAccount.FbUserId, "", item.Timeintervalminutes);
                        }
                        catch { }
                        Thread.Sleep(1000 * 60 * item.Timeintervalminutes);
                    }
                }

            }
            catch (Exception ex)
            {


            }
        }
        public void PostDataPage(Shareathon item, string pageid)
        {

            Domain.Socioboard.Domain.FacebookAccount facebookpage = sharepo.getFacebookAccountDetailsByUserProfileId(pageid, item.Userid);
                Domain.Socioboard.Domain.FacebookAccount facebookAccount = sharepo.getFacebookAccountDetailsByUserProfileId(item.Facebookaccountid,item.Userid);

            if (facebookpage != null)
            {
                string feeds = FacebookHelper.getFacebookRecentPost(facebookAccount.AccessToken, facebookpage.FbUserId);



                string feedId = string.Empty;
                try
                {
                    if (!string.IsNullOrEmpty(feeds) && !feeds.Equals("[]"))
                    {
                        JObject fbpageNotes = JObject.Parse(feeds);
                        foreach (JObject obj in JArray.Parse(fbpageNotes["data"].ToString()))
                        {
                            try
                            {
                                string feedid = obj["id"].ToString();
                                feedid = feedid.Split('_')[1];
                                if (sharepo.IsShareathonExistById(item.Id))
                                {
                                    string ret = FacebookHelper.ShareFeed(facebookAccount.AccessToken, feedid, facebookpage.FbUserId, "", facebookAccount.FbUserId, facebookpage.FbUserName);
                                    if (ret == "success")
                                    {

                                        Thread.Sleep(1000 * 60 * item.Timeintervalminutes);
                                    }
                                    else if (ret == "Error validating access token")
                                    {
                                        facebookAccount.IsActive = 2;
                                        facebookrepo.updateFacebookUserStatus(facebookAccount);
                                    }
                                }
                            }
                            catch { }


                        }
                    }
                    else
                    {
                        if (!feeds.Contains("The remote server returned an error: (400) Bad Request."))
                        {
                            facebookpage.IsActive = 2;
                            facebookrepo.updateFacebookUserStatus(facebookpage);
                        }
                        else {

                            facebookpage.IsActive = 2;
                            facebookrepo.updateFacebookUserStatus(facebookAccount);
                            sharepo.UpadteShareathonByFacebookUserId(facebookAccount.FbUserId, facebookAccount.UserId);
                        }
                    }

                }
                catch (Exception ex)
                {


                }
            }

        }