public void AddComment(Models.ComboComment comment)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";

            BLL.ComboComment newComment = new ComboComment();
            newComment.AddNew();

            if (comment.ComboUserID != 0)
                newComment.ComboUserID = comment.ComboUserID;
            else
            {
                _response.ErrorCode = 30;
                _response.ErrorMsg = "Can't insert commnet. No user id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            if (comment.ComboPostID != 0)
                newComment.ComboPostID = comment.ComboPostID;
            else
            {
                _response.ErrorCode = 31;
                _response.ErrorMsg = "Can't insert comment. No post id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            newComment.CommentText = comment.CommentText.Replace("\n", " "); ;
            newComment.CommentDate = DateTime.UtcNow;

            newComment.Save();

            JavaScriptSerializer js = new JavaScriptSerializer();
            Models.Attachment[] att = js.Deserialize<Models.Attachment[]>(js.Serialize(comment.Attachments));
            if (att != null)
            {
                ComboCommentAttachment attachment = new ComboCommentAttachment();
                foreach (Models.Attachment item in att)
                {
                    attachment.AddNew();
                    attachment.AttachmentID = item.AttachmentID;
                    attachment.ComboCommnetID = newComment.ComboCommentID;

                }
                attachment.Save();
            }

            ComboCommentAttachment notificationAtt = new ComboCommentAttachment ();
            notificationAtt.GetCommentAttachmentsByCommentID(newComment.ComboCommentID);

            Models.CommentUserTag[] userTags = js.Deserialize<Models.CommentUserTag[]>(js.Serialize(comment.UserTags));
            if (userTags != null)
            {
                CommentUserTag usertag = new CommentUserTag();
                foreach (Models.CommentUserTag item in userTags)
                {
                    usertag.AddNew();
                    usertag.ComboUserID = item.ComboUserID;
                    usertag.ComboCommentID = newComment.ComboCommentID;
                    usertag.Offset = item.Offset;

                    /**************************/
                    // save notification and push it to device
                    ComboUser commentcreator = new ComboUser();
                    ComboUser tagged = new ComboUser();
                    commentcreator.GetUserByUserId(newComment.ComboUserID);
                    tagged.GetUserByUserId(item.ComboUserID);
                    List<Models.CommentUserTag> postTag = new List<Models.CommentUserTag>();
                    postTag.Add(new Models.CommentUserTag
                    {
                        ComboCommentID = newComment.ComboCommentID,
                        ComboUserID = tagged.ComboUserID,
                        UserName = tagged.UserName,
                        Offset = item.Offset,
                        ComboPostID = newComment.ComboPostID,
                        CreatorUserID = commentcreator.ComboUserID,
                        CreatorUserName= commentcreator.UserName,
                        CreatorProfilePic = commentcreator.GetColumn("ProfilePic").ToString(),
                        CommentText = newComment.CommentText,
                        Attachments = notificationAtt.DefaultView.Table.AsEnumerable().Select(r =>
                        {
                            return new Models.Attachment
                            {
                                AttachmentID = Convert.ToInt32(r["AttachmentID"]),
                                Path = r["Path"].ToString(),
                                AttachmentTypeID = Convert.ToInt32(r["AttachmentTypeID"]),
                                ThumbsPath = r["ThumbsPath"].ToString()
                            };
                        }).ToList(),
                    });

                    ComboNotification notification = new ComboNotification();
                    notification.AddNew();
                    notification.ComboUserID = tagged.ComboUserID;
                    notification.NotificationType = (int)Combo.Models.NotificationType.TAG_USER_IN_POST; // tag user to post
                    notification.NotificationDate = DateTime.UtcNow;
                    notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(postTag);
                    notification.IsRead = false;
                    notification.Save();

                    NotificationUserSettings settings = new NotificationUserSettings();
                    settings.LoadByPrimaryKey(tagged.ComboUserID, (int)Combo.Models.NotificationType.TAG_USER_IN_POST);
                    bool notify = false;
                    if (settings.RowCount == 0)
                        notify = true;
                    else
                        notify = settings.CanGetNotification(tagged.ComboUserID, commentcreator.ComboUserID, (int)Combo.Models.NotificationType.TAG_USER_IN_POST);
                    if (notify)
                    {
                        List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row =>
                        {
                            return new Models.ComboNotification
                            {
                                ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]),
                                ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                                IsRead = Convert.ToBoolean(row["IsRead"]),
                                NotificationBody = row["NotificationBody"].ToString(),
                                NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                                NotificationType = Convert.ToInt32(row["NotificationType"])
                            };
                        }).ToList();

                        SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), tagged.DeviceID);
                    }
                    /**************************/

                }
                usertag.Save();
            }

            Models.CommentHashTag[] hashTags = js.Deserialize<Models.CommentHashTag[]>(js.Serialize(comment.HashTags));
            if (hashTags != null)
            {
                CommentHashTag commentthashtag = new CommentHashTag();

                foreach (Models.CommentHashTag item in hashTags)
                {
                    HashTag currenttag = new HashTag();

                    if (!currenttag.GetHashTagByName(item.TagName))
                    {
                        currenttag.AddNew();
                        currenttag.Name = item.TagName;
                        currenttag.Save();
                    }

                    commentthashtag.AddNew();
                    commentthashtag.HashTagID = currenttag.HashTagID;
                    commentthashtag.ComboCommentID = newComment.ComboCommentID;
                    commentthashtag.Offset = item.Offset;
                }
                commentthashtag.Save();
            }

            /**************************/
            // save notification and push it to device
            ComboPost post = new ComboPost();
            post.LoadByPrimaryKey(comment.ComboPostID);
            ComboUser creator = new ComboUser();
            ComboUser commentor = new ComboUser();
            ComboPostAttachment postatt = new ComboPostAttachment();
            postatt.GetPostAttachmentsByPostID(comment.ComboPostID);
            creator.GetUserByUserId(post.ComboUserID);
            commentor.GetUserByUserId(comment.ComboUserID);
            if (creator.ComboUserID != commentor.ComboUserID)
            {
                List<Models.ComboComment> acomment = newComment.DefaultView.Table.AsEnumerable().Select(row =>
                {
                    return new Models.ComboComment
                    {
                        ComboPostID = Convert.ToInt32(row["ComboPostID"]),
                        ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                        ComboUserName = commentor.UserName,
                        ComboDisplayName = commentor.DisplayName,
                        CommentText = newComment.CommentText,
                        CommentDate = newComment.CommentDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                        ProfilePic = commentor.GetColumn("ProfilePic").ToString(),
                        PostAttachemnts = postatt.DefaultView.Table.AsEnumerable().Select(r =>
                        {
                            return new Models.Attachment
                            {
                                AttachmentID = Convert.ToInt32(r["AttachmentID"]),
                                Path = r["Path"].ToString(),
                                AttachmentTypeID = Convert.ToInt32(r["AttachmentTypeID"]),
                                ThumbsPath = r["ThumbsPath"].ToString()
                            };
                        }).ToList(),
                    };
                }).ToList();

                ComboNotification notification = new ComboNotification();
                notification.AddNew();
                notification.ComboUserID = post.ComboUserID;
                notification.NotificationType = (int)Combo.Models.NotificationType.COMMENT_ON_POST; // add comment to post
                notification.NotificationDate = DateTime.UtcNow;
                notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(acomment);
                notification.IsRead = false;
                notification.Save();

                NotificationUserSettings settings = new NotificationUserSettings();
                settings.LoadByPrimaryKey(creator.ComboUserID, (int)Combo.Models.NotificationType.COMMENT_ON_POST);
                bool notify = false;
                if (settings.RowCount == 0)
                    notify = true;
                else
                    notify = settings.CanGetNotification(creator.ComboUserID, commentor.ComboUserID, (int)Combo.Models.NotificationType.COMMENT_ON_POST);
                if (notify)
                {
                    List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row =>
                    {
                        return new Models.ComboNotification
                        {
                            ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]),
                            ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                            IsRead = Convert.ToBoolean(row["IsRead"]),
                            NotificationBody = row["NotificationBody"].ToString(),
                            NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                            NotificationType = Convert.ToInt32(row["NotificationType"])
                        };
                    }).ToList();

                    SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), creator.DeviceID);
                }
            }
            /**************************/

            comment.ComboCommentID = newComment.ComboCommentID;
            comment.CommentDate = newComment.CommentDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
            _response.Entity = new Models.ComboComment[] { comment };

            SetContentResult(_response);
            return;
        }
        public void ToggleFollowFriend(int userId, int FollowerId)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";
            bool follow = false;

            ProfileFollower follower = new ProfileFollower();
            if (!follower.LoadByPrimaryKey(userId, FollowerId))
            {
                follower.AddNew();
                follower.ComboFollowerID = FollowerId;
                follower.ComboUserID = userId;
                follower.IsRequestApproved = true;
                follower.Save();
                follow = true;
            }
            else
            {
                follower.MarkAsDeleted();
                follower.Save();
            }

            if (follow)
            {
                /**************************/
                // save notification and push it to device
                ComboUser creator = new ComboUser();
                ComboUser commentor = new ComboUser();
                creator.GetUserByUserId(userId);
                commentor.GetUserByUserId(FollowerId);

                List<Models.FollowRequest> arequest = follower.DefaultView.Table.AsEnumerable().Select(row =>
                {
                    return new Models.FollowRequest
                    {
                        ComboFriendID = Convert.ToInt32(row["ComboFollowerID"]),
                        ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                        ComboUserName = commentor.UserName,
                        ComboDisplayName = commentor.DisplayName,
                        ProfilePic = creator.GetColumn("ProfilePic").ToString(),
                        FriendProfilePic = commentor.GetColumn("ProfilePic").ToString(),
                        IsFollowUser = follow
                    };
                }).ToList();

                ComboNotification notification = new ComboNotification();
                notification.AddNew();
                notification.ComboUserID = creator.ComboUserID;
                if (follow)
                    notification.NotificationType = (int)Combo.Models.NotificationType.FOLLOW_FIREND; // follow friend
                else
                    notification.NotificationType = (int)Combo.Models.NotificationType.UNFOLLOW_FRIEND; // unfollow friend
                notification.NotificationDate = DateTime.UtcNow;
                notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(arequest);
                notification.IsRead = false;
                notification.Save();

                NotificationUserSettings settings = new NotificationUserSettings();
                settings.LoadByPrimaryKey(creator.ComboUserID, (int)Combo.Models.NotificationType.FOLLOW_FIREND);
                bool notify = false;
                if (settings.RowCount == 0)
                    notify = true;
                else
                    notify = settings.CanGetNotification(creator.ComboUserID, commentor.ComboUserID, (int)Combo.Models.NotificationType.FOLLOW_FIREND);
                if (notify)
                {
                    List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row =>
                    {
                        return new Models.ComboNotification
                        {
                            ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]),
                            ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                            IsRead = Convert.ToBoolean(row["IsRead"]),
                            NotificationBody = row["NotificationBody"].ToString(),
                            NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                            NotificationType = Convert.ToInt32(row["NotificationType"])
                        };
                    }).ToList();

                    SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), creator.DeviceID);
                }
                /**************************/
            }

            _response.Entity = null;
            SetContentResult(_response);
        }
        public void ToggleLikePostByID(int id,int userid)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";

            ComboPostAttachment att = new ComboPostAttachment();
            att.GetPostAttachmentsByPostID(id);

            ComboPostLike likes = new ComboPostLike();
            if (!likes.LoadByPrimaryKey(userid, id))
            {
                likes.AddNew();
                likes.ComboPostID = id;
                likes.ComboUserID = userid;
                likes.Save();

                // save notification and push it to device
                ComboPost post = new ComboPost();
                post.LoadByPrimaryKey(id);
                ComboUser creator = new ComboUser();
                ComboUser liker = new ComboUser();
                creator.LoadByPrimaryKey(post.ComboUserID);
                liker.GetUserByUserId(userid);
                if (creator.ComboUserID != liker.ComboUserID)
                {
                    List<Models.ComboPostLike> alike = likes.DefaultView.Table.AsEnumerable().Select(row =>
                        {
                            return new Models.ComboPostLike
                            {
                                ComboPostID = Convert.ToInt32(row["ComboPostID"]),
                                ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                                UserName = liker.UserName,
                                DisplayName = liker.DisplayName,
                                PostText = post.PostText,
                                ProfilePic = liker.GetColumn("ProfilePic").ToString(),
                                Attachments = att.DefaultView.Table.AsEnumerable().Select(r =>
                                {
                                    return new Models.Attachment
                                    {
                                        AttachmentID = Convert.ToInt32(r["AttachmentID"]),
                                        Path = r["Path"].ToString(),
                                        AttachmentTypeID = Convert.ToInt32(r["AttachmentTypeID"]),
                                        ThumbsPath = r["ThumbsPath"].ToString()
                                    };
                                }).ToList(),
                            };
                        }).ToList();

                    ComboNotification notification = new ComboNotification();
                    notification.AddNew();
                    notification.ComboUserID = post.ComboUserID;
                    notification.NotificationType = (int)Combo.Models.NotificationType.LIKE; // like
                    notification.NotificationDate = DateTime.UtcNow;
                    notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(alike);
                    notification.IsRead = false;
                    notification.Save();

                    NotificationUserSettings settings = new NotificationUserSettings();
                    settings.LoadByPrimaryKey(creator.ComboUserID, (int)Combo.Models.NotificationType.LIKE);
                    bool notify = false;
                    if (settings.RowCount == 0)
                        notify = true;
                    else
                        notify = settings.CanGetNotification(creator.ComboUserID, liker.ComboUserID, (int)Combo.Models.NotificationType.LIKE);
                    if (notify)
                    {
                        List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row =>
                            {
                                return new Models.ComboNotification
                                {
                                    ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]),
                                    ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                                    IsRead = Convert.ToBoolean(row["IsRead"]),
                                    NotificationBody = row["NotificationBody"].ToString(),
                                    NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                                    NotificationType = Convert.ToInt32(row["NotificationType"])
                                };
                            }
                            ).ToList();

                        string notification_response = SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), creator.DeviceID);
                    }
                }
            }
            else
            {
                likes.MarkAsDeleted();
                likes.Save();
            }

            likes.GetPostLikesByPostID(id);

            List<Models.ComboPostLike> Alllikes = likes.DefaultView.Table.AsEnumerable().Select(row =>
            {
                return new Models.ComboPostLike
                {
                    ComboPostID = Convert.ToInt32(row["ComboPostID"]),
                    ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                    UserName = row["UserName"].ToString(),
                    DisplayName = row["DisplayName"].ToString(),
                };
            }).ToList();

            _response.Entity = Alllikes;
            SetContentResult(_response);
        }
        public void AddMessageComment(Models.ComboComment comment)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";

            BLL.ComboComment newComment = new ComboComment();
            newComment.AddNew();

            if (comment.ComboUserID != 0)
                newComment.ComboUserID = comment.ComboUserID;
            else
            {
                _response.ErrorCode = 30;
                _response.ErrorMsg = "Can't insert commnet. No user id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            if (comment.ComboMsgID != 0)
                newComment.ComboMsgID = comment.ComboMsgID;
            else
            {
                _response.ErrorCode = 31;
                _response.ErrorMsg = "Can't insert comment. No Msg id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            newComment.CommentText = comment.CommentText.Replace("\n", " "); ;
            newComment.CommentDate = DateTime.UtcNow;
            newComment.IsRead = false;
            newComment.Save();

            /**************************/
            // save notification and push it to device
            ComboMsg post = new ComboMsg();
            post.LoadByPrimaryKey(comment.ComboMsgID);
            ComboUser creator = new ComboUser();
            ComboUser commentor = new ComboUser();
            creator.LoadByPrimaryKey(post.ComboUserID);
            commentor.GetUserByUserId(comment.ComboUserID);

            if (creator.ComboUserID != commentor.ComboUserID)
            {
                List<Models.ComboComment> acomment = newComment.DefaultView.Table.AsEnumerable().Select(row =>
                {
                    return new Models.ComboComment
                    {
                        ComboMsgID = Convert.ToInt32(row["ComboMsgID"]),
                        ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                        ComboUserName = commentor.UserName,
                        ComboDisplayName = commentor.DisplayName,
                        CommentText = newComment.CommentText,
                        CommentDate = newComment.CommentDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                        ProfilePic = commentor.GetColumn("ProfilePic").ToString()
                    };
                }).ToList();

                ComboNotification notification = new ComboNotification();
                notification.AddNew();
                notification.ComboUserID = post.ComboUserID;
                notification.NotificationType = (int)Combo.Models.NotificationType.COMMENT_ON_MESSAGE; // add comment to post
                notification.NotificationDate = DateTime.UtcNow;
                notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(acomment);
                notification.IsRead = false;
                notification.Save();

                NotificationUserSettings settings = new NotificationUserSettings();
                settings.LoadByPrimaryKey(creator.ComboUserID, (int)Combo.Models.NotificationType.COMMENT_ON_MESSAGE);
                bool notify = false;
                if (settings.RowCount == 0)
                    notify = true;
                else
                    notify = settings.CanGetNotification(creator.ComboUserID, commentor.ComboUserID, (int)Combo.Models.NotificationType.COMMENT_ON_MESSAGE);
                if (notify)
                {
                    List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row =>
                    {
                        return new Models.ComboNotification
                        {
                            ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]),
                            ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                            IsRead = Convert.ToBoolean(row["IsRead"]),
                            NotificationBody = row["NotificationBody"].ToString(),
                            NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                            NotificationType = Convert.ToInt32(row["NotificationType"])
                        };
                    }).ToList();

                    SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), creator.DeviceID);
                }
            }
            /**************************/

            JavaScriptSerializer js = new JavaScriptSerializer();
            Models.Attachment[] att = js.Deserialize<Models.Attachment[]>(js.Serialize(comment.Attachments));

            ComboMsgAttachment attachment = new ComboMsgAttachment();
            foreach (Models.Attachment item in att)
            {
                attachment.AddNew();
                attachment.AttachmentID = item.AttachmentID;
                attachment.ComboMsgID = newComment.ComboMsgID;

            }
            attachment.Save();

            comment.ComboCommentID = newComment.ComboCommentID;
            comment.CommentDate = newComment.CommentDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
            _response.Entity = new Models.ComboComment[] { comment };

            SetContentResult(_response);
            return;
        }
        public void SharePost(int PostId, int UserId)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";

            ComboPostShare post = new ComboPostShare();
            post.AddNew();
            post.ComboPostID = PostId;
            post.ShareDate = DateTime.UtcNow;
            post.ComboUserID = UserId;
            post.Save();

            /**************************/
            // save notification and push it to device
            ComboPost ref_post = new ComboPost();
            ref_post.LoadByPrimaryKey(PostId);

            ComboPostAttachment att = new ComboPostAttachment();
            att.GetPostAttachmentsByPostID(PostId);

            ComboUser creator = new ComboUser();
            ComboUser requester = new ComboUser();
            creator.LoadByPrimaryKey(ref_post.ComboUserID);
            requester.LoadByPrimaryKey(UserId);
            if (creator.ComboUserID != requester.ComboUserID)
            {
                List<Models.ComboSharePost> info = post.DefaultView.Table.AsEnumerable().Select(row =>
                {
                    return new Models.ComboSharePost
                    {
                        ComboFriendID = requester.ComboUserID,
                        ComboUserID = creator.ComboUserID,
                        ComboUserName = creator.UserName,
                        ComboFriendName = requester.UserName,
                        ComboFriendDisplayName = requester.DisplayName,
                        ComboPostID = Convert.ToInt32(row["ComboPostID"].ToString()),
                        PostText = ref_post.PostText,
                        Attachments = att.DefaultView.Table.AsEnumerable().Select(r =>
                        {
                            return new Models.Attachment
                            {
                                AttachmentID = Convert.ToInt32(r["AttachmentID"]),
                                Path = r["Path"].ToString(),
                                AttachmentTypeID = Convert.ToInt32(r["AttachmentTypeID"]),
                                ThumbsPath = r["ThumbsPath"].ToString()
                            };
                        }).ToList(),
                    };
                }).ToList();

                ComboNotification notification = new ComboNotification();
                notification.AddNew();
                notification.ComboUserID = creator.ComboUserID;
                notification.NotificationType = (int)Combo.Models.NotificationType.SHARE_POST; // share post
                notification.NotificationDate = DateTime.UtcNow;
                notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(info);
                notification.IsRead = false;
                notification.Save();

                NotificationUserSettings settings = new NotificationUserSettings();
                settings.LoadByPrimaryKey(creator.ComboUserID, (int)Combo.Models.NotificationType.SHARE_POST);
                bool notify = false;
                if (settings.RowCount == 0)
                    notify = true;
                else
                    notify = settings.CanGetNotification(creator.ComboUserID, requester.ComboUserID, (int)Combo.Models.NotificationType.SHARE_POST);
                if (notify)
                {
                    List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row =>
                    {
                        return new Models.ComboNotification
                        {
                            ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]),
                            ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                            IsRead = Convert.ToBoolean(row["IsRead"]),
                            NotificationBody = row["NotificationBody"].ToString(),
                            NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                            NotificationType = Convert.ToInt32(row["NotificationType"])
                        };
                    }).ToList();

                    SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), creator.DeviceID);
                }
            }
            /**************************/

            _response.Entity = null;
            SetContentResult(_response);
        }
        public void SetNotificationSettings(object NotificationUserSetting)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";

            NotificationUserSettings setting = new NotificationUserSettings();
            JavaScriptSerializer js = new JavaScriptSerializer();
            Models.NotificationUserSetting[] usersettings = js.Deserialize<Models.NotificationUserSetting[]>(js.Serialize(NotificationUserSetting));
            if (usersettings != null)
            {
                foreach (Models.NotificationUserSetting item in usersettings)
                {
                    if (!setting.LoadByPrimaryKey(item.ComboUserID, item.NotificationTypeID))
                    {
                        setting.AddNew();
                        setting.NotificationTypeID = item.NotificationTypeID;
                        setting.ComboUserID = item.ComboUserID;
                        setting.Status = item.Status;
                        setting.Save();
                    }
                    else
                    {
                        setting.Status = item.Status;
                        setting.Save();
                    }
                }

            }

            _response.Entity = null;
            SetContentResult(_response);
        }
        public void AddMessage(Models.ComboMessage msg)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";

            if (string.IsNullOrEmpty(msg.ToIds))
            {
                _response.ErrorCode = 30;
                _response.ErrorMsg = "Can't add Msg. No to id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            if (msg.ToIds.Split(',').Length == 0)
            {
                _response.ErrorCode = 30;
                _response.ErrorMsg = "Can't add Msg. No to idsor error in format .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            BLL.ComboMsg newMsg = new ComboMsg();
            newMsg.AddNew();

            if (msg.ComboUserID != 0)
                newMsg.ComboUserID = msg.ComboUserID;
            else
            {
                _response.ErrorCode = 30;
                _response.ErrorMsg = "Can't add Msg. No user id .";
                _response.bool_result = false;
                SetContentResult(_response);
                return;
            }

            newMsg.MsgText = msg.MsgText.Replace("\n", " "); ;
            newMsg.MsgDate = DateTime.UtcNow;
            newMsg.IsRead = false;
            newMsg.Save();

            string[] ToIds = msg.ToIds.Split(',');
            ComboUserMsg tomsg = new ComboUserMsg();
            foreach (string item in ToIds)
            {
                tomsg.AddNew();
                tomsg.ComboUserID = Convert.ToInt32(item);
                tomsg.ComboMsgID = newMsg.ComboMsgID;
            }
            tomsg.Save();

            /**************************/
            // save notification and push it to device
            ComboUser creator = new ComboUser();
            ComboUser commentor = new ComboUser();
            creator.GetUserByUserId(newMsg.ComboUserID);

            List<Models.ComboMessage> amsg = newMsg.DefaultView.Table.AsEnumerable().Select(row =>
            {
                return new Models.ComboMessage
                {
                    ComboMsgID = Convert.ToInt32(row["ComboMsgID"]),
                    ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                    ComboUserName = creator.UserName,
                    ComboUserDisplayName = creator.DisplayName,
                    MsgText = newMsg.MsgText,
                    MsgDate = newMsg.MsgDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                    ProfilePic = creator.GetColumn("ProfilePic").ToString()
                };
            }).ToList();

            foreach (string item in ToIds)
            {
                ComboNotification notification = new ComboNotification();
                notification.AddNew();
                notification.ComboUserID = Convert.ToInt32(item);
                notification.NotificationType = (int)Combo.Models.NotificationType.RECEIVE_MSG; // new Msg recieved
                notification.NotificationDate = DateTime.UtcNow;
                notification.NotificationBody = Newtonsoft.Json.JsonConvert.SerializeObject(amsg);
                notification.IsRead = false;
                notification.Save();

                commentor.GetUserByUserId(Convert.ToInt32(item));

                NotificationUserSettings settings = new NotificationUserSettings();
                settings.LoadByPrimaryKey(Convert.ToInt32(item), (int)Combo.Models.NotificationType.RECEIVE_MSG);
                bool notify = false;
                if (settings.RowCount == 0)
                    notify = true;
                else
                    notify = settings.CanGetNotification(commentor.ComboUserID, creator.ComboUserID, (int)Combo.Models.NotificationType.FOLLOW_FIREND);
                if (notify)
                {
                    List<Models.ComboNotification> notificationJson = notification.DefaultView.Table.AsEnumerable().Select(row =>
                    {
                        return new Models.ComboNotification
                        {
                            ComboNotificationID = Convert.ToInt32(row["ComboNotificationID"]),
                            ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                            IsRead = Convert.ToBoolean(row["IsRead"]),
                            NotificationBody = row["NotificationBody"].ToString(),
                            NotificationDate = Convert.ToDateTime(row["NotificationDate"].ToString()).Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
                            NotificationType = Convert.ToInt32(row["NotificationType"])
                        };
                    }).ToList();

                    SendGCMNotification(Newtonsoft.Json.JsonConvert.SerializeObject(notificationJson), commentor.DeviceID);
                }
            }
            /**************************/

            JavaScriptSerializer js = new JavaScriptSerializer();
            Models.Attachment[] att = js.Deserialize<Models.Attachment[]>(js.Serialize(msg.Attachments));

            ComboMsgAttachment attachment = new ComboMsgAttachment();
            foreach (Models.Attachment item in att)
            {
                attachment.AddNew();
                attachment.AttachmentID = item.AttachmentID;
                attachment.ComboMsgID = newMsg.ComboMsgID;

            }
            attachment.Save();

            msg.ComboMsgID = newMsg.ComboMsgID;
            msg.MsgDate = newMsg.MsgDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
            _response.Entity = new Models.ComboMessage[] { msg };

            SetContentResult(_response);
            return;
        }
        public void GetNotificationSettings(int userID)
        {
            Models.ComboResponse _response = new Models.ComboResponse();
            _response.bool_result = true;
            _response.ErrorCode = 0;
            _response.ErrorMsg = "";

            NotificationUserSettings settings = new NotificationUserSettings();
            settings.GetNotificationSettingsbyUserId(userID);
            List<Models.NotificationUserSetting> allnotifications = settings.DefaultView.Table.AsEnumerable().Select(row =>
            {
                return new Models.NotificationUserSetting
                {
                    ComboUserID = Convert.ToInt32(row["ComboUserID"]),
                    NotificationTypeID = Convert.ToInt32(row["NotificationTypeID"]),
                    Status = Convert.ToInt16(row["Status"])
                };
            }).ToList();

            _response.Entity = allnotifications;
            SetContentResult(_response);
            //return _response;
        }